def create_sprites(self): self.player = Player((PLAY_SCREEN_RECT.width / 2, PLAY_SCREEN_RECT.bottom - 20)) for i in xrange(20): Alien(((i % 10) * 40 + 20,(i / 10) * 40 + 20)) self.high_score = HighScore((PLAY_SCREEN_RECT.topright)) self.score = Score((PLAY_SCREEN_RECT.topright[0], PLAY_SCREEN_RECT.topright[1] + 75))
class ScenePlay(SceneBase): def __init__(self, game_manager): SceneBase.__init__(self, game_manager) self.load_images() self.load_sounds() self.create_group() self.create_sprites() self.playing = True def update(self, filtered_events, pressed_keys): self.player.input(pressed_keys) self.all.update() self.collide_alien_shot() if self.playing: self.collide_player_beam() if self.check_gameover(): self.game_manager.change_scene(SceneGameOver(self.game_manager, self)) self.playing = False elif self.check_gameclear(): self.game_manager.change_scene(SceneGameClear(self.game_manager, self)) self.playing = False def draw(self, screen): self.all.draw(screen) self.score.draw(screen) self.high_score.draw(screen) def load_images(self): Alien.load_image() Beam.load_image() Explosion.load_image() Player.load_image() Shot.load_image() def load_sounds(self): Alien.load_sound() Player.load_sound() Shot.load_sound() def create_group(self): self.all = pygame.sprite.RenderUpdates() self.aliens = pygame.sprite.Group() self.beams = pygame.sprite.Group() self.shots = pygame.sprite.Group() Alien.containers = self.all, self.aliens Beam.containers = self.all, self.beams Explosion.containers = self.all Player.containers = self.all Shot.containers = self.all, self.shots def create_sprites(self): self.player = Player((PLAY_SCREEN_RECT.width / 2, PLAY_SCREEN_RECT.bottom - 20)) for i in xrange(20): Alien(((i % 10) * 40 + 20,(i / 10) * 40 + 20)) self.high_score = HighScore((PLAY_SCREEN_RECT.topright)) self.score = Score((PLAY_SCREEN_RECT.topright[0], PLAY_SCREEN_RECT.topright[1] + 75)) def collide_alien_shot(self): alien = pygame.sprite.groupcollide(self.aliens, self.shots, True, True) for alien in alien.keys(): Explosion(alien.rect.center) Alien.sound.play() self.score.add_score(alien.score) def collide_player_beam(self): if pygame.sprite.spritecollide(self.player, self.beams, True): Explosion(self.player.rect.center) Player.sound.play() self.player.kill() def check_gameclear(self): for alien in self.aliens: if alien.alive(): return False return True def check_gameover(self): if not self.player.alive(): return True for alien in self.aliens: if self.player.rect.centery <= alien.rect.centery: return True return False def update_score(self): if self.score.get_score() > self.high_score.get_score(): self.high_score.save_score(self.score.get_score())