def run_game(): # Initialize game, settings, and create a screen object. pygame.init() settings = Settings.getInstance() screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height)) pygame.display.set_caption(settings.window_caption) # Make the Play button. play_button = Button(screen, "Play") # Make Groupmanager em = EntityManager.getInstance() # Make ship ship = Ship(screen) em.add_single("ship", ship) # Make a group to store bullets in. em.add_group("bullets") # Make a group to store aliens in em.add_group("aliens") # Create statistics and scoreboard stats = GameStats() sb = Scoreboard(settings, screen, stats) # Load background. bg = BackGround() # Create explosions group em.add_group("explosions") # Start background music SoundManager.getInstance().play_music() # Make power up group em.add_group("power_ups") # Start the main loop for the game. while True: # Watch for keyboard and mouse events. gf.check_events(screen, stats, sb, play_button) if stats.game_active: em.update_singles() gf.update_bullets(screen, stats, sb) gf.update_aliens(screen, stats, sb) gf.update_explosions() gf.update_power_ups() gf.update_screen(screen, bg, stats, sb, play_button)
def create_explosion(screen, x, y): """ Creates an explosion """ # Generate indices in climbing order cols = settings.explosion_cols rows = settings.explosion_rows indices = [] for i in range(cols * rows): indices.append(i) # play explosion sound SoundManager.getInstance().explosion.play() # Return the explosion return Explosion(screen, x, y, (indices))
def check_keydown_events(event, screen): """Respond to keypresses.""" ship = ent_man.get_single("ship") if event.key == pygame.K_RIGHT: ship.moving_right = True elif event.key == pygame.K_LEFT: ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(screen) SoundManager.getInstance().laser.play() elif event.key == pygame.K_q: sys.exit(0)