-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchar.py
433 lines (383 loc) · 16.1 KB
/
char.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import pygame
import random
from game import SpriteSheetLoader, Point
# STATES
# IDLE -> 0
# WALKING LEFT -> 1
# WALKING RIGHT -> 2
# JUMPING -> 3
# PUNCHING -> 4
# KICKING -> 5
# TAKE HIT -> 6
# POWER -> 7
# checks if lines x and y collide, with y >= x
def one_dimension_collide(x0, x1, y0, y1):
return (y0 <= x0 <= y1) or (y0 <= x1 <= y1)
def collide(p1, p2):
if p1.width <= p2.width and p1.height <= p2.height:
return one_dimension_collide(p1.x, p1.x + p1.width, p2.x, p2.x + p2.width) and \
one_dimension_collide(p1.y, p1.y + p1.height, p2.y, p2.y + p2.height)
elif p1.width <= p2.width and p1.height > p2.height:
return one_dimension_collide(p1.x, p1.x + p1.width, p2.x, p2.x + p2.width) and \
one_dimension_collide(p2.y, p2.y + p2.height, p1.y, p1.y + p1.height)
elif p1.width > p2.width and p1.height <= p2.height:
return one_dimension_collide(p2.x, p2.x + p2.width, p1.x, p1.x + p1.width) and \
one_dimension_collide(p1.y, p1.y + p1.height, p2.y, p2.y + p2.height)
else:
return one_dimension_collide(p2.x, p2.x + p2.width, p1.x, p1.x + p1.width) and \
one_dimension_collide(p2.y, p2.y + p2.height, p1.y, p1.y + p1.height)
class Char:
def __init__(self, name, rgb, index):
self.icon = CharIcon(rgb, index)
self.name = name
self.index = index
self.t_anim = 0
self.velocity = None
self.jumping = self.up_animation = self.down_animation = False
self.left_animation = self.right_animation = self.idle_animation = False
self.punching = self.punch_animation = False
self.kicking = self.kick_animation = False
self.hit_animation = self.power_animation = False
self.win_animation = False
self.tie = False
self.sprites = self.spriteline = self.spritecolumn = None
self.hitbox = self.health = self.power_bar = self.superpower = None
def load(self, is_left_player):
self.sprites = SpriteSheetLoader('personagens//' + str(self.index) + '//sprites.png', 120, 100).getSpriteList()
self.spriteline = 0
self.spritecolumn = 0
if is_left_player:
self.hitbox = Hitbox(30, 40, Point(45, 200), True)
else:
self.hitbox = Hitbox(30, 40, Point(245, 200), False)
self.health = HealthBar(is_left_player)
self.power_bar = PowerBar(is_left_player)
self.superpower = SuperPower(self.index)
self.superpower.load(is_left_player)
def set_state(self, state):
if state == 0: # idle
self.idle_animation = True
self.jumping = self.left_animation = self.right_animation = self.up_animation = self.down_animation = False
elif state == 1: # left
self.left_animation = True
self.jumping = self.right_animation = self.up_animation = self.down_animation = self.idle_animation = False
elif state == 2: # right
self.right_animation = True
self.jumping = self.left_animation = self.up_animation = self.down_animation = self.idle_animation = False
elif state == 3: # jumping
self.jumping = True
self.left_animation = self.right_animation = self.up_animation = self.down_animation = self.idle_animation = False
elif state == 4: # punching
self.punch_animation = self.punching = True
self.left_animation = self.right_animation = self.idle_animation = False
elif state == 5: # kicking
self.kick_animation = self.kicking = True
self.left_animation = self.right_animation = self.idle_animation = False
elif state == 6: # take hit
self.hit_animation = True
self.left_animation = self.right_animation = self.idle_animation = False
elif state == 7: # launch power
self.power_animation = True
self.left_animation = self.right_animation = self.idle_animation = False
def idle(self):
self.spriteline = 0
if not self.idle_animation:
self.spritecolumn = 0
def right(self, other):
self.spriteline = 1
if not self.right_animation:
self.spritecolumn = 0
self.velocity = 0.2
self.hitbox.x += self.velocity
if self.hitbox.x + self.hitbox.width > 320 or collide(self.hitbox, other):
self.hitbox.x -= self.velocity
def left(self, other):
self.spriteline = 1
if not self.left_animation:
self.spritecolumn = 0
self.velocity = -0.2
self.hitbox.x += self.velocity
if self.hitbox.x < 0 or collide(self.hitbox, other):
self.hitbox.x -= self.velocity
def jump(self, delta, other):
self.spritecolumn = 0
if self.up_animation and not self.down_animation:
self.spriteline = 6
elif self.down_animation:
self.spriteline = 7
deltah = 0.40 * delta - 0.0004 * delta * delta
if deltah < 0:
self.jumping = False
self.hitbox.y = self.hitbox.ground
self.hitbox.update_side(other)
if collide(self.hitbox, other):
if self.hitbox.is_left_player:
self.hitbox.x = other.x - other.width - 1
else:
self.hitbox.x = other.x + other.width + 1
return
self.hitbox.y = self.hitbox.ground - deltah
self.up_animation = True
if self.hitbox.y < 102:
self.down_animation = True
if self.velocity != 0 and (self.hitbox.x + self.hitbox.width <= 320) and self.hitbox.x >= 0:
self.hitbox.x += self.velocity
def punch(self, other):
dmg = 5
if self.punching:
self.spritecolumn = 0
self.t_anim = 0
if self.hitbox.is_left_player:
hitbox = Hitbox(18, 30, Point(self.hitbox.x + self.hitbox.width, self.hitbox.y))
else:
hitbox = Hitbox(18, 30, Point(self.hitbox.x - 18, self.hitbox.y))
if collide(hitbox, other.hitbox):
other.take_hit(dmg)
self.power_bar.up(dmg, False)
self.punching = False
if not self.jumping:
self.spriteline = 21
else:
self.spriteline = 25
def kick(self, other):
dmg = 5
if self.kicking:
self.spritecolumn = 0
self.t_anim = 0
if self.hitbox.is_left_player:
hitbox = Hitbox(18, 30, Point(self.hitbox.x + self.hitbox.width, self.hitbox.y))
else:
hitbox = Hitbox(18, 30, Point(self.hitbox.x - 18, self.hitbox.y))
if collide(hitbox, other.hitbox):
other.take_hit(dmg)
self.power_bar.up(dmg, False)
self.kicking = False
if not self.jumping:
self.spriteline = 23
else:
self.spriteline = 27
def launch_superpower(self, other):
if self.power_bar.is_ready():
self.superpower.active = True
self.superpower.hitbox.x = self.hitbox.x
self.superpower.hitbox.y = self.hitbox.y
self.power_bar.value = 0
self.set_state(7)
self.t_anim = 0
self.spritecolumn = 0
self.spriteline = 33
def take_hit(self, dmg):
self.health.take_damage(dmg)
self.power_bar.up(dmg, True)
self.set_state(6)
self.spritecolumn = 0
self.t_anim = 0
if not self.jumping:
self.spriteline = random.randint(9,10)
else:
self.spriteline = random.randint(10,11)
def refresh_movement_animation(self, time):
if self.left_animation or self.right_animation or self.idle_animation:
if time - self.t_anim > 100:
self.t_anim = time
self.spritecolumn += 1
self.spritecolumn %= len(self.sprites[self.spriteline])
def refresh_combat_animation(self, time):
if self.spritecolumn == 0 and self.t_anim == 0:
self.t_anim = time
# punch/kick animation
if self.punch_animation or self.kick_animation:
if not self.jumping:
if time - self.t_anim > 50 and self.spritecolumn < 3:
self.t_anim = time
self.spritecolumn += 1
elif self.spritecolumn == 3:
self.punch_animation = False
self.kick_animation = False
else:
if time - self.t_anim > 200:
self.punch_animation = False
self.kick_animation = False
# hit animation
if self.hit_animation:
if time - self.t_anim > 200:
self.hit_animation = False
# launch power animation
if self.power_animation:
if time - self.t_anim > 100 and self.spritecolumn < 3:
self.t_anim = time
self.spritecolumn += 1
elif self.spritecolumn == 3:
self.power_animation = False
def roundend_animation(self, time):
if self.spritecolumn == 0 and self.t_anim == 0:
self.t_anim = time
if self.win_animation:
self.spriteline = 17
if time - self.t_anim > 400 and self.spritecolumn < 3:
self.t_anim = time
self.spritecolumn += 1
elif self.spritecolumn == 3:
if time - self.t_anim > 3000:
self.win_animation = False
elif self.tie:
self.spriteline = 5
if time - self.t_anim > 3000:
self.tie = False
else:
self.spriteline = 5
def reset(self, is_left_player):
self.t_anim = 0
self.velocity = 0
self.spriteline = 0
self.spritecolumn = 0
self.hitbox.y = 200
self.hitbox.is_left_player = is_left_player
if is_left_player:
self.hitbox.x = 45
else:
self.hitbox.x = 245
self.health.value = 100
self.set_state(0)
self.punching = self.punch_animation = self.kicking = self.kick_animation = False
self.hit_animation = self.power_animation = self.superpower.active = False
def print_me(self, screen):
sprite = self.sprites[self.spriteline][self.spritecolumn]
if not self.hitbox.is_left_player:
sprite = pygame.transform.flip(sprite, True, False)
screen.blit(sprite, self.hitbox.get_print_pos(True))
self.health.print_me(screen)
self.power_bar.print_me(screen)
#self.hitbox.print_me(screen)
if self.superpower.active:
self.superpower.print_me(screen)
class CharIcon:
def __init__(self, rgb, index):
self.rgb = rgb
self.position = None
self.iconimg = pygame.transform.scale(
pygame.image.load('personagens//' + str(index) + '//icon.png').convert_alpha(), (32, 24))
def print_me(self, screen):
icon = pygame.Surface((32, 24))
icon.fill(self.rgb)
icon.blit(self.iconimg, (0, 0))
screen.blit(icon, self.position.value())
class HealthBar:
def __init__(self, is_left_player):
self.value = 100
self.img = pygame.image.load('img//healthbar.png').convert_alpha()
self.redbar = pygame.image.load('img//redbar.png').convert_alpha()
self.damage = 0
self.is_left_player = is_left_player
def get_health_bar_position(self, is_left_player, health_bar_width):
if is_left_player:
return 143 - health_bar_width, 10
else:
return 177, 10
def get_red_bar_position(self, is_left_player, health_bar_position, health_bar_width, red_bar_width):
if is_left_player:
return health_bar_position[0] - red_bar_width, health_bar_position[1]
else:
return health_bar_position[0] + health_bar_width - 1, health_bar_position[1]
def take_damage(self, dmg):
if self.value > dmg:
self.value -= dmg
self.damage += dmg
else:
self.value = 0
def print_me(self, screen):
health_bar_width = int(140 * self.value / 100)
health_bar_position = self.get_health_bar_position(self.is_left_player, health_bar_width)
if self.damage > 0:
red_bar_width = int(140 * self.damage / 100) + 1
red_bar = pygame.transform.scale(self.redbar, (red_bar_width, 10))
screen.blit(red_bar, self.get_red_bar_position(
self.is_left_player, health_bar_position, health_bar_width, red_bar_width))
self.damage -= 0.01
health_bar_width = int(140 * self.value / 100)
health_bar = pygame.transform.scale(self.img, (health_bar_width, 12))
screen.blit(health_bar, health_bar_position)
class PowerBar:
def __init__(self, is_left_player):
self.value = 0
self.img = pygame.image.load('img//powerbar.png').convert_alpha()
self.is_left_player = is_left_player
def get_power_bar_position(self, is_left_player, power_bar_width):
if is_left_player:
return 143 - power_bar_width, 30
else:
return 177, 30
def up(self, dmg, hit_taken):
if self.value < 100:
if hit_taken:
self.value += dmg/2
else:
self.value += dmg
elif self.value > 100:
self.value = 100
def print_me(self, screen):
power_bar_width = int(140 * self.value / 100)
power_bar_position = self.get_power_bar_position(self.is_left_player, power_bar_width)
power_bar_width = int(140 * self.value / 100)
power_bar = pygame.transform.scale(self.img, (power_bar_width, 12))
screen.blit(power_bar, power_bar_position)
def is_ready(self):
if self.value == 100:
return True
return False
class Hitbox:
def __init__(self, width, height, position, is_left_player=None):
self.width = width
self.height = height
self.is_left_player = is_left_player
# coordinates for the upper left point of the hitbox
self.x = position.x
self.y = self.ground = position.y
def get_print_pos(self, is_player):
if is_player:
return self.x - 45, self.y - 60
else: # is superpower
if self.is_left_player:
return self.x - 5, self.y - 28
else:
return self.x - 25, self.y - 28
def update_side(self, other):
if self.x < other.x:
self.is_left_player = True
other.is_left_player = False
else:
self.is_left_player = False
other.is_left_player = True
# def print_me(self, screen):
# rect = pygame.Rect(self.x, self.y, self.width, self.height)
# pygame.draw.rect(screen, (255, 0, 0), rect, 1)
class SuperPower:
def __init__(self, player_idx):
self.sprites = self.spriteline = self.spritecolumn = self.hitbox = None
self.player_idx = player_idx
self.active = False
def load(self, is_left_player):
self.sprites = SpriteSheetLoader('img//superpower.png', 60, 60).getSpriteList()
self.spriteline = self.player_idx - 1
self.spritecolumn = 0
if is_left_player:
self.hitbox = Hitbox(30, 25, Point(45, 180), True)
else:
self.hitbox = Hitbox(30, 25, Point(245, 180), False)
def launch(self, player, other, dmg):
if not collide(self.hitbox, other.hitbox) and -self.hitbox.width <= self.hitbox.x <= 320:
if player.hitbox.is_left_player:
self.hitbox.x += 0.4
else:
self.hitbox.x -= 0.4
elif collide(self.hitbox, other.hitbox):
other.take_hit(dmg)
player.power_bar.value += dmg/2
self.active = False
else:
self.active = False
def print_me(self, screen):
sprite = self.sprites[self.spriteline][self.spritecolumn]
if not self.hitbox.is_left_player:
sprite = pygame.transform.flip(sprite, True, False)
screen.blit(sprite, self.hitbox.get_print_pos(False))