From 441dce7e723c439d7cdc9517fe4c64460c7eedee Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Tue, 22 Jan 2013 22:53:03 -0430 Subject: [PATCH] Npc's escape from the player. --- actor.py | 11 ++++++++++- ingame.py | 36 ++++++++++++++++++++++++------------ math_utils.py | 4 ++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/actor.py b/actor.py index 95485af..55f31e2 100644 --- a/actor.py +++ b/actor.py @@ -253,6 +253,7 @@ class OmnidirectionalActor(BaseActor): self.frame_rate = frame_rate self.moving = False self.rotate_on_constraint = False + self.scared = False self.friction = 0.90 @@ -260,7 +261,7 @@ class OmnidirectionalActor(BaseActor): self.constraint_min_y = 0 self.constraint_max_x = 1024 self.constraint_max_y = 768 - + self.acc_fract_x = ((0.4 * float(pygame.display.Info().current_w)) / 1024.0) self.acc_fract_y = ((0.4 * float(pygame.display.Info().current_h)) / 768.0) @@ -269,6 +270,10 @@ class OmnidirectionalActor(BaseActor): self.scared_frames = [] self.current_frame = 0 + def set_acceleration_fraction(self, fraction): + self.acc_fract_x = ((fraction * float(pygame.display.Info().current_w)) / 1024.0) + self.acc_fract_y = ((fraction * float(pygame.display.Info().current_h)) / 768.0) + def is_moving(self): return self.moving @@ -387,10 +392,14 @@ class OmnidirectionalActor(BaseActor): if self.current_frame >= len(self.moving_frames): self.current_frame = len(self.moving_frames) - 1 + def is_scared(self): + return self.scared + def toggle_scared(self): aux = self.moving_frames self.moving_frames = self.scared_frames self.scared_frames = aux + self.scared = not self.scared def draw(self, canvas): if self.image is not None: diff --git a/ingame.py b/ingame.py index 96d9103..87a6124 100644 --- a/ingame.py +++ b/ingame.py @@ -163,6 +163,8 @@ class InGameState(BaseState): self.ret_rect = self.reticule.get_rect() self.ret_rect.center = self.screen_center + self.scare_dist = (250 * pygame.display.Info().current_h) / 768 + def recenter_view(self): # Reset the view. self.bckg_x = 0 @@ -215,6 +217,7 @@ class InGameState(BaseState): huggable.set_fps(5) huggable.set_angle(math_utils.ang_2_radians(float(random.randrange(-180, 180, 1)))) huggable.set_velocity([0, 0]) + huggable.set_acceleration_fraction(0.35) huggable.set_position(position) huggable.set_constraints(self.constraints) huggable.set_rotate_on_constraint(True) @@ -380,11 +383,6 @@ class InGameState(BaseState): if not self.done: if self.cursor_x != self.screen_center[0] or self.cursor_y != self.screen_center[1]: - #p_pos = self.player.get_position() - #(dist_x, dist_y) = (self.screen_center[0] - p_pos[0], self.screen_center[1] - p_pos[1]) - #self.cursor_x += dist_x - #self.cursor_y += dist_y - #vec_2 = (float(self.cursor_x) - float(p_pos[0]), float(self.cursor_y) - float(p_pos[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)) @@ -411,17 +409,34 @@ class InGameState(BaseState): npc.set_angle(math_utils.ang_2_radians(float(random.randrange(-180, 180, 1)))) self.change_angle = 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()) player.PLAYERS[1].inc_score_by_one() - # TODO: Detect collisions with player here. - # Remove invisible npcs. - removal = set() - for npc in self.npcs: + # If the npc exploded this turn, remove it. if not npc.is_visible(): removal.add(npc) @@ -490,9 +505,6 @@ class InGameState(BaseState): #canvas.fill(self.background_color) self.game_area.blit(self.background, (0, 0)) - for spawner in self.spawners: - spawner.draw(self.game_area) - # Blit everything to the bacground. # Sort npcs by Y coordinate and draw. # The idea is to draw npcs near the bottom edge of the screen last. diff --git a/math_utils.py b/math_utils.py index e38a4cf..7118fa5 100644 --- a/math_utils.py +++ b/math_utils.py @@ -26,3 +26,7 @@ def ang_2_radians(ang): def radians_2_ang(rad): return (rad * 180.0) / PI + +def distance_2D(point1, point2): + return math.sqrt(((point1[0] - point2[0]) * (point1[0] - point2[0])) + + ((point1[1] - point2[1]) * (point1[1] - point2[1])))