Skip to content

Commit

Permalink
Updated collision test
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbejcek committed Mar 1, 2024
1 parent 15743fb commit 933ce9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions Main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def run(self,test_case=False, max_iterations=50):
"""Main images function"""
draw_background()

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

Expand Down
30 changes: 19 additions & 11 deletions Tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def setUp(self):

"""Instance of player from PlayerCharacter class"""
self.player = PlayerCharacter(600,895)

"""Instance from main game loop"""
self.game.player = PlayerCharacter(600,895)

Expand All @@ -63,16 +64,18 @@ def setUp(self):
self.initial_pos = self.player.img_pos

"""Setting up a collision object"""
self.floor_platform = platform_collision(self.game_pos, self.player.image)
self.floor_platform = platform_collision(self.game_pos, self.game.player.image)


def tearDown(self):
pass

@patch('pygame.event.get')
def test_movement_jump(self, mock_get_event):
mock_get_event.return_value = [self.simulate_key_press(pygame.K_SPACE)]
self.game.run(True,90)
self.game.run(True,100)
self.assertLess(self.game_pos[1],self.initial_pos[1])
self.assertTrue(self.game.player.jump)
self.assertFalse(self.game.player.movement_y[1])
self.assertTrue(self.game.player.movement_y[0])

Expand Down Expand Up @@ -116,15 +119,20 @@ def test_attack_animation(self, mock_get_event):
self.assertFalse(self.player.movement_x[1])
self.assertTrue(self.game.player.attack)

@patch('pygame.event.get')
def test_collision(self, mock_jump):
mock_jump.return_value = [self.simulate_key_press(pygame.K_SPACE)]
"""Setting the number of iterations to 90 for the animation to be able to finish the jumping sequence"""
self.assertEqual(self.game_pos[1],self.floor_platform)
self.game.run(True,90)
self.assertTrue(self.game.player.jump)
self.assertLess(self.game.player.y_velocity, 8)
self.assertNotEqual(self.game_pos[1],self.floor_platform)
"""
Test how the character reacts to collision after being dropped from Y coordinate of 500.
After we process 40 iterations of the in-game loop, the character should be now leveled
with the 'self.floor_platform'
"""
def test_collision(self):
self.game_pos[1] = 500
self.assertFalse(self.game.player.touchdown)
self.assertNotEqual(self.game_pos[1], self.floor_platform)
self.game.run(True, 40)

"""Subtracting '151' from the 'self.floor_platform' to compensate for the player image size."""
self.assertEqual(self.game_pos[1], (self.floor_platform-151))
self.assertTrue(self.game.player.touchdown)

def simulate_key_press(self, key):
mock_event_key = MagicMock()
Expand Down

0 comments on commit 933ce9f

Please sign in to comment.