예제 #1
0
    def move(self, game):
        """
        The logic of moving the snake that includes collision detection and eating too.
        :return: whether the snake is still alive
        """
        # next location
        x, y = self.head.get_location()
        old_direction = self.direction
        # Calculate the next coordinate based on moving direction
        new = Location(*[sum(i) for i in zip((x, y), self.direction.value)])

        # verify valid move
        collision = self.check_collision(*new.get_location())

        if not collision:
            self.head.set_location(*new.get_location())
            self._add_body_part(Location(x, y), old_direction)

            # check whether he found food
            if self.check_for_food(game.board, new.get_location()):
                game.board.generate_food()
                game.score_inc(500)
            else:
                self.body.pop(-1)

        return not collision
예제 #2
0
class Board:
    """Represent the game board"""
    def __init__(self, size):
        self.table_size = size
        self.food_location = Location(0, 0)

        self.generate_food()

    def get_food_location(self):
        return self.food_location.get_location()

    def generate_food(self):
        self.food_location = Location(*random_position())