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


Python Snake.get_head_pos方法代码示例

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


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

示例1: __init__

# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import get_head_pos [as 别名]

#.........这里部分代码省略.........
        for x in range(WIDTH + 2):  # pylint: disable=invalid-name
            self.screen.blit(self.wallblock, (
                x * BLOCK_SIZE,
                0,
            ))
            self.screen.blit(self.wallblockdark, (
                x * BLOCK_SIZE + 5,
                5,
            ))
            self.screen.blit(self.wallblock, (
                x * BLOCK_SIZE,
                (HEIGHT + 1) * BLOCK_SIZE,
            ))
            self.screen.blit(self.wallblockdark, (
                x * BLOCK_SIZE + 5,
                (HEIGHT + 1) * BLOCK_SIZE + 5,
            ))

    def loop(self):
        """The game's main loop"""
        while True:
            # check crash or move outside the limits
            if self.snake.outside_limits(WIDTH, HEIGHT) or self.snake.crashed:
                self.crashsound.play()
                return

            # draw screen with snake and foods
            self.food.draw()
            self.snake.draw()
            self.draw_walls()
            pygame.display.flip()

            # check if snake eates
            if self.food.get_pos() == self.snake.get_head_pos():
                self.eatsound.play()
                self.snake.grow()
                # food should not appear where the snake is
                self.food = Food(self.screen, 1, HEIGHT + 1, 1, WIDTH + 1)
                while self.food.get_pos() in self.snake.pos_list:
                    self.food = Food(self.screen, 1, HEIGHT + 1, 1, WIDTH + 1)
                self.eaten += 1
                # increase game speed
                if self.eaten % SPEED_INC == 0:
                    self.speed += SPEED_TICK

            # game speed control
            self.clock.tick(self.speed)

            # get the next event on queue
            event = pygame.event.poll()
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                actmotdir = self.snake.motion_dir
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                elif event.key == pygame.K_UP and actmotdir != DOWN:
                    self.snake.motion_dir = UP
                elif event.key == pygame.K_DOWN and actmotdir != UP:
                    self.snake.motion_dir = DOWN
                elif event.key == pygame.K_RIGHT and actmotdir != LEFT:
                    self.snake.motion_dir = RIGHT
                elif event.key == pygame.K_LEFT and actmotdir != RIGHT:
                    self.snake.motion_dir = LEFT

            # remove the snake and make movement
开发者ID:mrpelotazo,项目名称:snake,代码行数:70,代码来源:game.py


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