-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_test.py
93 lines (81 loc) · 2.74 KB
/
player_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import unittest
import pygame
from game.sprites.player import Player
from game.level import Level
from game.sprites.background import BackGround
MAP_1 = [
[0, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 1],
]
class TestPlayer(unittest.TestCase):
def setUp(self):
self.level = Level(
player=Player(20, 10),
game_map=MAP_1,
background=BackGround()
)
def test_can_move_left(self):
pos1 = self.level.player.rect.x
self.level.player._move_left(self.level.ground)
pos2 = self.level.player.rect.x
self.assertTrue(pos1 > pos2)
def test_can_move_right(self):
pos1 = self.level.player.rect.x
self.level.player._move_right(self.level.ground)
pos2 = self.level.player.rect.x
self.assertTrue(pos1 < pos2)
def test_can_fall(self):
pos1 = self.level.player.rect.y
for _ in range(100):
self.level.player.update_position(
self.level.ground,
self.level.target,
)
pos2 = self.level.player.rect.y
self.assertTrue(pos1 < pos2)
def test_can_jump(self):
for _ in range(100):
self.level.player.update_position(
self.level.ground,
self.level.target,
)
pos1 = self.level.player.rect.y
self.level.player._jump()
self.level.player.update_position(
self.level.ground,
self.level.target,
)
pos2 = self.level.player.rect.y
self.assertTrue(pos1 > pos2)
def test_controls_left(self):
pos1 = self.level.player.rect.x
event = pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_LEFT})
self.level.player.controls(event)
self.level.player.update_position(
self.level.ground,
self.level.target,
)
pos2 = self.level.player.rect.x
self.assertTrue(pos1 > pos2)
def test_controls_right(self):
pos1 = self.level.player.rect.x
event = pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_RIGHT})
self.level.player.controls(event)
self.level.player.update_position(
self.level.ground,
self.level.target,
)
pos2 = self.level.player.rect.x
self.assertTrue(pos1 < pos2)
def test_cannot_jump_falling(self):
self.assertEqual(self.level.player.can_jump, False)
def test_falling_to_ground_resets_jump(self):
# fall to ground first
for _ in range(100):
self.level.player.update_position(
self.level.ground,
self.level.target,
)
self.assertEqual(self.level.player.can_jump, True)