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


Python Snake.remove方法代码示例

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


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

示例1: __init__

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

#.........这里部分代码省略.........
                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
            self.snake.remove()
            self.snake.move()

    def game_over(self):
        """When crashed print "game over" and wait for Esc key"""
        self.clock.tick(LONG)
        self.snake.draw()
        self.draw_walls()

        for pos in self.snake.pos_list[1:]:
            self.screen.blit(self.snake.backblock, (
                pos[1] * BLOCK_SIZE,
                pos[0] * BLOCK_SIZE,
            ))
            pygame.display.flip()
            self.clock.tick(SHORT)

        while True:
            self.screen.blit(self.gameovertext, (
                (WIDTH - 4) * BLOCK_SIZE / 2,
                HEIGHT * BLOCK_SIZE / 2,
            ))
            pygame.display.flip()
            event = pygame.event.wait()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()

    def start(self):
        """The game starts here"""
        # press any key to start!!!
        self.draw_walls()
        self.screen.blit(self.starttext, (
            (WIDTH - 10) * BLOCK_SIZE / 2,
            HEIGHT * BLOCK_SIZE / 2,
        ))
        pygame.display.flip()

        waiting = True
        while waiting:
            event = pygame.event.wait()
            if event.type == pygame.KEYDOWN:
                waiting = False
        self.screen.fill(BLACK)

        # main loop
        self.loop()
        self.game_over()
开发者ID:mrpelotazo,项目名称:snake,代码行数:104,代码来源:game.py


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