-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.py
254 lines (213 loc) · 14.1 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import pygame
from pygame.locals import *
from HealthBar import HealthBar
vec = pygame.math.Vector2
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.image.load("Images/Player_Sprite_R.png")
self.rect = pygame.Rect(x, y, 10, -16)
# Player Info
self.pos = vec(x, y)
self.acc = vec(0, 0)
self.vel = vec(0, 0)
self.healthbar = HealthBar(10, 10)
# Player Constants
self.ACC = 0.4
self.FRIC = -0.1
# Player Movements
self.jumping = False
self.running = False
self.sleep =False
self.direction = "RIGHT"
self.move_frame = 0
# Player Attacking
self.attacking = False
self.attack_frame = 0
self.attack_counter = 0
self.attack_range = pygame.Rect(0, 0, 0, 0)
self.hit_cooldown = False
# Player Events
self.hit_cooldown_event = pygame.USEREVENT + 1
def move(self):
self.acc = vec(0, 0.5)
if abs(self.vel.x) > 0.3:
self.running = True
else:
self.running = False
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.acc.x = -self.ACC
if keys[K_RIGHT]:
self.acc.x = self.ACC
self.acc.x += self.vel.x * self.FRIC
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
if self.pos.x > 800:
self.pos.x = -40
elif self.pos.x < -40:
self.pos.x = 800
self.rect.topleft = self.pos
self.rect.x += 32
def walking(self):
if self.move_frame > 6:
self.move_frame = 0
return
if self.jumping == False and self.running == True:
if self.vel.x >= 0:
self.image = self.animation_right[self.move_frame]
self.direction = "RIGHT"
elif self.vel.x < 0:
self.image = self.animation_left[self.move_frame]
self.direction = "LEFT"
self.move_frame += 1
if self.running == False and self.move_frame != 0:
self.move_frame = 0
if self.direction == "RIGHT":
self.image = self.animation_right[self.move_frame]
elif self.direction == "LEFT":
self.image = self.animation_left[self.move_frame]
def attack(self):
if self.attacking == True:
if self.direction == "RIGHT":
self.attack_range = pygame.Rect(self.rect.x + self.rect.width,self.pos.y, 30, self.rect.height)
elif self.direction == "LEFT":
self.attack_range = pygame.Rect(self.pos.x, self.pos.y, 30, self.rect.height)
if self.attack_frame > 38:
self.attack_frame = 0
self.attacking = False
self.attack_range = pygame.Rect(0, 0, 0, 0)
return
if self.direction == "RIGHT":
self.image = self.attack_animation_right[self.attack_frame]
elif self.direction == "LEFT":
self.image = self.attack_animation_left[self.attack_frame]
self.attack_counter += 1
if self.attack_counter >= 3:
self.attack_frame += 1
self.attack_counter = 0
def update(self, group):
self.walking()
self.move()
self.attack()
self.collision(group)
def player_hit(self, damage):
if self.hit_cooldown == False:
self.hit_cooldown = True
self.healthbar.takeDamage(damage)
pygame.time.set_timer(self.hit_cooldown_event, 1000)
def collision(self, group):
hits = pygame.sprite.spritecollide(self, group, False)
if self.vel.y > 0:
if hits:
lowest = hits[0]
if self.rect.bottom >= lowest.rect.top:
self.pos.y = lowest.rect.top - self.rect.height
self.rect.y = lowest.rect.top - self.rect.height
self.vel.y = 0
self.jumping = False
def jump(self):
if self.jumping == False:
self.jumping = True
self.vel.y = -12
def render(self, display):
#pygame.draw.rect(display, (255, 0, 0), self.rect)
#pygame.draw.rect(display, (0, 255, 0), self.attack_range)
display.blit(self.image, self.pos)
self.healthbar.render(display)
def load_animations(self):
self.animation_right = [pygame.image.load("Images/Player_Sprite_R.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite2_R.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite3_R.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite4_R.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite5_R.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite6_R.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite_R.png").convert_alpha()]
self.animation_left = [pygame.image.load("Images/Player_Sprite_L.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite2_L.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite3_L.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite4_L.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite5_L.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite6_L.png").convert_alpha(),
pygame.image.load("Images/Player_Sprite_L.png").convert_alpha()]
############################################obrdle###bordel*******************************************************
self.attack_animation_right = [pygame.image.load("Images/Player_Sprite_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack1_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack1_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack2_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack2_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack3_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack3_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack3_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack3_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4bis_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4bis_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4bis_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack5_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack5bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_R.png").convert_alpha(),
pygame.image.load("Images/Player_Attack5cis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5cis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5cis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5cis_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack6_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack6_R.png").convert_alpha(),
pygame.image.load("Images\Player_Attack_R.png").convert_alpha()]
self.attack_animation_left = [pygame.image.load("Images/Player_Sprite_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack1_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack1_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack2_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack2_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack3_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack3_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack3_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack3_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack4bis_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4bis_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack4bis_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack5_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack5bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5bis_L.png").convert_alpha(),
pygame.image.load("Images/Player_Attack5cis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5cis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5cis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack5cis_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack6_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack6_L.png").convert_alpha(),
pygame.image.load("Images\Player_Attack_L.png").convert_alpha()]