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
    sn = Snake(width=width, height=height, block=block)
    x_snake, y_snake = sn.get_random_location()
    snake = [(x_snake, y_snake)]
    direction = 'right'
    eaten = True
    while True:
        # Draw scene
        gr = Graphic(scene_width=width,
                     scene_height=height,
                     block=block,
                     bg_color=bg,
                     food_color=pink,
                     wall_color=black,
                     snake_color=blue)

        scene = gr.draw_scene(scene=screen)
        if eaten:
            x_food = random.randint(3 * block, width - 2 * block)
            y_food = random.randint(3 * block, height - 2 * block)
            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:
Beispiel #3
0
    snake = [(x_snake, y_snake)]
    direction = ''
    eaten = True
    score = 1
    # Draw scene
    gr = Graphic(scene_width=width,
                 scene_height=height,
                 info_zone=info_zone,
                 block=block,
                 bg_color=bg,
                 food_color=pink,
                 wall_color=black,
                 snake_color=blue)
    while True:

        scene = gr.draw_scene(scene=screen, score=score)
        if eaten:
            food = ()
            while food == ():
                x_food = random.randint(3 * block, width - 2 * block)
                y_food = random.randint(3 * block, height - 2 * block)
                +info_zone
                x_food = block * round(x_food / block)
                y_food = block * round(y_food / block)
                if (x_food, y_food) not in snake:
                    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: