def run_game(): 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 = Button(ai_settings, screen, "PLAY") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) ufo = UFO(ai_settings, screen) bullets = Group() aliens = Group() enemy_bullets = Group() # Create the fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) ai_settings.alien_number = 0 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, enemy_bullets, ufo) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets, enemy_bullets, ufo) gf.update_ufo(ai_settings, ufo) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, enemy_bullets, ufo)
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") # Make the Play button. play_button = Button(ai_settings, screen, "Play") high_score_button = Button_high(ai_settings, screen, "High Score") startsc = Startscreen(ai_settings, screen, play_button, high_score_button) game = Game_sound() # Create an instance to store game statistics, and a scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Set the background color. bg_color = (230, 230, 230) # Make a ship, a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() #UFO = ufo(ai_settings, screen) ufo_respawn_time = randint(10, 18) * 1000 # 10-18s at level 1 ufo_timer = ufo_respawn_time delta_time = 0 # 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, game) if stats.game_active: ship.update() # gf.create_ufo(ai_settings,screen,aliens) gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, game) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_ufo(ai_settings, screen, ufo_respawn_time, ufo_timer, aliens, delta_time) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, high_score_button, startsc)
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 run_game(): 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 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, a group of barriers, and a UFO ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # barrier = Group() ufo = UFO(ai_settings, screen) # create the fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) # create a way to keep track of time (for the ufo spawn) time_elapsed = 0 clock = pygame.time.Clock() # Start the main loop for the game while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) dt = clock.tick() time_elapsed += dt if stats.game_active: ship.update() gf.update_ufo(ai_settings, screen, ufo) 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, ufo)
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") ufo_sound = pygame.mixer.Sound('sounds/UFO_Sound_Effect.wav') # Make a ship, a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) ufo = Ufo(ai_settings, screen) bullets = Group() aliens = Group() lasers = Group() bunkers = Group() # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) # 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 = PlayButton(ai_settings, screen, "Play") high_scores_button = HighScoresButton(ai_settings, screen, "High Scores") # Start the main loop for the game while True: gf.check_events(ai_settings, screen, stats, sb, play_button, high_scores_button, ship, aliens, ufo, bullets, bunkers) 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_ufo(ai_settings, stats, sb, ufo, bullets, ufo_sound) #gf.update_bunkers(ai_settings, bullets, lasers, bunkers) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, ufo, bullets, play_button, high_scores_button, bunkers)
def run_game(): 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 = Button(screen, "Play", "Highscores") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats, 'images/highscores.txt', play_button) ship = Ship(ai_settings, screen) ufo = Group() bullets = Group() alien_bullets = Group() aliens = Group() bunkers = Group() pygame.mixer.music.load('sounds/winter.wav') pygame.mixer.music.play(-1) menu = Menu(ai_settings, screen, play_button, aliens, alien_bullets) gf.create_ufo(ai_settings, screen, aliens, alien_bullets) while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, alien_bullets, bunkers) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, alien_bullets, menu, ufo, bunkers) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets, alien_bullets, menu, ufo) gf.update_ufo(ai_settings, screen, ufo) gf.update_bunker(bunkers) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, alien_bullets, menu, ufo, bunkers)
def run_game(): 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 play_button = Button(ai_settings, screen, "PLAY") hs = Highscorelist(ai_settings, screen, "HIGHSCORES") # Create an instance to store game statistics stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) bullets = Group() #ebullets = Group() aliens = Group() alien = Alien(ai_settings, screen) ufos = Group() ufo = UFO(ai_settings, screen) # Create the fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, hs) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, ufos) #gf.update_ebullets(ai_settings, screen, stats, sb, ship, aliens) gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) gf.update_ufo(ai_settings, screen, stats, sb, ship, bullets, ufos) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, hs, ufos)
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 frame_step(self, simplify=False, inputs=None, name="Player"): init_bullet_count = len(self.bullets) gf.check_events(self.ai_settings, self.screen, self.sounds, self.stats, self.sb, self.scores, self.play_button, self.high_score_button, self.ship, self.aliens, self.ufo, self.bullets, self.bullet_delay, self.barriers, self.alien_bullets, self.smokes, inputs) bullet_count = len(self.bullets) alien_count = len(self.aliens) ufo_state = self.ufo.hit ship_pos = self.ship.rect.left if init_bullet_count < bullet_count: self.bullet_delay = self.ai_settings.bullet_delay elif self.bullet_delay > 0: self.bullet_delay -= 1 if self.stats.game_active: gf.update_timers(self.alien_timer, self.ufo_timer, self.ship_timer, self.smoke_timer) gf.update_ship(self.stats, self.sb, self.scores, self.ship, self.aliens, self.ufo, self.bullets, self.alien_bullets, self.ship_timer, self.alien_timer, simplify) if not self.ship.hit: gf.update_bullets(self.ai_settings, self.screen, self.sounds, self.stats, self.sb, self.ship, self.aliens, self.ufo, self.bullets, self.bullet_delay, self.barriers, self.alien_bullets, self.smokes, self.alien_timer, self.ufo_timer, self.smoke_timer, simplify) gf.update_aliens(self.ai_settings, self.screen, self.sounds, self.ship, self.aliens, self.barriers, self.alien_bullets, self.alien_timer, simplify) gf.update_ufo(self.ufo, self.ufo_timer, simplify) gf.update_smokes(self.smokes, self.smoke_timer) gf.update_screen(self.ai_settings, self.screen, self.stats, self.sb, self.ship, self.aliens, self.ufo, self.bullets, self.menu_bg, self.play_button, self.high_score_button, self.barriers, self.alien_bullets, self.smokes, simplify) pygame.display.update() reward = 0.0 if len(self.aliens) > 0: if len(self.aliens) < alien_count or self.ufo.hit != ufo_state: reward += 0.5 elif len(self.bullets) == 0: reward -= 0.2 if gf.ship_in_invader_range(self.ai_settings, self.ship, self.aliens, self.ufo): reward += 0.1 else: reward -= 0.1 reward += gf.bullet_aim(self.ai_settings, self.bullets, self.aliens, self.ufo) reward += gf.ship_in_bullet_path(self.ship, self.alien_bullets) if reward > 1.0: reward = 1.0 elif reward < -1.0: reward = -1.0 print reward if alien_count < len(self.aliens): game_state = False reward = 1 self.inactive = 240 self.bullet_delay = 0 else: game_state = self.stats.game_active if self.stats.game_active is False or self.inactive <= 0: self.stats.game_active = True reward = -1 self.inactive = 240 self.bullet_delay = 0 self.scores.check_place(int(round(self.stats.score, -1)), name) gf.restart(self.ai_settings, self.screen, self.sounds, self.stats, self.sb, self.ship, self.aliens, self.ufo, self.bullets, self.barriers, self.alien_bullets, self.smokes) image_data = None if inputs is not None: if len(inputs) == 3: img = self.screen image_data = pygame.surfarray.array3d(img) clock.tick(self.ai_settings.fps) # self.inactive += reward return reward, image_data, game_state
def run_game(): 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 and high scores button play_button = Button(ai_settings, screen, "Play!") high_scores_button = HighScoresButton(ai_settings, screen, "High Scores") # 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, a group of barriers, and a UFO ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() barrier = Barrier(ai_settings, screen) ufo = UFO(ai_settings, screen) hs_init = open("high_scores.txt", "r+") stats.high_score = int(hs_init.readline()) stats.high_score_2 = int(hs_init.readline()) stats.high_score_3 = int(hs_init.readline()) hs_init.close() # Make the start screen start_screen = StartScreen( ai_settings, screen, aliens, "Alien Invasion!", "= 50", "= 100", "= 150", "= 300", "Gabriel Magallanes | For CPSC 386 (Video Game Design) | Cal State Fullerton | 2019" ) # Make the high score window high_score_window = HighScoreWindow(ai_settings, screen, stats) # Create the fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) # Create a way to keep track of time (for the ufo spawn) time_elapsed = 0 clock = pygame.time.Clock() # Start the main loop for the game while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, high_scores_button) if stats.game_active: ship.update() gf.update_ufo(ai_settings, screen, ufo) gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, high_score_window) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets, high_score_window) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, start_screen, play_button, ufo, barrier, high_scores_button)
def run_game(): pygame.init() clock = pygame.time.Clock() highscores = HighScore() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") sounds = Sound() play_button = Button(screen, pygame.image.load('images/play_btn.png'), 850, 400) high_score_button = Button(screen, pygame.image.load('images/high_score_btn.png'), 850, 600) menu_bg = Button(screen, pygame.image.load('images/menu.png'), ai_settings.screen_width / 2, ai_settings.screen_height / 2) stats = GameStats(ai_settings, highscores) sb = Scoreboard(ai_settings, screen, sounds, stats) ship = Ship(ai_settings, screen, sounds) bullets = Group() alien_bullets = Group() aliens = Group() ufo = UFO(ai_settings, screen, sounds) barriers = Group() smokes = Group() gf.create_fleet(ai_settings, screen, sounds, aliens) gf.create_barriers(ai_settings, screen, barriers) # timers used for animation and event checking alien_timer = Timer(ai_settings.alien_frame_factor) smoke_timer = Timer(8) ship_timer = Timer(4) ufo_timer = Timer(ai_settings.alien_frame_factor * 5) while True: clock.tick(60) gf.check_events(ai_settings, screen, sounds, stats, sb, highscores, play_button, high_score_button, ship, aliens, bullets, barriers, alien_bullets, smokes) if stats.game_active: gf.update_timers(alien_timer, ufo_timer, ship_timer, smoke_timer) gf.update_ship(stats, sb, highscores, ship, aliens, ufo, bullets, alien_bullets, ship_timer, alien_timer) if not ship.hit: gf.update_bullets(ai_settings, screen, sounds, stats, sb, ship, aliens, ufo, bullets, barriers, alien_bullets, smokes, alien_timer, ufo_timer, smoke_timer) gf.update_aliens(ai_settings, screen, sounds, ship, aliens, barriers, alien_bullets, alien_timer) gf.update_ufo(ufo, ufo_timer) gf.update_smokes(smokes, smoke_timer) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, ufo, bullets, menu_bg, play_button, high_score_button, barriers, alien_bullets, smokes)
def run_game(): # Initializes background setting needed to run properly pygame.init() # Initializes settings ai_settings = Settings() # Creates a display window where all of the game's graphic elements are drawn # The numbers represent the dimensions of the window screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Delay time delay = 0 timer = random.randint(400, 1001) # Create an instance to store game statistics amd create a scoreboard stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Makes a ship, group of bullets, alien bullets, group of aliens, and ufos ship = Ship(ai_settings, screen, stats) bunkers = Group() bullets = Group() aliens_1 = Group() aliens_2 = Group() aliens_3 = Group() alien_bullets = Group() ufo = Group() # Make the play button that also contains the startup screen play_button = Button(screen, "Play", ai_settings, alien_bullets, stats) score_button = ScoreButton(screen, "High Scores", ai_settings) back_button = ScoreButton(screen, "Back (B)", ai_settings) # Starts the main loop for the game while True: # Checks for any type of event gf.check_events(ai_settings, screen, stats, sb, play_button, score_button, ship, aliens_1, aliens_2, aliens_3, ufo, bullets, alien_bullets, bunkers) # Creates a new UFO at a random interval # Delay to prevent too many from spawning at once if delay == timer: gf.create_ufo(ai_settings, screen, ufo, alien_bullets, stats) # plays ufo sound ai_settings.ufo_sound.play() delay = 0 timer = random.randint(700, 1001) else: delay += 1 # Runs the game is active if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens_1, aliens_2, aliens_3, ufo, bullets, alien_bullets, bunkers) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens_1, aliens_2, aliens_3, ufo, bullets, alien_bullets, bunkers) gf.update_ufo(ai_settings, screen, stats, sb, ship, aliens_1, aliens_2, aliens_3, ufo, bullets, alien_bullets, bunkers) # Manages the screen updates gf.update_screen(ai_settings, screen, stats, sb, ship, aliens_1, aliens_2, aliens_3, ufo, bullets, alien_bullets, bunkers, play_button, score_button, back_button)