Skip to content

Commit

Permalink
Y axis collision update
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbejcek committed Mar 4, 2024
1 parent 7812d27 commit eb3c1e4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
Binary file modified Images/Background/temp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 18 additions & 9 deletions Main/collisions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import pygame

def platform_collision(character_pos, character_img):
floor = pygame.Rect(0, 892, 1792, 100)
floor_platform = floor.top
def platform_collision(screen,character_pos, character_img):
platform_list = []

lamp = pygame.Rect(1002, 700, 185, 10)
platform_list.append(lamp)

floor = pygame.Rect(0, 892, 1792, 10)
platform_list.append(floor)

character_rect = character_img.get_rect(topleft=character_pos)

# pygame.draw.rect(screen, (0, 0, 0,), floor)
# if character_rect.colliderect(floor):
# pygame.draw.rect(screen, (255, 0, 0,), floor)
# else:
# pygame.draw.rect(screen, (0, 0, 0,), floor)
return floor_platform
for collide_point in platform_list:
if character_rect.colliderect(collide_point):
if collide_point == floor:
pygame.draw.rect(screen, (255, 0, 0,), floor)
elif collide_point == lamp:
pygame.draw.rect(screen, (255, 0, 0,), lamp)
else:
pygame.draw.rect(screen, (0, 0, 0,), floor)
pygame.draw.rect(screen, (0, 0, 0,), lamp)
return platform_list

11 changes: 8 additions & 3 deletions Main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
pygame.init()
pygame.display.set_caption("FlashJump")
self.clock = pygame.time.Clock()
self.player = PlayerCharacter(600,600)
self.player = PlayerCharacter(600,300)
self.screen = self.player.screen

def run(self,test_case=False, max_iterations=50):
Expand All @@ -22,9 +22,14 @@ def run(self,test_case=False, max_iterations=50):
"""Main images function"""
draw_background()

print(self.player.touchdown)
"""Test character square"""
character_rect = self.player.image.get_rect(topleft=self.player.img_pos)
pygame.draw.rect(self.screen, (255, 0, 0), character_rect, 2)

"""Method that checks for collision and adjusts the character position accordingly"""
floor_platform = platform_collision(self.player.img_pos, self.player.image)
self.player.check_collision(floor_platform)
platform = platform_collision(self.screen,self.player.img_pos, self.player.image)
self.player.check_collision(platform)

"""Controls the movement of the player character"""
self.player.player_movement()
Expand Down
35 changes: 20 additions & 15 deletions Main/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, x,y):
self.action_divider = 0

"""Initial image object"""
self.image = pygame.Surface((128, 128))
self.image = pygame.Surface((0, 0))

"""'self.frame_index' serves to access the specific image as an index in a 'animation_list' received from 'animate_character' function."""
self.frame_index = 0
Expand Down Expand Up @@ -56,8 +56,6 @@ def __init__(self, x,y):
self.arrow_quiver = []
self.arrow_duration = 3000



"""
Variables that help control the movement of the character. When user presses 'Key_A' to run left and right after 'Key_D' to run right,
character will stop and enter 'Idle' animation, until one of the keys is released.
Expand Down Expand Up @@ -173,27 +171,34 @@ def player_movement(self):
Whenever player is airborne, 'self.touchdown' is set to False.
The main movement of the character's apparatus is changing according to this variable
"""
def check_collision(self, platform):
def check_collision(self, platform_list):

"""Conditions that set X axis boundaries of the edge of the screen"""
if self.img_pos[0] <= -30:
self.movement_x[0] = False
if self.img_pos[0] >= 1670:
self.movement_x[1] = False

"""Conditions that set Y axis"""
if self.img_pos[1] >= platform - self.image.get_height():
self.img_pos[1] = platform - self.image.get_height()
self.peak = False
self.touchdown = True
self.action, self.action_divider = 'Idle', 0
for platform in platform_list:
self.character_rect = self.image.get_rect(topleft=self.img_pos)


if pygame.key.get_pressed()[pygame.K_a] or pygame.key.get_pressed()[pygame.K_d]:
self.action, self.action_divider = 'Running', 1
"""Conditions that set Y axis"""
if self.character_rect.colliderect(platform) and self.jump == False:

if pygame.key.get_pressed()[pygame.K_a] and pygame.key.get_pressed()[pygame.K_d]:
self.img_pos[1] = platform.top - self.image.get_height()
self.peak = False
self.touchdown = True
self.action, self.action_divider = 'Idle', 0
else:
self.touchdown = False


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
else:
self.touchdown = False


def update_animation(self):
Expand Down

0 comments on commit eb3c1e4

Please sign in to comment.