当前位置: 首页>>代码示例>>Python>>正文


Python Snake.is_collide方法代码示例

本文整理汇总了Python中snake.Snake.is_collide方法的典型用法代码示例。如果您正苦于以下问题:Python Snake.is_collide方法的具体用法?Python Snake.is_collide怎么用?Python Snake.is_collide使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在snake.Snake的用法示例。


在下文中一共展示了Snake.is_collide方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Game

# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import is_collide [as 别名]
class Game(object):
    def __init__(self, window):
        self.snake = Snake()
        self.apple = Apple()
        self.window = window
        self.grid = []
        self.score = 0

    def move_snake(self, key):
        """Manual move of snake"""
        if key in [KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT]:
            self.snake.move(key)
        else:
            self.snake.move(self.snake.last_direction)

        if self.snake.head == self.apple.position:
            self.snake.eat(self.apple, self.grid)
            self.score += 1

        if self.snake.is_collide():
            self.reset()

    def automove(self):
        """Deplace position of the snake with A* algorithm"""
        path = find_path(tuple(self.snake.head), tuple(self.apple.position), self.grid)
        move = path[0] if path else False

        if not move:
            move = self.any_possible_move()

        if move:
            if not self.snake.is_eating:
                self.snake.position.pop(0)

            self.snake.is_eating = False
            self.snake.position.append(move)
            self.snake.head = self.snake.position[-1]
            self.snake.tail = self.snake.position[0]

        if self.snake.head == self.apple.position:
            self.snake.eat(self.apple, self.grid)
            self.score += 1

        if self.snake.is_collide() or not move:
            self.reset()

    def any_possible_move(self):
        """Return any possible position"""
        neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        for i, j in neighbors:
            neighbor = self.snake.head[0] + i, self.snake.head[1] + j
            if self.grid[neighbor[0]][neighbor[1]] == 0:
                return [neighbor[0], neighbor[1]]
        return False

    def display(self):
        """display game"""
        self.grid[:] = []
        for line in range(HEIGHT):
            self.grid.append([])
            for column in range(WIDTH):
                if column == 0 or line == 0 or column == WIDTH - 1 or line == HEIGHT - 1:
                    self.grid[line].append(1)
                else:
                    self.grid[line].append(0)

        for line, column in self.snake.position:
            self.grid[line][column] = 1
            self.window.addstr(line, column, 's')

        line, column = self.apple.position
        self.window.addstr(line, column, 'a')

    def reset(self):
        """Reset game"""
        self.apple.reset()
        self.snake.reset()
        self.score = 0
开发者ID:marmelab,项目名称:snake_solver_one,代码行数:80,代码来源:game.py


注:本文中的snake.Snake.is_collide方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。