def __init__(self): self.score = 0 self.level = 1 # make the gui self.gui = GUI.GUI() # make the game object manager self.game_object_manager = GameObjectManager() # create the bullets temp_bullets = [] for index in range(Consts.MAX_BULLETS): temp_bullets.append(Bullet()) self.game_object_manager.add_game_object(temp_bullets[index]) # make a player self.player = Ship(Consts.CENTER, temp_bullets) self.game_object_manager.add_game_object(self.player) # spawn the asteroids self.spawn_asteroids() self.add_sounds() Sound.sound_manager.play_sound(Sound.sounds.begin)
class Game(): def __init__(self): self.score = 0 self.level = 1 # make the gui self.gui = GUI.GUI() # make the game object manager self.game_object_manager = GameObjectManager() # create the bullets temp_bullets = [] for index in range(Consts.MAX_BULLETS): temp_bullets.append(Bullet()) self.game_object_manager.add_game_object(temp_bullets[index]) # make a player self.player = Ship(Consts.CENTER, temp_bullets) self.game_object_manager.add_game_object(self.player) # spawn the asteroids self.spawn_asteroids() self.add_sounds() Sound.sound_manager.play_sound(Sound.sounds.begin) def reinit(self, deltaT): self.level = 1 self.score = 0 self.clear_everything_except_player() self.spawn_asteroids() self.player.reinit() self.game_object_manager.update(deltaT) def draw(self, screen): # clear the screen Consts.screen.fill(Consts.BLACK) # draw the game objects self.game_object_manager.draw(screen) # draw the ui self.draw_ui(screen) # flip the buffer pygame.display.flip() def update(self, deltaT): self.game_object_manager.update(deltaT) self.game_object_manager.collision_detection() self.game_logic(deltaT) self.split_hurt_asteroids() self.check_for_homing() def game_logic(self, deltaT): # if the player has died finish the game and restart if self.player.lives <= 0: self.show_end_screen() self.reinit(deltaT) # if there are no more asteroids advance the level if len(self.get_asteroids_remaining()) == 0: self.advance_level() def show_end_screen(self): self.gui.draw_text("You lost!",Consts.CENTER, Consts.screen) self.gui.draw_text("Score: " + str(self.score),(Consts.CENTER[0], Consts.CENTER[1]+50), Consts.screen) pygame.display.flip() pygame.time.delay(1500) def advance_level(self): self.level += 1 self.spawn_asteroids() self.player.reset_bullets() Sound.sound_manager.play_sound(Sound.sounds.begin) def spawn_asteroids(self): for index in range(0, self.level): self.game_object_manager.add_game_object(Asteroid(32, Consts.SCREENLENGTHS, self.player)) def clear_everything_except_player(self): for game_object in self.game_object_manager.game_objects: if not(isinstance(game_object,Ship) or isinstance(game_object, Bullet)): game_object.needs_removing = True def split_hurt_asteroids(self): # splits any asteroids that need splitting for go in self.game_object_manager.game_objects: if isinstance(go, Asteroid): if go.isHurt: self.increase_score(go.radius) if go.radius > 4: # if its big enough split it self.split_asteroid(go) if go.radius == 32: # chance an item drop when asteroid is 32 big self.chance_a_health_drop(go) else: # else just kill it go.needs_removing = True def split_asteroid(self, asteroid): for index in range(2): self.game_object_manager.add_game_object(Asteroid(asteroid.radius/2,Consts.SCREENLENGTHS, self.player, asteroid.position)) self.game_object_manager.remove_game_object(asteroid) def chance_a_health_drop(self, go): if random.randint(1,2) == 1: self.game_object_manager.add_game_object(Item(go.position)) def increase_score(self, amount): self.score += amount def get_asteroids_remaining(self): # Returns the amount of asteroids in the game asteroids = [] for game_object in self.game_object_manager.game_objects: if isinstance(game_object, Asteroid): asteroids.append(game_object) return asteroids def check_for_homing(self): small_count = 0 asteroids_in_game = self.get_asteroids_remaining() for asteroid in asteroids_in_game: if asteroid.radius == 4: small_count += 1 if small_count == len(asteroids_in_game): for asteroid in asteroids_in_game: asteroid.is_homing = True asteroid.target = self.player.position if Sound.sound_manager.sounds[Sound.sounds.homing].get_num_channels() == 0: Sound.sound_manager.play_sound(Sound.sounds.homing, 20) def add_sounds(self): Sound.sound_manager.add_sound(pygame.mixer.Sound("Sounds/start.wav")) Sound.sound_manager.add_sound(pygame.mixer.Sound("Sounds/shoot.wav")) Sound.sound_manager.add_sound(pygame.mixer.Sound("Sounds/asteroid_hurt.wav")) Sound.sound_manager.add_sound(pygame.mixer.Sound("Sounds/item_pickup.wav")) Sound.sound_manager.add_sound(pygame.mixer.Sound("Sounds/ship_thrusters2.wav")) Sound.sound_manager.add_sound(pygame.mixer.Sound("Sounds/homing_alarm.wav")) Sound.sound_manager.add_sound(pygame.mixer.Sound("Sounds/player_hurt.wav")) def draw_ui(self,screen): self.gui.draw_text("Level: " + str(self.level), (10,10), screen) #show the level top right self.gui.draw_text( "Asteroids Remaining: " + str(len(self.get_asteroids_remaining())) , (10, 40), screen) #show remaining asteroids - top right self.gui.draw_text("Score: " + str(self.score), (10, 70), screen) self.gui.draw_text("Lives: " + str(self.player.lives), (10, 100), screen) #show players health - top left