Skip to content

Commit

Permalink
Fixed enemy collision
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbejcek committed Mar 20, 2024
1 parent 806ba94 commit aea8061
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
6 changes: 2 additions & 4 deletions Main/collisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 17 additions & 11 deletions Main/enemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions Main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != []:
Expand Down
2 changes: 2 additions & 0 deletions Tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,5 @@ def assertAlmostEqualsInt(self, first, second, delta):
else:
self.fail("Delta is not great enough")

class TestEnemyCharacter(unittest.TestCase):
pass

0 comments on commit aea8061

Please sign in to comment.