def run_game(): # Initialize game and create a screen object. pg.init() # Init background settings that pygame needs to work properly ai_stts = Settings() screen = pg.display.set_mode((ai_stts.screen_width, ai_stts.screen_height)) # Create the display window pg.display.set_caption("Alien Invasion") # Create an instance of GameStats to store game statistics stats = GameStats(ai_stts) gf.load_score(stats) # Create an instance of Scoreboard to create a scoreboard sb = Scoreboard( ai_stts, screen, stats) # self.rect.center = self.screen_rect.center # self.rect.centerx = self.screen_rect.width / 2 # self.rect.centery += self.screen_rect.height / 4 screen_rect = screen.get_rect() # Make the Play button. play_button = Button(ai_stts, screen, "Play", screen_rect.width / 2, screen_rect.height / 2 + screen_rect.height / 4) pause_button = Button(ai_stts, screen, "Pause", screen_rect.width / 2, screen_rect.height / 2 + screen_rect.height / 4 + play_button.rect.height * 2) # Player's instance player = Ship(ai_stts, screen) # Make aliens aliens = Group() # Make a group to store bullets in bullets = Group() #start_timer = 0 # starter timer for countdown # Start the main loop for the game. while 1: gf.check_events(ai_stts, stats, sb, screen, play_button, pause_button, player, bullets, aliens) if stats.game_active: if player.invulnerable: if not ai_stts.countdown_running: start_timer = pg.time.get_ticks() ai_stts.countdown_running = True elif ai_stts.countdown_running: countdown_timer = (pg.time.get_ticks() - start_timer) / 1000 player.blinking(countdown_timer) if countdown_timer > ai_stts.invulnerable_time: player.invulnerable = False ai_stts.countdown_running = False player.blinking(countdown_timer) print("Jugador vuelve a ser vulnerable") player.update() gf.update_bullets(ai_stts, stats, sb, screen, player, aliens, bullets) gf.update_aliens(ai_stts, stats, screen, player, aliens) gf.update_screen(ai_stts, stats, sb, screen, player, aliens, bullets, play_button, pause_button)
def run_game(): # Initialize pygame, settings, and screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Set FPS FPS = 60 clock = pygame.time.Clock() # Make a ship, a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) bullets = Group() enemy_bullets = Group() aliens = Group() # Create an instance to store game statistics, a quit statement and create a scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) qs = Quit_State(ai_settings, screen) # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) # Make the Play and Game Over button. play_button = Button(ai_settings, screen, "PRESS ENTER") game_over = Game_Over(ai_settings, screen, "GAME OVER") # Load the high score. gf.load_score(stats) sb.prep_high_score() sb.show_score() # Start the main loop for the game. while True: clock.tick(FPS) gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets, sb, enemy_bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_enemy_bullets(ai_settings, screen, stats, sb, ship, aliens, enemy_bullets) gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets, enemy_bullets, game_over) if not stats.game_active: game_over.draw_button() gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, qs, enemy_bullets, game_over)
def run_game(): # Initialise pygame, settings and create a screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Alien Invasion') # Create an instance to store game statistics and create a scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Make the play button. play_button = Button(ai_settings, screen, "Play") # make a ship ship = Ship(ai_settings, screen) # Make a group to store bullets in. bullets = Group() aliens = Group() # Create the fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) # Load the high score from a json file gf.load_score(stats) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, sb, stats, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def run_game(): # Initialize pygame , settings and screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") gf.bg_music("game start.mp3") # Make the Play button. play_button = Button(ai_settings, screen, "Play") # Create an instance to store game statistics and create a scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Make a ship,a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) gf.load_score(stats) sb.prep_high_score() sb.show_score() # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)