Skip to content

Commit

Permalink
Platform collision refactored according to the touchdown var.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbejcek committed Mar 6, 2024
1 parent 80dc87d commit 88cdc61
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
5 changes: 1 addition & 4 deletions Images/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@
platform_1 = pygame.image.load(img_paths['platform_1'])

def draw_background():

screen.blit(bg_img, (0, 0))
# screen.blit(platform_0,(0,900))
# screen.blit(platform_0,(835,900))
screen.blit(bg_img, (0, 0))
7 changes: 4 additions & 3 deletions Main/collisions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import pygame

# def platform_collision(screen,character_pos, character_img):
def platform_collision():
platform_list = []
lamp = pygame.Rect(1002, 748, 185, 1)
floor = pygame.Rect(0, 900, 1792, 1)
lamp = pygame.Rect(1002, 748, 185, 2)
floor = pygame.Rect(0, 900, 1792, 2)
roof = pygame.Rect(1350, 675, 200, 2)

platform_list.append(lamp)
platform_list.append(floor)
platform_list.append(roof)

return platform_list

1 change: 1 addition & 0 deletions Main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def run(self,test_case=False, max_iterations=50):
update_hitbox = self.player.update_hitbox(self.player.img_pos[0],self.player.img_pos[1])
self.hitbox = pygame.draw.rect(self.screen, (255, 0, 0), update_hitbox, 2)


"""Method that checks for vertical collision and adjusts the character position accordingly"""
platform = platform_collision()
self.player.check_vertical_collision(platform, self.hitbox)
Expand Down
48 changes: 38 additions & 10 deletions Main/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, x,y):

"""Creating a custom hitbox to better control the character collisions"""
def update_hitbox(self,x,y):
self.hitbox = (x + 53, y + 70,55,90)
self.hitbox = (x + 53, y + 71,55,90)
return self.hitbox

def draw_character(self):
Expand All @@ -82,6 +82,8 @@ def player_movement(self):
"""Y axis position"""
self.img_pos[1] += (self.movement_y[1] - self.movement_y[0]) * self.y_velocity



"""Main player animation loop"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
Expand All @@ -92,6 +94,7 @@ def player_movement(self):
if self.touchdown == True:
if event.type == pygame.KEYDOWN and not any([self.bow, self.attack]):


"""X axis"""
if event.key == pygame.K_a:
self.movement_x[0] = True
Expand Down Expand Up @@ -180,21 +183,46 @@ def player_movement(self):
The main movement of the character's apparatus is changing according to this variable
"""
def check_vertical_collision(self, platforms, hitbox):
"""
self.touchdown is set to False at the beginning of the method.
If a collision is detected, it is set to True. If no collision occurs, it remains False.
Method is being updated in every frame of the loop to ensure proper collision functionality.
"""
self.touchdown = False
for platform in platforms:
if hitbox.colliderect(platform) and self.jump == False:
pygame.draw.rect(self.screen, (255, 0, 0,), platform)

self.img_pos[1] = platform.top - (self.image.get_height())
if hitbox.colliderect(platform):
if self.jump == False:
self.movement_y[1] = False
self.img_pos[1] = platform.top - self.image.get_height()
self.peak = False

self.touchdown = True
self.peak = False
self.action, self.action_divider = 'Idle', 0
pygame.draw.rect(self.screen, (255, 0, 0,), platform)

self.action, self.action_divider = 'Idle', 0
pygame.draw.rect(self.screen, (100, 100, 100,), platform)

if pygame.key.get_pressed()[pygame.K_a] or pygame.key.get_pressed()[pygame.K_d]:
self.action, self.action_divider = 'Running', 1

if pygame.key.get_pressed()[pygame.K_a] and pygame.key.get_pressed()[pygame.K_d]:
self.action, self.action_divider = 'Idle', 0
self.touchdown = True
break

if not self.touchdown:
"""
If jump animation is not detected, character will now perform a 'Landing' animation,
as he is falling from the edge of the collision platform towards the ground.
"""
if not self.jump:
self.movement_y[1] = True
self.peak = True
self.action, self.action_divider = 'Landing', 6






def update_animation(self):
Expand Down Expand Up @@ -229,7 +257,7 @@ def update_animation(self):
self.peak = True
self.jump = False

"""Once player reaches the 'self.peak', descending sequence is initialized"""
"""Once player reaches the 'self.peak', descending sequence is initialized, increasing the falling speed"""
if self.peak:
self.y_velocity += .5
self.action, self.action_divider = 'Landing', 6
Expand Down Expand Up @@ -259,12 +287,12 @@ def update_animation(self):
self.attack_animation = 'Attack_1'

"""Helper conditions that allow fluent movement if any of the direction keys is pressed while performing the attack"""
if pygame.key.get_pressed()[pygame.K_d] and not pygame.key.get_pressed()[pygame.K_a]:
if pygame.key.get_pressed()[pygame.K_d] and not pygame.key.get_pressed()[pygame.K_q]:
self.movement_x[1] = True
self.motion_right = True
self.flip = False

if pygame.key.get_pressed()[pygame.K_a] and not pygame.key.get_pressed()[pygame.K_d]:
if pygame.key.get_pressed()[pygame.K_a] and not pygame.key.get_pressed()[pygame.K_q]:
self.movement_x[0] = True
self.motion_left = True
self.flip = True
Expand Down

0 comments on commit 88cdc61

Please sign in to comment.