def logic(self): self.collision_grid.update() for g in self.group_list: #For all Sprite groups... g.update() if len(ENEMIES) == 0: #If all enemies have been killed... enemysquadron.reset() enemy.Enemy.velocity[0] = abs(enemy.Enemy.velocity[0]) + 0.05 if enemy.Enemy.should_flip: #If at least one enemy has touched the side of the screen... enemy.Enemy.velocity[0] *= -1 enemysquadron.move_down() enemy.Enemy.should_flip = False if lives == 0 and self.game_running: #If we run out of lives... self.__game_over() enemysquadron.celebrate() self.game_running = False if random.uniform(1, 50000) < self.ufoSpawnRange: self.__add_ufo()
def __init__(self, *args): self.args = args #The arguments we can use to change this gamestate self.block_list = [] #The blocks available for use self.collision_grid = collisions.CollisionGrid(4, 4, 1) #The collision-handling system self.group_list = [BLOCKS, ENEMIES, PLAYER, HUD] #The groups of sprites to process self.ship = player.Ship() #The player self.ufo = ufo.UFO() self.ufoSpawnRange = 10 #The UFO self.hud_score = hudobject.HudObject(pygame.Surface((0, 0)), (16, 16)) self.hud_lives = hudobject.HudObject(pygame.Surface((0, 0)), (config.SCREEN_WIDTH-160, 16)) self.gameovertext = hudobject.HudObject(config.FONT.render("GAME OVER", False, color.WHITE).convert(config.DEPTH, config.FLAGS), config.SCREEN_RECT.center ) #The components of our HUD; let the player know how he's doing! self.game_running = True #False if we've gotten a game over self.key_actions = { pygame.K_ESCAPE: self.__return_to_menu , pygame.K_SPACE : self.ship.on_fire_bullet , pygame.K_F1 : config.toggle_fullscreen , pygame.K_c : self.__clear_blocks , pygame.K_d : config.toggle_debug , pygame.K_f : config.toggle_frame_limit, pygame.K_p : config.toggle_pause , pygame.K_u : self.__add_ufo , } #The keys available for use; key is keycode, element is a function self.mouse_actions = { 1: color.RED , 2: color.YELLOW, 3: color.BLUE , } #What clicking the mouse can do: key is mouse button, element is color PLAYER.add(self.ship) HUD.add(self.hud_score, self.hud_lives) enemysquadron.reset()