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(): # initialize pygame, settings, and screen object pygame.mixer.pre_init(22050, -16, 2, 512) pygame.mixer.init() pygame.init() laser = pygame.mixer.music crash = pygame.mixer.music ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Space Invaders") #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) #Make a group to store bullets in bullets = Group() alienBullets = Group() aliens = Group() motherships = Group() bunkers = Group() #create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) gf.create_bunkers(ai_settings, screen, ship, aliens, bunkers) bg_color = (230, 230, 230) mainMenu = MainMenu(screen) mainMenu.wait() #start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, alienBullets, bunkers, motherships, laser, crash) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, alienBullets, bunkers, motherships, crash) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets, motherships) gf.update_bunkers(ai_settings, screen, sb, ship, aliens, bullets, bunkers) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, alienBullets, bunkers, motherships)
def run_game(): # initialize game, create screen object, group of bullets, and group of aliens pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") play_button = butt.Button(ai_settings, screen, "Normal") mode_button = butt.ModeButton(ai_settings, screen, "Highscores") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) bullets = Group() alien_bullets = Group() aliens = Group() bunkers = Group() bonus = Group() explosions = Group() gf.create_fleet(ai_settings, screen, stats, sb, ship, aliens, alien_bullets, explosions) # fleet gf.create_bunkers(ai_settings, bunkers) # bunkers bonus_tic = randrange(0, 1000) # Main loop for game while True: # Listen for user activity, and input all data. gf is a # file alias # tic.increment_tic() gf.check_events(ai_settings, screen, stats, play_button, mode_button, sb, ship, bullets, aliens, alien_bullets, bunkers, bonus, explosions) if stats.game_active: # put things in motion if bonus_tic > 10500: # spawn bonus gf.create_bonus(ai_settings, screen, stats, sb, bullets, bonus, explosions) bonus_tic = randrange(0, 1000) bonus_tic += 1 ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, bullets, aliens, alien_bullets, bunkers, bonus, explosions) gf.update_alien_bullets(ai_settings, stats, ship, alien_bullets, bunkers) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bonus) gf.explosion_update(explosions) else: file = open('scores.txt', 'w') check = 0 while check != 10: file.writelines(str(stats.high_score[check])) file.writelines('\n') check += 1 file.close() gf.update_screen(ai_settings, screen, stats, play_button, mode_button, sb, ship, bullets, aliens, alien_bullets, bunkers, bonus, explosions)
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 run_game(): # Initialize pygame, settings, and screen object pygame.mixer.pre_init(44100, -16, 1, 512) pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height) ) game_master = GameMaster(screen) title_screen = TitleScreen(screen) pygame.display.set_caption("Space Invaders") # Make the Play button play_button = Button(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, group of bunkers, a group of bullets, and a group of aliens ship = Ship(ai_settings, screen) bunkers = Group() bullets = Group() aliens = Group() lasers = Group() game_master.last_laser_shot = pygame.time.get_ticks() game_master.next_laser_shot = game_master.last_laser_shot + random.randrange(250, 2000) gf.create_bunkers(bunkers, screen) gf.create_fleet(ai_settings, screen, aliens) # Start the main loop for the game while True: gf.check_events(ai_settings, game_master, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, game_master, screen, stats, sb, bunkers, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) gf.update_lasers(ai_settings, screen, game_master, aliens, lasers, ship, bunkers, stats, sb, bullets) gf.update_screen(ai_settings, screen, sb, ship, bunkers, aliens, game_master.alien_explosions, lasers, bullets, game_master.ufo) game_master.check_ufo_spawn(screen, bullets, stats, sb) else: gf.update_title_screen(play_button, title_screen)
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("Space Invaders") # Makes the Play button. play_button = Button(ai_settings, screen, "Play Game") # Makes the game title. titlefont = pygame.font.SysFont('Comic Sans MS', 70) game_title = titlefont.render('Alien Invasion', False, (255, 255, 255)) # Makes the infosheet scoresheet = pygame.image.load('images/infosheet.png') # Make the High Scores button. high_scores_button = Button(ai_settings, screen, "High Scores") high_scores_button.make_high_scores_button("High Scores") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Make a ship ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() albullets = Group() bunkers = Group() gf.create_bunkers(ai_settings, screen, bunkers) ufo = Ufo(ai_settings, screen) ufo.blitme() # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) # Load sounds ai_settings.change_sound = pygame.mixer.Sound("sounds/boop.wav") # Start the main loop for the game. while True: # Check for key presses or mouse clicks gf.check_events(ai_settings, screen, stats, sb, play_button, high_scores_button, ship, aliens, bullets, bunkers) if stats.game_active: ticks = pygame.time.get_ticks() ai_settings.time = ticks if ai_settings.time >= 13000: ai_settings.change_sound = pygame.mixer.Sound( "sounds/boop2.wav") if ai_settings.time >= 20000: ai_settings.change_sound = pygame.mixer.Sound( "sounds/boop3.wav") # Move the ship left/right ship.update(ai_settings) # Move bullets and check for alien collisions gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, albullets, ufo, bunkers) # Move the aliens and check for edge collisions gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets, bunkers) # Move the UFO across gf.update_ufo(ai_settings, screen, stats, sb, ship, aliens, bullets, ufo) gf.drop_lead(ai_settings, screen, stats, sb, ship, aliens, bullets, albullets, bunkers) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, albullets, play_button, high_scores_button, game_title, scoresheet, ufo, bunkers)
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("Space Invaders") pygame.mixer.music.load('Super Space Invader Theme.mp3') pygame.mixer.music.play(-1) # Make the Play button. high_button = Button1(ai_settings, screen, "") play_button = Button(ai_settings, screen, "Play Now") high_score_file = open("high_score.txt", "r") high_score = int(high_score_file.read()) high_score_file.close() print("The high score is", high_score) # Create an instance to store game statistics, and 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() bullets2 = Group() aliens = Group() bunkers = Group() bunker = Bunker(ai_settings, screen) alien = Alien(ai_settings, screen) # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) gf.create_bunkers(ai_settings, screen, ship, bunkers) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, high_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, alien, aliens, bunker, bunkers, bullets, bullets2, play_button, high_button) gf.update_bullets2(ai_settings, screen, stats, sb, ship, aliens, bunker, bunkers, bullets, bullets2) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets, bullets2) if pygame.time.get_ticks() % 300 == 10: gf.fire_bullet2(ai_settings, screen, aliens, bullets2) gf.update_screen(ai_settings, screen, stats, sb, ship, alien, aliens, bunker, bunkers, bullets, bullets2, play_button, high_button) if stats.score > high_score: high_score_file = open("high_score.txt", "w") high_score_file.write(str(stats.score)) high_score_file.close()