def run_game(): clock = pygame.time.Clock() # 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") # Make the Play button. play_button = Button(ai_settings, screen, "Play") # Create an instance to store game statistics, and a scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Start start = Start(screen, stats) # Make a ship, a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) bullets = Group() bad_bullets = Group() aliens = Group() bunkers = Group() ufos = UFO(ai_settings, screen) # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, aliens) # Create bunkers gf.create_bunkers(ai_settings, screen, bunkers) pygame.mixer.music.load('sounds/music.mp3') pygame.mixer.music.play(-1) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, bad_bullets, start) if stats.game_active and stats.game_pause: # ship.update() ufos.update(screen) bad_bullets.update() gf.fire_bad_bullet(ai_settings, screen, aliens, bad_bullets) gf.update_bullets(ai_settings, screen, stats, sb, aliens, bullets, bunkers, bad_bullets, ufos) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bad_bullets) ship.update(stats) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, bad_bullets, play_button, bunkers, start, ufos) clock.tick(60)
def run_game(): clock = pygame.time.Clock() # 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("Space Invasion") # Make the Play button. play_button = Button(ai_settings, screen, "Play") high_score_button = h_score(ai_settings, screen, "HIGH SCORES") # Create an instance to store game statistics, and a scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) start = Start(screen, stats) # Make a ship, a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) bullets = Group() alien_bullets = Group() aliens = Group() bunkers = Group() ufos = UFO(ai_settings, screen) gf.create_fleet(ai_settings, screen, aliens) gf.create_bunkers(ai_settings, screen, bunkers) while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, alien_bullets, start) if stats.game_active and stats.game_pause: ufos.update(screen) alien_bullets.update() gf.alien_bullet(ai_settings, screen, aliens, alien_bullets) gf.update_bullets(ai_settings, screen, stats, sb, aliens, bullets, bunkers, alien_bullets, ufos) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, alien_bullets) ship.update(stats) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, alien_bullets, play_button, high_score_button, bunkers, start, ufos) clock.tick(60)
def _create_fleet(self): self.settings.alien_counter = 0 self.settings.faster = False """Create the fleet of aliens.""" # Create an alien and find the number of aliens in a row. # Spacing between each alien is equal to one alien width. alien = Alien1(self) alien_width, alien_height = alien.rect.size # available_space_x = self.settings.screen_width - (2 * 60) # number_aliens_x = available_space_x // (2 * alien_width) # Determine the number of rows of aliens that fit on the screen. ship_height = self.ship.rect.height available_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height) number_rows = available_space_y // (2 * alien_height) # Create ufo # Create the full fleet of aliens. for row_number in range(number_rows): if row_number == 0: alien = UFO(self) for alien_number in range(9): # Change the alien depending on which row it is if row_number == 1: alien = Alien1(self) if row_number == 2: alien = Alien2(self) if row_number == 3: alien = Alien3(self) if row_number == 4: alien = Alien4(self) self._create_alien(alien_number, row_number, alien) self.settings.alien_counter += 1
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 Inveders") clock = pygame.time.Clock() # make a ship ship = Ship(ai_settings, screen) ufo = UFO(ai_settings, screen) play_button = Button(screen, "Play", 200) score_button = Button(screen, "High Scores", 300) screen_rect = screen.get_rect() stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) bullets = Group() lasers = Group() aliens = Group() explosions = Group() bunkers = [ Bunker(screen_rect.width / 4, screen_rect.bottom - 150, screen, 1), Bunker(screen_rect.width / 2, screen_rect.bottom - 150, screen, 2), Bunker(3 * screen_rect.width / 4, screen_rect.bottom - 150, screen, 3), Bunker(screen_rect.width, screen_rect.bottom - 150, screen, 4) ] # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, score_button, ship, aliens, bullets) if stats.game_active: pygame.mixer.music.load('sounds/bgm.wav') pygame.mixer.music.set_volume(400) pygame.mixer.music.play(-1) ship.update() gf.update_ufo(ufo, ai_settings) gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, lasers, ufo, explosions, bunkers) gf.update_aliens(ai_settings, stats, screen, sb, ship, ufo, aliens, bullets, lasers) gf.update_explosions(explosions) gf.update_screen(ai_settings, stats, screen, sb, ship, ufo, aliens, bullets, lasers, play_button, score_button, explosions, bunkers) clock.tick(60)
def setup(): global aliens, bombs, bullets, bunkers, hits, shots, ufos aliens = [] a1 = TieFighter(230, 90) a2 = TieFighter(330, 90) a3 = TieFighter(430, 90) a4 = TieFighter(530, 90) a5 = TieFighter(630, 90) a6 = TieFighter(730, 90) a7 = Bug(230, 160) a8 = Bug(330, 160) a9 = Bug(430, 160) a10 = Bug(530, 160) a11 = Bug(630, 160) a12 = Bug(730, 160) a13 = Scrubby(230, 240) a14 = Scrubby(330, 240) a15 = Scrubby(430, 240) a16 = Scrubby(530, 240) a17 = Scrubby(630, 240) a18 = Scrubby(730, 240) tie_fighters = [a1, a2, a3, a4, a5, a6] bugs = [a7, a8, a9, a10, a11, a12] scrubbies = [a13, a14, a15, a16, a17, a18] aliens = tie_fighters + bugs + scrubbies for a in aliens: a.v_x = alien_speed ufo_x = random.randrange(3000, 10000) ufos = [UFO(ufo_x, 60)] b1 = Bunker(200, 400) b2 = Bunker(460, 400) b3 = Bunker(720, 400) bunkers = [b1, b2, b3] bombs = [] bullets = [] hits = 0 shots = 0
def spawnUFO(ai_settings, screen, ufo): now = time.time() sec = now % 60 sweep = UFO(ai_settings, screen) if int(sec) % 10 == 0 and int(sec) != 0 and int(sec) % 20 != 0: ufo.add(sweep)