-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
186 lines (144 loc) · 7.09 KB
/
player.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pygame
import time
from animation import Anim
class Player:
def __init__(self, square_size, obstacles, sprites):
self.square_size = square_size
self.obstacles = obstacles
self.sprites = sprites
self.move_anim = Anim(sprites["moving"]["right"], speed=0.12)
self.death_anim = Anim(sprites["death"], speed=0.1)
self.font = pygame.font.SysFont("coopbl", 55)
self.timer = {"move": 0}
self.move_time = 0.02
# initial amount of lives
self.lives = 3
# the player will move 2 pixel every move_time sec
self.reset_move_speed = self.move_speed = 3
self.color = (211, 144, 11)
self.direction = {"current": "left",
"next": None,
"previous": None,
"right": (1, 0),
"left": (-1, 0),
"down": (0, 1),
"up": (0, -1)}
self.killed = False
self.make_death_anim = False
self.make_ghost_respawn = False
self.start_pos = [self.square_size * 9, self.square_size * 11]
self.rect = pygame.Rect(self.start_pos[0],
self.start_pos[1],
self.square_size,
self.square_size)
self.define_current_tile()
self.score = 0
def define_current_tile(self):
current_tile_x = int(self.rect.centerx / self.square_size)
current_tile_y = int(self.rect.centery / self.square_size)
self.current_tile = [current_tile_x, current_tile_y]
def user_input(self):
keys_pressed = pygame.key.get_pressed()
if ((keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]) and not (
keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d])):
self.direction["next"] = "left"
if ((keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]) and not (
keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a])):
self.direction["next"] = "right"
if ((keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]) and not (
keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w])):
self.direction["next"] = "down"
if ((keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]) and not (
keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s])):
self.direction["next"] = "up"
def change_direction(self, surface):
if self.direction["next"] is not None:
next_rect = self.rect.copy()
next_rect.x += self.move_speed * self.direction[self.direction["next"]][0]
next_rect.y += self.move_speed * self.direction[self.direction["next"]][1]
# for obstacle in self.obstacles:
if next_rect.right > 0 and next_rect.left < surface.get_width():
if next_rect.collidelist(self.obstacles) == -1:
self.direction["current"] = self.direction["next"]
self.direction["previous"] = self.direction["current"]
self.direction["next"] = None
def move(self, surface):
self.user_input()
if time.time() > self.timer["move"]:
self.timer["move"] = time.time() + self.move_time
self.define_current_tile()
self.change_direction(surface)
if self.direction["current"] is not None:
self.rect.x += self.move_speed * self.direction[self.direction["current"]][0]
self.rect.y += self.move_speed * self.direction[self.direction["current"]][1]
# if the player is outside the screen at the right
if self.rect.x > surface.get_width():
self.rect.x = 0 - self.square_size
# if the player is outside the screen at the left
if self.rect.x < 0 - self.square_size:
self.rect.x = surface.get_width()
# if the player HAS collide with an obstacle
collide_index = self.rect.collidelist(self.obstacles)
if collide_index != -1:
if self.direction["current"] == "up":
self.rect.y = self.obstacles[collide_index].bottom
elif self.direction["current"] == "down":
self.rect.y = self.obstacles[collide_index].top - self.square_size
elif self.direction["current"] == "right":
self.rect.x = self.obstacles[collide_index].left - self.square_size
elif self.direction["current"] == "left":
self.rect.x = self.obstacles[collide_index].right
self.direction["previous"] = self.direction["current"]
self.direction["current"] = None
def draw_score(self, surface):
label = self.font.render(f"Score : {self.score}", 1, (222, 222, 222))
x_pos = surface.get_width() - label.get_width() - self.square_size
y_pos = self.square_size // 2 - label.get_height() // 2
surface.blit(label, (x_pos, y_pos))
def draw_lives(self, surface):
label = self.font.render(f"Lives : {self.lives}", 1, (222, 222, 222))
x_pos = surface.get_width() // 2 - label.get_width() // 2
y_pos = surface.get_height() - label.get_height() - 4
surface.blit(label, (x_pos, y_pos))
def draw(self, surface):
self.draw_score(surface)
self.draw_lives(surface)
if not self.make_death_anim:
self.move_anim.do()
if self.direction["current"] is None:
self.move_anim.s_index = 0
direction = "right"
else:
direction = self.direction["current"]
sprite = self.sprites["moving"][direction][self.move_anim.s_index]
pos_x = self.rect.x + (self.square_size - sprite.get_width()) / 2
pos_y = (self.rect.y +
(self.square_size - sprite.get_height()) / 2 + 1)
surface.blit(sprite, (pos_x, pos_y))
# death anim
if self.make_death_anim:
self.move_speed = 0
sprite = self.death_anim.current_sprite
pos_x = self.rect.x + (self.square_size - sprite.get_width()) / 2
pos_y = (self.rect.y +
(self.square_size - sprite.get_height()) / 2 + 1)
surface.blit(sprite, (pos_x, pos_y))
if self.death_anim.do() == "end":
# reset
self.make_death_anim = False
self.make_ghost_respawn = True
self.reset_pos()
def reset_pos(self):
self.rect.x = self.start_pos[0]
self.rect.y = self.start_pos[1]
self.move_speed = self.reset_move_speed
self.direction["current"] = "left"
# Tests if the player is killed or not
def test_kill(self):
if self.killed is True:
self.killed = False
self.lives -= 1
self.death_anim.s_index = 0
self.make_death_anim = True
return True
# make anim