-
Notifications
You must be signed in to change notification settings - Fork 0
/
fighter.py
260 lines (230 loc) · 8.44 KB
/
fighter.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
255
256
257
258
259
260
from pickle import TRUE
from re import T
import pygame
#teste
class Fighter():
def __init__(self, player, x, y, flip, data, sprite_sheet, animation_steps,sound):
self.player = player
self.size = data[0]
self.image_scale = data[1]
self.offset = data[2]
self.flip = flip
self.animation_list = self.load_images(sprite_sheet, animation_steps)
self.action = 0#0:idle #1:run #2:jump #3:attack1 #4: attack2 #5:hit #6:death
self.frame_index = 0
self.image = self.animation_list[self.action][self.frame_index]
self.update_time = pygame.time.get_ticks()
self.rect = pygame.Rect((x, y, 80, 180))
self.vel_y = 0
self.running = False
self.jump = False
self.attacking = False
self.attack_type = 0
self.attack_cooldown = 0
self.hit = False
self.health = 1000
self.alive = True
self.defend = False
self.defendHit = False
self.squat = False
self.sound_atack = sound
def load_images(self, sprite_sheet, animation_steps):
#extract images from spritesheet
animation_list = []
for y, animation in enumerate(animation_steps):
temp_img_list = []
for x in range(animation):
temp_img = sprite_sheet.subsurface(x * self.size, y * self.size, self.size, self.size)
temp_img_list.append(pygame.transform.scale(temp_img, (self.size * self.image_scale, self.size * self.image_scale)))
animation_list.append(temp_img_list)
return animation_list
def move(self, screen_width, screen_height, surface, target, round_over):
SPEED = 10
GRAVITY = 2
dx = 0
dy = 0
self.running = False
self.attack_type = 0
#get keypresses
key = pygame.key.get_pressed()
#can only perform other actions if not currently attacking
if self.attacking == False and self.alive == True and round_over == False:
#check player 1 controls
if self.player == 1:
#movement
if key[pygame.K_a] and (not(self.squat) and not(self.defend)):
dx = -SPEED
self.running = True
if key[pygame.K_d] and (not(self.squat) and not(self.defend)):
dx = SPEED
self.running = True
#jump
if key[pygame.K_w] and self.jump == False and not(self.defend):
self.vel_y = -30
self.jump = True
#squat
if key[pygame.K_s] and self.jump == False:
self.rect = pygame.Rect(( self.rect.left, self.rect.bottom, 80, 110))
self.squat = True
else:
self.rect = pygame.Rect((self.rect.left, self.rect.top, 80, 180))
self.squat = False
#defend
if key[pygame.K_y]:
self.defend = True
else:
self.defend = False
#attack
if (key[pygame.K_r] or key[pygame.K_t]) and not(self.defend):
self.attack(surface, target)
#determine which attack type was used
if key[pygame.K_r]:
self.attack_type = 1
if key[pygame.K_t]:
self.attack_type = 2
#check player 2 controls
if self.player == 2:
#movement
if key[pygame.K_LEFT] and (not(self.squat) and not(self.defend)):
dx = -SPEED
self.running = True
if key[pygame.K_RIGHT] and (not(self.squat) and not(self.defend)):
dx = SPEED
self.running = True
#jump
if key[pygame.K_UP] and self.jump == False and not(self.defend):
self.vel_y = -30
self.jump = True
#attack
if key[pygame.K_i] or key[pygame.K_o]:
self.attack(surface, target)
#determine which attack type was used
if key[pygame.K_i]:
self.attack_type = 1
if key[pygame.K_o]:
self.attack_type = 2
#squat
if key[pygame.K_DOWN]:
self.rect = pygame.Rect(( self.rect.left, self.rect.bottom, 90, 110))
self.squat = True
else:
self.rect = pygame.Rect(( self.rect.left, self.rect.top, 80, 180))
self.squat = False
#defend
if key[pygame.K_p]:
self.defend = True
else:
self.defend = False
#apply gravity
self.vel_y += GRAVITY
dy += self.vel_y
#ensure player stays on screen
if self.rect.left + dx < 0:
dx = -self.rect.left
if self.rect.right + dx > screen_width:
dx = screen_width - self.rect.right
if self.rect.bottom + dy > screen_height - 110:
self.vel_y = 0
self.jump = False
dy = screen_height - 110 - self.rect.bottom
#ensure players face each other
if target.rect.centerx > self.rect.centerx:
self.flip = False
else:
self.flip = True
#apply attack cooldown
if self.attack_cooldown > 0:
self.attack_cooldown -= 1
#update player position
self.rect.x += dx
self.rect.y += dy
#handle animation updates
def update(self):
#check what action the player is performing
if self.health <= 0:
self.health = 0
self.alive = False
self.update_action(6)#6:death
elif self.hit == True:
self.update_action(5)#5:hit
elif self.defendHit == True:
if self.squat == True:
self.update_action(11)
else:
self.update_action(9)
elif self.attacking == True:
if self.squat == True:
if self.attack_type == 1:
self.update_action(12)
elif self.attack_type == 2:
self.update_action(13)
else:
if self.attack_type == 1:
self.update_action(3)#3:attack1
elif self.attack_type == 2:
self.update_action(4)#4:attack2
elif self.jump == True:
self.update_action(2)#2:jump
elif self.defend == True:
if self.squat == True:
self.update_action(10)
else:
self.update_action(8)
elif self.squat == True:
self.update_action(7)
elif self.running == True:
self.update_action(1)#1:run
else:
self.update_action(0)#0:idle
animation_cooldown = 50
#update image
self.image = self.animation_list[self.action][self.frame_index]
#check if enough time has passed since the last update
if pygame.time.get_ticks() - self.update_time > animation_cooldown:
self.frame_index += 1
self.update_time = pygame.time.get_ticks()
#check if the animation has finished
if self.frame_index >= len(self.animation_list[self.action]):
#if the player is dead then end the animation
if self.alive == False:
self.frame_index = len(self.animation_list[self.action]) - 1
else:
self.frame_index = 0
#check if an attack was executed
if self.action == 3 or self.action == 4 or self.action == 12 or self.action == 13:
self.attacking = False
self.attack_cooldown = 20
#check if damage was taken
if self.action == 5:
self.hit = False
#if the player was in the middle of an attack, then the attack is stopped
self.attacking = False
self.attack_cooldown = 20
if self.action == 11 or 9:
self.defendHit = False
def attack(self,surface, target):
if self.attack_cooldown == 0:
#execute attack
self.attacking = True
if self.squat == True:
attacking_rect = pygame.Rect(self.rect.centerx - (1.5 * self.rect.width * self.flip), self.rect.y - 110, 1.5 * self.rect.width, self.rect.height / 2.85)
else:
attacking_rect = pygame.Rect(self.rect.centerx - (1.5 * self.rect.width * self.flip), self.rect.y, 1.5 * self.rect.width, self.rect.height / 2.85)
if attacking_rect.colliderect(target.rect):
self.sound_atack.play()
if target.defend and (self.squat == target.squat):
target.health -= 10
target.defendHit = True
else:
target.health -= 100
target.hit = True
def update_action(self, new_action):
#check if the new action is different to the previous one
if new_action != self.action:
self.action = new_action
#update the animation settings
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
def draw(self, surface,):
img = pygame.transform.flip(self.image, self.flip, False)
surface.blit(img, (self.rect.x - (self.offset[0] * self.image_scale), self.rect.y - (self.offset[1] * self.image_scale)))