Beispiel #1
0
    def play_game(self, snake, food, button_direction, score, screen, clock):
        """Launch the snake game.

        Parameters
        ----------
        snake : tuple[int]
            The snake coordinates.
        food : tuple[int]
            The food coordinates.
        button_direction : int
            The direction the snake will follow.
        score : int
            The score value.
        screen : type
            The scene.
        clock : int
            The speed of the snake.

        Returns
        -------
        snake: tuple[int]
            The snake's coordinates.
        food: tuple[int]
            The food's coordinates.
        score: int
            The updated score value.

        """

        gr = Graphic(scene_width=self.width, scene_height=self.height,
                     info_zone=self.info_zone, block=self.block,
                     bg_color=self.bg, food_color=self.pink,
                     wall_color=self.black, snake_color=self.blue)
        alive = True
        while alive:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    alive = False
            scene = gr.draw_scene(scene=screen, score=score)
            gr.draw_food(scene=scene, food=food)
            snake, food, score = self.move(
                snake, food, button_direction, score)
            scene = gr.draw_snake(scene=scene, snake=snake)
            pygame.display.update()
            pygame.time.Clock().tick(clock)
            return snake, food, score
Beispiel #2
0
            x_food = block * round(x_food / block)
            y_food = block * round(y_food / block)
            food = (x_food, y_food)
            eaten = False
        gr.draw_food(scene=scene, food=food)
        # time.sleep(3)
        keys_pressed = pygame.event.get()
        for event in keys_pressed:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    snake, direction, eaten = sn.turn_up(snake, food)
                elif event.key == pygame.K_DOWN:
                    snake, direction, eaten = sn.turn_down(snake, food)
                elif event.key == pygame.K_RIGHT:
                    snake, direction, eaten = sn.turn_right(snake, food)
                elif event.key == pygame.K_LEFT:
                    snake, direction, eaten = sn.turn_left(snake, food)
                elif event.key == pygame.K_ESCAPE:
                    print('exit')
                    pygame.quit()
                    sys.exit(0)

        if sn.is_dead(snake=snake, direction=direction):
            print('died')
            pygame.quit()
            sys.exit(0)

        scene = gr.draw_snake(scene=scene, snake=snake)
        pygame.display.update()
    # time.sleep(10)