def kill(self): groups = self.groups() # Store the groups on which the asteroid was super().kill() # Exclude the asteroid from the groups Sounds.big_explosion_sound() spread_angle = gauss(0, 45) spread_speed = gauss(0, 1) vel_dir1 = (cos(radians(self.direction+spread_angle)), sin(radians(self.direction+spread_angle))) vel_dir2 = (cos(radians(self.direction-spread_angle)), sin(radians(self.direction-spread_angle))) speed1 = [] speed1.append(self.speed[0] + spread_speed*vel_dir1[0]) speed1.append(self.speed[1] + spread_speed*vel_dir1[1]) speed2 = [] speed2.append(self.speed[0] - spread_speed*vel_dir2[0]) speed2.append(self.speed[1] - spread_speed*vel_dir2[1]) # Add two smaller asteroids to the groups new_asteroids = [MediumAsteroid(self.screen, self.x, self.y, speed1, vel_dir1), MediumAsteroid(self.screen, self.x, self.y, speed2, vel_dir2)] for group in groups: group.add(new_asteroids)
def __init__(self, display): super().__init__(display) # Title screen music Sounds.play_title_song() # Calculate position of game title and buttons self.display_width_factor = display.get_width() / 800 self.display_height_factor = display.get_height() / 640 self.GAME_TITLE_CENTER = (self.display_width_factor * 400, self.display_height_factor * 200) self.PLAY_BUTTON_CENTER = (self.GAME_TITLE_CENTER[0], self.GAME_TITLE_CENTER[1] + self.display_height_factor * 150) self.SCORE_BUTTON_CENTER = (self.PLAY_BUTTON_CENTER[0], self.PLAY_BUTTON_CENTER[1] + 50) # Create game title and buttons self.game_title = TextBox(self.display, self.GAME_TITLE_CENTER, self.GAME_TITLE_FONT_SIZE, "METEORITOS") self.play_button = TextButton(self.display, self.PLAY_BUTTON_CENTER, self.BUTTON_FONT_SIZE, "PLAY GAME") self.score_button = TextButton(self.display, self.SCORE_BUTTON_CENTER, self.BUTTON_FONT_SIZE, "HIGH SCORES") # Create background asteroids self.asteroids = pg.sprite.Group() for i in range(12): if i % 3 == 0: self.asteroids.add(SmallAsteroid(display)) elif i % 3 == 1: self.asteroids.add(MediumAsteroid(display)) else: self.asteroids.add(BigAsteroid(display))
def __init__(self, display): super().__init__(display) # Setting sounds self.sounds = Sounds() Sounds.stop_title_song() # Factor for decreasing of the beat period self.period_factor = 3e-4 # Create a score counter to assign each player's score self.score_counter = ScoreCounter() # Create players self.player = BotPlayer(self, display.get_width() // 2, display.get_height() // 2, 1, 1) self.players = [self.player] self.visible_players = pg.sprite.Group() self.visible_players.add(self.player) # Round counter self.round = 1 # Flag for game over self.game_over = False # Create background asteroids self.asteroids = pg.sprite.Group() for i in range(12): if i % 3 == 0: self.asteroids.add(SmallAsteroid(display)) elif i % 3 == 1: self.asteroids.add(MediumAsteroid(display)) else: self.asteroids.add(BigAsteroid(display)) # Create a variable for counting time since last saucer died self.frames_since_saucer = 0 self.SAUCER_SPAWN_TIME = 2000 self.saucers = pg.sprite.Group() # Add all sprites to active sprites group self.active_sprites.add(self.visible_players) for player in self.visible_players: self.active_sprites.add(player.shot_bullets) self.active_sprites.add(self.saucers) for saucer in self.saucers: self.active_sprites.add(saucer.saucer_shot_bullets) self.active_sprites.add(self.asteroids)
def __init__(self, display): super().__init__(display) # Calculate position of game title and buttons self.display_width_factor = display.get_width() / 800 self.display_height_factor = display.get_height() / 640 self.SCREEN_TITLE_CENTER = (self.display_width_factor * 400, self.display_height_factor * 50) self.BACK_BUTTON_CENTER = (self.SCREEN_TITLE_CENTER[0] - self.display_height_factor * 250, self.SCREEN_TITLE_CENTER[1] + self.display_height_factor * 50) self.CLEAR_SCORE_BUTTON_CENTER = (self.SCREEN_TITLE_CENTER[0] + self.display_height_factor * 250, self.BACK_BUTTON_CENTER[1]) self.FIRST_SCORE_BOX_CENTER = (self.SCREEN_TITLE_CENTER[0], self.CLEAR_SCORE_BUTTON_CENTER[1] + self.display_height_factor * 50) # Create screen title and buttons self.screen_title = TextBox(self.display, self.SCREEN_TITLE_CENTER, self.SCREEN_TITLE_FONT_SIZE, "HIGH SCORES") self.back_button = TextButton(self.display, self.BACK_BUTTON_CENTER, self.BUTTON_FONT_SIZE, "BACK TO MENU") self.clear_score_button = TextButton(self.display, self.CLEAR_SCORE_BUTTON_CENTER, self.BUTTON_FONT_SIZE, "CLEAR SCORES") # Create vector of text boxes representing the scores table self.score_text_box = [] # Create score communicator path = os.path.dirname(__file__) path = os.path.join(path, os.pardir, os.pardir, 'db', 'scores_db.csv') self.score_communicator = ScoreCommunicator(path) # Create background asteroids self.asteroids = pg.sprite.Group() for i in range(12): if i % 3 == 0: self.asteroids.add(SmallAsteroid(display)) elif i % 3 == 1: self.asteroids.add(MediumAsteroid(display)) else: self.asteroids.add(BigAsteroid(display))
def update(self, event): super().update(event) # Play back beat self.sounds.theme_song() self.sounds.set_period(self.sounds.period - self.period_factor) # If esc is pressed, switch to settings screen # if event.type == pg.KEYDOWN: # if event.key == pg.K_ESCAPE: # self.switch_to_scene(SettingsScreen(self.display, self, self.active_sprites)) # Create a random saucer if not self.saucers.__nonzero__(): self.frames_since_saucer += 1 if self.frames_since_saucer >= self.SAUCER_SPAWN_TIME: i = randint(1, 2) # Saucers were removed for training purpose, because of their difficulty # if i == 1: # saucer = BigSaucer(self.display) # else: # saucer = SmallSaucer(self.display, self.round, self.player) #self.saucers.add(saucer) #self.active_sprites.add(self.saucers) #for saucer in self.saucers: # self.active_sprites.add(saucer.saucer_shot_bullets) # Whenever the first saucer appears in a round, other saucers will spawn # more often until the round is over self.frames_since_saucer = 1200 + randint(1, 5) * 100 # Todo: Create a round function to call rounds # If game is not over and all asteroids and saucers were destroyed, call a new round if not (self.asteroids.__nonzero__() or self.saucers.__nonzero__()): self.round += 1 # Reset back beat period self.sounds.set_period(Sounds.INITIAL_PERIOD) # Create asteroids for i in range(12): if i % 3 == 0: self.asteroids.add(SmallAsteroid(self.display)) elif i % 3 == 1: self.asteroids.add(MediumAsteroid(self.display)) else: self.asteroids.add(BigAsteroid(self.display)) # Add all sprites to active sprites group self.active_sprites.add(self.visible_players) for player in self.visible_players: self.active_sprites.add(player.shot_bullets) self.active_sprites.add(self.asteroids) # Respawn all visible players in their initial positions for player in self.visible_players: player.vanish() player.respawn() # Restart saucers time counting self.frames_since_saucer = 0 # Make all collisions for player in self.players: player.check_bullets_collision(self.asteroids) player.check_bullets_collision(self.saucers) for player in self.visible_players: player.check_self_collision(self.asteroids) player.check_self_collision(self.saucers) for saucer in self.saucers: player.check_self_collision(saucer.saucer_shot_bullets) # Look for respawned players and add them again to visible players for player in self.players: if player not in self.visible_players and player.is_visible: self.visible_players.add(player) self.active_sprites.add(player) # Update all active sprites but the players for sprite in self.active_sprites: if sprite.__class__.__name__ not in [ 'Player', 'UserPlayer', 'BotPlayer' ]: sprite.update() for saucer in self.saucers: self.active_sprites.add(saucer.saucer_shot_bullets) # Update players based on active sprites for player in self.players: player.update(event, self.active_sprites) self.active_sprites.add(player.shot_bullets)
def __init__(self, display): super().__init__(display) # Setting game logging self.logging = False # Dialog box to indicate logging self.display_width_factor = display.get_width() / 800 self.display_height_factor = display.get_height() / 640 self.LOG_BOX_CENTER = (self.display_width_factor * 400, self.display_height_factor * 50) self.log_box = TextBox(self.display, self.LOG_BOX_CENTER, self.LOG_BOX_FONT_SIZE, "* REC") # Setting sounds self.sounds = Sounds() Sounds.stop_title_song() # Factor for decreasing of the beat period self.period_factor = 3e-4 # Create a score counter to assign each player's score self.score_counter = ScoreCounter() # Create players self.player = UserPlayer(self, display.get_width() // 2, display.get_height() // 2, 1) self.players = [self.player] self.visible_players = pg.sprite.Group() self.visible_players.add(self.player) # Round counter self.round = 1 # Flag for game over self.game_over = False # Create background asteroids self.asteroids = pg.sprite.Group() for i in range(12): if i % 3 == 0: self.asteroids.add(SmallAsteroid(display)) elif i % 3 == 1: self.asteroids.add(MediumAsteroid(display)) else: self.asteroids.add(BigAsteroid(display)) # Create a variable for counting time since last saucer died self.frames_since_saucer = 0 self.SAUCER_SPAWN_TIME = 2000 self.saucers = pg.sprite.Group() # Add all sprites to active sprites group self.active_sprites.add(self.visible_players) for player in self.visible_players: self.active_sprites.add(player.shot_bullets) self.active_sprites.add(self.saucers) for saucer in self.saucers: self.active_sprites.add(saucer.saucer_shot_bullets) self.active_sprites.add(self.asteroids)
WHITE = (255, 255, 255) bg_color = BLACK pg.init() screen = pg.display.set_mode(SIZE) pg.display.set_caption('Asteroid Movement Testing') clock = pg.time.Clock() asteroids = pg.sprite.Group() for i in range(12): if i % 3 == 0: asteroids.add(SmallAsteroid(screen)) elif i % 3 == 1: asteroids.add(MediumAsteroid(screen)) else: asteroids.add(BigAsteroid(screen)) while True: ev = pg.event.poll() if ev.type == pg.QUIT: break elif ev.type == pg.KEYDOWN: if ev.key == pg.K_ESCAPE: break if ev.key == pg.K_SPACE: bg_color = change_color(screen) screen.fill(bg_color)