Skip to content

Commit

Permalink
Conditions set for enemy attack sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbejcek committed Mar 25, 2024
1 parent aea8061 commit 6e1bc61
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
12 changes: 5 additions & 7 deletions Main/collisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def check_vertical_collision(self,hitbox, character,type):
# pygame.draw.rect(self.screen, (100, 100, 100,), platform)



"""
Whenever player wants to descent and presses the 'S' key, current platform is stored in 'self.drop_platform' variable,
usable only when colliding with platform -> 'self.touchdown == True'.
Expand Down Expand Up @@ -117,8 +116,8 @@ def check_vertical_collision(self,hitbox, character,type):
Method that checks for horizontal hitbox collision between the enemy objects.
Once enemies collide between themselves a small gap will be implemented to prevent image overlapping.
"""
def check_horizontal_collision(self, enemy_object_list):
enemy_collision = False
def check_horizontal_collision(self,enemy_object_list):

for i in range(len(enemy_object_list)):
for j in range(i + 1, len(enemy_object_list)):

Expand All @@ -130,12 +129,11 @@ def check_horizontal_collision(self, enemy_object_list):
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]

return enemy_collision
enemy2.enemy_movement_x = [False,False]



def hit_register(self,player_type,attack_type):
pass
24 changes: 17 additions & 7 deletions Main/enemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self):
self.enemy_attack = False
self.enemy_attack_start_time = None
self.enemy_attack_duration = 500
self.enemy_attack_window = False

"""Enemy movement variable"""
self.enemy_touchdown = False
Expand All @@ -39,7 +40,11 @@ def update_enemy_hitbox(self,x,y):
self.hitbox = (x + 80, y + 102,30,90)
return self.hitbox

def enemy_movement(self,player_pos, enemy_collision):

def enemy_movement(self,player_pos):
"""Setting up an attack window for the enemy character that constantly checks if player is within enemy's attack range"""
self.enemy_attack_window = False

"""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]

Expand All @@ -62,9 +67,12 @@ def enemy_movement(self,player_pos, enemy_collision):
self.enemy_movement_x[1] = False


"""Condition that checks if the player's vertical axis matches the one of the enemy"""
"""
Condition that checks if the player's vertical axis matches the one of the enemy.
Once these conditions are met, enemy is set up for an attack sequence
"""
if player_pos[1] >= self.enemy_img_pos[1]:
pass
self.enemy_attack_window = True
# Placeholder for enemy attack animation


Expand All @@ -77,10 +85,12 @@ def enemy_movement(self,player_pos, enemy_collision):
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


"""Condition that checks if the player's vertical axis matches the one of the enemy"""
"""
Condition that checks if the player's vertical axis matches the one of the enemy.
Once these conditions are met, enemy is set up for an attack sequence.
"""
if player_pos[1] >= self.enemy_img_pos[1]:
pass
self.enemy_attack_window = True
# Placeholder for enemy attack animation


Expand Down Expand Up @@ -122,7 +132,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) <= 2:
if len(self.enemy_list) <= 0:
self.enemy_list.append(self.enemy)
self.enemy_current_spawn = current_time
# print(self.enemy_list)
Expand Down
8 changes: 6 additions & 2 deletions Main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,25 @@ def run(self,test_case=False, max_iterations=50):
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.enemy_movement(self.player.img_pos)
enemy_player.update_enemy_animation()






"""Arrow object animation, method is called only when 'arrow_quiver' list is not empty"""
if self.player.arrow_quiver != []:
self.player.draw_arrow()

# print(self.enemy.enemy_hitpoints)
# if self.player.attack_register:
# print(self.player.attack_register)

"""Player character image"""
self.player.draw_player()


pygame.display.update()
self.clock.tick(60)

Expand Down
7 changes: 7 additions & 0 deletions Main/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self):

"""Attack animation variables"""
self.attack = False
self.attack_register = False
self.attack_animation = 'Attack_1'
self.attack_start_time = None
self.attack_duration = 700
Expand Down Expand Up @@ -232,10 +233,16 @@ def update_player_animation(self):
self.action, self.action_divider = self.attack_animation, 4
self.image = animate_character(self.action)[self.action_divider][self.frame_index]

"""Condition that sets the time window for the attack, when it can be actually registered as a hit"""
if 300 < pygame.time.get_ticks() - self.attack_start_time < 320:
self.attack_register = True

"""Condition is triggered after the attack animation is finished"""
if pygame.time.get_ticks() - self.attack_start_time > self.attack_duration:
self.attack = False
self.attack_start_time = None
self.attack_register = False


"""Clears the pygame key input queue in case the 'Q' key remains pressed, preventing looping of the animation"""
pygame.event.clear()
Expand Down

0 comments on commit 6e1bc61

Please sign in to comment.