From aea8061d24b4d34439c96ae2cfb531472dcd73a2 Mon Sep 17 00:00:00 2001 From: danielbejcek Date: Wed, 20 Mar 2024 11:41:22 +0100 Subject: [PATCH] Fixed enemy collision --- Main/collisions.py | 6 ++---- Main/enemy.py | 28 +++++++++++++++++----------- Main/main.py | 20 +++++++++++--------- Tests/test_main.py | 2 ++ 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Main/collisions.py b/Main/collisions.py index d4af1e4..f2df407 100644 --- a/Main/collisions.py +++ b/Main/collisions.py @@ -127,15 +127,13 @@ def check_horizontal_collision(self, enemy_object_list): hitbox1 = pygame.Rect(enemy1.update_enemy_hitbox(enemy1.enemy_img_pos[0], enemy1.enemy_img_pos[1])) hitbox2 = pygame.Rect(enemy2.update_enemy_hitbox(enemy2.enemy_img_pos[0], enemy2.enemy_img_pos[1])) + pygame.draw.rect(self.screen, (255, 0, 0), hitbox1, 1) + pygame.draw.rect(self.screen, (0, 0, 0), hitbox2, 1) if hitbox2.colliderect(hitbox1): enemy_collision = True - enemy2.enemy_movement_x = [False,False] - enemy1.enemy_action, enemy1.enemy_action_divider = 'Running', 1 - enemy2.enemy_action, enemy2.enemy_action_divider = 'Idle', 0 - return enemy_collision diff --git a/Main/enemy.py b/Main/enemy.py index 74df530..b642384 100644 --- a/Main/enemy.py +++ b/Main/enemy.py @@ -7,21 +7,25 @@ class EnemyCharacter(PlayerCharacter): def __init__(self): super().__init__() self.enemy_action, self.enemy_action_divider = 'Idle', 0 + self.enemy_image = pygame.Surface((0, 0)) """Enemy attack variables""" self.enemy_attack = False self.enemy_attack_start_time = None self.enemy_attack_duration = 500 + """Enemy movement variable""" self.enemy_touchdown = False self.enemy_img_pos = [50,600] self.enemy_movement_y = [False, True] self.enemy_movement_x = [False, False] self.enemy_x_velocity = 3 self.enemy_flip = False - self.enemy_image = pygame.Surface((0,0)) + + """Enemy attributes""" self.enemy_hitpoints = 100 + """Multiple enemies configuration""" self.enemy_list = [] self.enemy_spawn_rate = 2000 self.enemy_current_spawn = pygame.time.get_ticks() @@ -36,46 +40,48 @@ def update_enemy_hitbox(self,x,y): return self.hitbox def enemy_movement(self,player_pos, enemy_collision): + """Range of player's hitbox that allows the enemies to attack once within this range""" self.enemy_horizontal_range = [player_pos[0] - 90, player_pos[0] + 70] """X axis position""" self.enemy_img_pos[0] += (self.enemy_movement_x[1] - self.enemy_movement_x[0]) * self.enemy_x_velocity - if not enemy_collision: + if self.enemy_movement_x[0] or self.enemy_movement_x[1]: self.enemy_action, self.action_divider = "Running", 1 else: self.enemy_action, self.action_divider = "Idle", 0 - """Enemy left side of the player movement""" + + """If enemy is not within the player's left 'horizontal_range', start to move towards him""" if self.enemy_img_pos[0] <= self.enemy_horizontal_range[0]: self.enemy_flip = False self.enemy_movement_x[1] = True - """Condition that checks if the enemy is within the 'LEFT' side range of the player""" + """Condition that checks if the enemy is within the left 'horizontal_range' of the player""" if self.enemy_img_pos[0] > self.enemy_horizontal_range[0] and self.enemy_img_pos[0] <= player_pos[0]: self.enemy_movement_x[1] = False - self.enemy_action, self.action_divider = "Idle", 0 + """Condition that checks if the player's vertical axis matches the one of the enemy""" if player_pos[1] >= self.enemy_img_pos[1]: pass - # print("LEFT") + # Placeholder for enemy attack animation - """Enemy right side of the player movement""" + """If enemy is not within the player's right 'horizontal range', start to move towards him""" if self.enemy_img_pos[0] >= self.enemy_horizontal_range[1]: self.enemy_flip = True self.enemy_movement_x[0] = True - """Condition that checks if the enemy is within the 'RIGHT' side range of the player""" + """Condition that checks if the enemy is within the right 'horizontal range' of the player""" if self.enemy_img_pos[0] < self.enemy_horizontal_range[1] and self.enemy_img_pos[0] >= player_pos[0]: self.enemy_movement_x[0] = False - self.enemy_action, self.action_divider = "Idle", 0 + """Condition that checks if the player's vertical axis matches the one of the enemy""" if player_pos[1] >= self.enemy_img_pos[1]: pass - # print("RIGHT") + # Placeholder for enemy attack animation """Y axis position""" @@ -116,7 +122,7 @@ def add_enemy(self,current_time): self.enemy_list.append(self.enemy) # print(self.enemy_list) if current_time - self.enemy_current_spawn > self.enemy_spawn_rate: - if len(self.enemy_list) <= 1: + if len(self.enemy_list) <= 2: self.enemy_list.append(self.enemy) self.enemy_current_spawn = current_time # print(self.enemy_list) diff --git a/Main/main.py b/Main/main.py index 934a5c2..940a06b 100644 --- a/Main/main.py +++ b/Main/main.py @@ -50,20 +50,22 @@ def run(self,test_case=False, max_iterations=50): current_time = pygame.time.get_ticks() self.enemy.add_enemy(current_time) - for index, enemyPlayer in enumerate(self.enemy_list): + for index, enemy_player in enumerate(self.enemy_list): - enemy_hitbox = enemyPlayer.update_enemy_hitbox(enemyPlayer.enemy_img_pos[0],enemyPlayer.enemy_img_pos[1]) + enemy_hitbox = enemy_player.update_enemy_hitbox(enemy_player.enemy_img_pos[0],enemy_player.enemy_img_pos[1]) self.enemyHitbox = pygame.Rect(enemy_hitbox) """Draw hitbox""" - pygame.draw.rect(self.screen, (255, 0, 0), enemy_hitbox, 1) + # pygame.draw.rect(self.screen, (255, 0, 0), enemy_hitbox, 1) + self.collide.check_horizontal_collision(self.enemy_list) + self.collide.check_vertical_collision(self.enemyHitbox, enemy_player, type='enemy') + + enemy_player.draw_enemy() + enemy_player.enemy_movement(self.player.img_pos,self.collide.check_horizontal_collision(self.enemy_list)) + enemy_player.update_enemy_animation() + + - enemyPlayer.draw_enemy() - enemyPlayer.enemy_movement(self.player.img_pos,self.collide.check_horizontal_collision(self.enemy_list)) - enemyPlayer.update_enemy_animation() - self.collide.check_horizontal_collision(self.enemy_list) - self.collide.check_vertical_collision(self.enemyHitbox, enemyPlayer, type='enemy') - # print(self.enemy.enemy_collide) """Arrow object animation, method is called only when 'arrow_quiver' list is not empty""" if self.player.arrow_quiver != []: diff --git a/Tests/test_main.py b/Tests/test_main.py index 9ae9bf3..9d1c25d 100644 --- a/Tests/test_main.py +++ b/Tests/test_main.py @@ -339,3 +339,5 @@ def assertAlmostEqualsInt(self, first, second, delta): else: self.fail("Delta is not great enough") +class TestEnemyCharacter(unittest.TestCase): + pass \ No newline at end of file