def create_new_enemy(self, position): play_img = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Idle_front.png') enemy = actor.OmnidirectionalActor(0, play_img, "Random Enemy", True) enemy.set_fps(5) enemy.set_velocity([0, 0]) enemy.set_acceleration_fraction(0.5) enemy.set_position(position) enemy.set_constraints(self.constraints) enemy.set_rotate_on_constraint(True) enemy.move() # Create a male enemy. image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Idle_side.png') enemy.add_idle_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Idle_front.png') enemy.add_idle_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Idle_side_flipped.png') enemy.add_idle_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Idle_back.png') enemy.add_idle_frame(image) # Add moving frames. image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_side_1.png') enemy.add_moving_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_side_2.png') enemy.add_moving_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_front_1.png') enemy.add_moving_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_front_2.png') enemy.add_moving_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_side_1_flipped.png') enemy.add_moving_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_side_2_flipped.png') enemy.add_moving_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_back_1.png') enemy.add_moving_frame(image) image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Walking_back_2.png') enemy.add_moving_frame(image) # Move in the direction of the player. p_pos = self.player.get_position() vec_2 = (float(p_pos[0] - position[0]), float(p_pos[1] - position[1])) vec_2 = math_utils.normalize_vector_2D(vec_2) enemy.set_angle(math_utils.angle_vectors_2D(self.vec_1, vec_2)) self.enemies.add(enemy) audio.cached_audio_manager.play_sound('sfx/amiguito.wav')
def update(self): if self.next_transition != VALID_STATES['QUIT']: if self.next_transition != VALID_STATES['STAY']: self.next_transition = VALID_STATES['STAY'] self.player.reset_then() self.then = pygame.time.get_ticks() # Start the huggable creation timer. pygame.time.set_timer(pygame.USEREVENT + 1, 1000) # Start the huggable angle change event. pygame.time.set_timer(pygame.USEREVENT + 2, 2000) # Start the enemy creation timer. pygame.time.set_timer(pygame.USEREVENT + 3, 3000) mixer.music.load('music/8-Bit_Easter/01-DANJYON KIMURA-TELEPORTER.mp3') mixer.music.play(-1) if self.cancel and self.time_left > 0: # If the player pressed escape, force a timeout. self.time_left = 0 now = pygame.time.get_ticks() delta_t = now - self.then if delta_t >= 1000: self.time_left -= delta_t // 1000 self.then = now if self.time_left <= 0 and player.PLAYERS[1].is_alive(): player.PLAYERS[1].kill() self.done = True self.create_explosion(self.player.get_position()) audio.cached_audio_manager.play_sound('sfx/Explo_4.wav') if not self.done: if self.cursor_x != self.screen_center[0] or self.cursor_y != self.screen_center[1]: vec_2 = (float(self.cursor_x) - float(self.screen_center[0]), float(self.cursor_y) - float(self.screen_center[1])) vec_2 = math_utils.normalize_vector_2D(vec_2) self.player.set_angle(math_utils.angle_vectors_2D(self.vec_1, vec_2)) self.player.update() self.recenter_view() # Create new huggables. if self.create_huggable: for spawner in self.spawners: if len(self.npcs) >= self.max_npc: # If we reached the maximum number of npcs, cancel the timer and ignore the rest of the spawners. pygame.time.set_timer(pygame.USEREVENT + 1, 0) break else: chance = random.randrange(100) if chance < 20: self.create_new_huggable(spawner.get_position()) self.create_explosion(spawner.get_position()) self.create_huggable = False if self.change_angle: for npc in self.npcs: npc.set_angle(math_utils.ang_2_radians(float(random.randrange(-180, 180, 1)))) self.change_angle = False if self.create_enemy: for spawner in self.spawners: if len(self.enemies) >= self.max_npc: # If we reached the maximum number of npcs, cancel the timer and ignore the rest of the spawners. pygame.time.set_timer(pygame.USEREVENT + 3, 0) break else: # First check the distance between the player and the spawner to avoid enemies spawning on top of # the player. if math_utils.distance_2D(self.player.get_position(), spawner.get_position()) > 1.5 * float(self.scare_dist): chance = random.randrange(0, 50) if chance < self.wave: self.create_new_enemy(spawner.get_position()) self.create_explosion(spawner.get_position()) self.create_enemy = False removal = set() for npc in self.npcs: # Check if the npc must run away from the player. if math_utils.distance_2D(self.player.get_position(), npc.get_position()) < self.scare_dist: if not npc.is_scared(): npc.toggle_scared() npc.set_velocity([0, 0]) # Move in the opposite direction of the player. n_pos = npc.get_position() p_pos = self.player.get_position() vec_2 = (float(n_pos[0] - p_pos[0]), float(n_pos[1] - p_pos[1])) vec_2 = math_utils.normalize_vector_2D(vec_2) npc.set_angle(math_utils.angle_vectors_2D(self.vec_1, vec_2)) else: if npc.is_scared(): npc.toggle_scared() npc.update() # Detect collisions with the player. if self.player.is_moving() and npc.test_collision_with_actor(self.player): npc.make_invisible() self.create_explosion(npc.get_position()) audio.cached_audio_manager.play_sound('sfx/Explo_4.wav') player.PLAYERS[1].inc_score_by_one() self.time_left += 1 if player.PLAYERS[1].get_score() % 25 == 0: self.wave += 1 audio.cached_audio_manager.play_sound('sfx/new_stage_1.wav') # If the npc exploded this turn, remove it. if not npc.is_visible(): removal.add(npc) if len(removal) > 0 and len(self.npcs) >= self.max_npc: # If npcs dissapeared this cycle restart the timer. pygame.time.set_timer(pygame.USEREVENT + 1, 1000) self.npcs.difference_update(removal) removal = set() for enemy in self.enemies: enemy.update() if enemy.test_collision_with_actor(self.player): # If the player lost, force a timeout. self.time_left = 0 if enemy.get_position()[0] <= self.constraints[0] or enemy.get_position()[0] >= self.constraints[1]: enemy.make_invisible() self.create_explosion(enemy.get_position()) removal.add(enemy) audio.cached_audio_manager.play_sound('sfx/Explo_2.wav') elif enemy.get_position()[1] <= self.constraints[2] or enemy.get_position()[1] >= self.constraints[3]: enemy.make_invisible() self.create_explosion(enemy.get_position()) removal.add(enemy) audio.cached_audio_manager.play_sound('sfx/Explo_2.wav') if len(removal) > 0 and len(self.enemies) >= self.max_npc: # If npcs dissapeared this cycle restart the timer. pygame.time.set_timer(pygame.USEREVENT + 1, 3000) self.enemies.difference_update(removal) elif self.time_left < -3: # Reset everything. self.time_left = 190 self.wave = 0 self.user_click = False self.done = False # Reset the player. self.player.set_angle(90) self.player.set_velocity([0, 0]) self.player.stop() self.player.set_position(self.game_area_center) # TODO: Destroy all NPC's. self.explosions.clear() self.npcs.clear() self.enemies.clear() player.PLAYERS[1].revive() database.cursor.execute('SELECT * FROM score ORDER BY score ASC') row = database.cursor.fetchone() if player.PLAYERS[1].get_score() > row[2]: self.next_transition = VALID_STATES['SCORE'] else: self.next_transition = VALID_STATES['MENU'] # Stop the huggable creation timer. pygame.time.set_timer(pygame.USEREVENT + 1, 0) pygame.time.set_timer(pygame.USEREVENT + 2, 0) pygame.time.set_timer(pygame.USEREVENT + 3, 0) self.cancel = False # Remove finished explosions removal = set() for explosion in self.explosions: if explosion.get_current_frame() == 6: removal.add(explosion) self.explosions.difference_update(removal) self.score_text = self.font.render("Puntos: " + str(player.PLAYERS[1].get_score()), True, (0, 0, 0)) if self.time_left > 30: self.time_text = self.font.render("Tiempo: " + str(self.time_left), True, (0, 0, 0)) else: self.time_text = self.font.render("Tiempo: " + str(max(self.time_left, 0)), True, (255, 0, 0)) self.wave_text = self.font.render("Oleada: " + str(self.wave), True, (0, 0, 0)) self.cursor_x = self.screen_center[0] self.cursor_y = self.screen_center[1] return self.next_transition