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


Python Space.draw方法代码示例

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


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

示例1: __init__

# 需要导入模块: from space import Space [as 别名]
# 或者: from space.Space import draw [as 别名]
class Game:

    """PyFighter - The game of the year."""

    ALIEN_DELAY = 2000
    ALIEN_DELTA = 200

    def __init__(self):
        """Initialize game engine."""
        pygame.init()
        self.screen = pygame.display.set_mode(Data.RESOLUTION)
        pygame.display.set_caption('PyFighter [using PyGame {0}]'.format(Data.PYGAME_INFO))

        self.clock = pygame.time.Clock()
        self.space = Space()
        self.story = _Story()
        self.audio = _Audio()

        self.player = pygame.sprite.GroupSingle(Player())
        self.aliens = pygame.sprite.Group()
        self.beams  = pygame.sprite.Group()

        self.timer = 0

    def play(self):
        """Start the game."""
        self.story.play_intro(self.screen)
        self.audio.play_music()
        self._idle_loop()

    def _idle_loop(self):
        """Main loop of the game."""
        while True:
            self._process_events()
            self._update_game()
            self._draw_screen()

    def _process_events(self):
        """Analyze all proper events from the queue."""
        for event in pygame.event.get():
            if event.type == QUIT:
                self._game_over(False)
            elif event.key == K_SPACE:
                # Generate the rocket and store it in the list.
                self.audio.play_laser()
                self.beams.add(Laser(self.player.sprite.rect))
            else:
                self.player.sprite.handle(event)

    def _update_game(self):
        """Calculate all changes."""
        self.space.update()
        self.player.update()
        self.beams.update()
        self._check_state()
        self.aliens.update()

        # Generate next alien.
        delay = Game.ALIEN_DELAY - Game.ALIEN_DELTA * Score.levelNo
        if pygame.time.get_ticks() - self.timer > delay and len(self.aliens) < Score.levelNo:
            self.aliens.add(Alien())
            self.timer = pygame.time.get_ticks()

        self._check_state()

    def _check_state(self):
        """Check all game actions."""
        # Check if the player was hit or if alien went past the screen.
        if pygame.sprite.spritecollideany(self.player.sprite, self.aliens) or Score.livesNo == 0:
            self._game_over(False)

        # Check for beams collisions.
        result = pygame.sprite.groupcollide(self.beams, self.aliens, True, True)
        for hits in result.values():
            Score.score += len(hits)
            Score.count += len(hits)

        # Increase level of difficulty if proper no of ships was destroyed
        if Score.count >= Score.levelNo:
            Score.count -= Score.levelNo
            Score.levelNo += 1

        # Show good ending in case player reached last level.
        if Score.levelNo == 7:
            self._game_over(True)

    def _draw_screen(self):
        """Draw everything."""
        self.space.draw(self.screen)
        self.player.draw(self.screen)
        self.beams.draw(self.screen)
        self.aliens.draw(self.screen)

        Score.draw(self.screen)

        # Show the screen.
        pygame.display.flip()
        self.clock.tick(Data.FRAMES_SEC)

    def _game_over(self, isGood):
#.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:103,代码来源:


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