-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.py
532 lines (422 loc) · 19.4 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
from Animations import AnimatedSprite
import Levels
from PPlay.sprite import *
from PPlay.gameobject import *
from PPlay.collision import *
from GameWindow import GameWindow
from Levels import *
from Input import *
import pygame
import Game
from Enemy import Enemy, allEnemies
class Player:
health = 100
direction = 2 #2 means right, 1 means left
standing = True
still = True
sliding = False
slidingTimer = Misc.Timer()
jumping = False
freeFalling = False
freeFallingTimer = Misc.Timer()
falling = False
grounded = False
jumpingTimer = Misc.Timer()
attacking = False
attack = False
attackOffCooldown = True
attackingTimer = Misc.Timer()
attackCooldownTimer = Misc.Timer()
groundLevelY = 0
groundLevelX = 0
collidedGround = False
collidedWall = False
sprite = Sprite("sprites/player/right/julius-idle/1.png")
sprite.set_sequence_time(0, 14, 80, True)
levelGroundFloor = 465
sprite.set_position(5, 0)
#sprite.set_position(1000, 0)
walkSpeed = 250
dashSpeed = 600
jumpSpeed = 1200
jumpSpeedDivision = 5000
fallSpeed = 800
tempDashSpeed = dashSpeed
tempJumpSpeed = jumpSpeed
tempJumpSpeedDivision = jumpSpeedDivision
tempFallSpeed = fallSpeed
currentSpeed = 0
@staticmethod
def spawnJulius(): pass
#Player.sprite.draw()
#Player.sprite.update()
#Julius.setGravity()
#Julius.sprite.mask.scale((800, 900))
@staticmethod
def controlJulius(Level):
if(Input.getKeyDown("d")):
Player.direction = 2
if(Input.getKeyDown("a")):
Player.direction = 1
if((GameWindow.window.keyboard.key_pressed("d") or GameWindow.window.keyboard.key_pressed("a")) and Player.standing or Player.sliding):
#Player.direction = 2
Player.still = False
else:
Player.still = True
if(Player.attacking and Player.grounded):
Player.still = True
if(GameWindow.window.keyboard.key_pressed("s") and not Player.jumping and not Player.falling):
Player.standing = False
elif(not GameWindow.window.keyboard.key_pressed("s") and not Player.sliding):
Player.standing = True
if(not Player.standing and not Player.jumping and not Player.freeFalling and not Player.falling and Input.getKeyDown("SPACE")):
Player.sliding = True
if(Player.sliding):
Player.slidingTimer.resumeTimer()
Player.slidingTimer.executeTimer()
if(Player.slidingTimer.time >= 0.8):
Player.sliding = False
Player.slidingTimer.stopTimer()
Player.slidingTimer.resetTimer()
if(not Player.attacking and not Player.freeFalling and not Player.falling and Input.getKeyDown("SPACE")):
Player.jumping = True
if (Player.jumping):
Player.jumpingTimer.resumeTimer()
Player.jumpingTimer.executeTimer()
if(GameWindow.keyboard.key_pressed("SPACE") and Player.jumpSpeedDivision > 1400):
Player.jumpSpeedDivision -= 10000 * GameWindow.window.delta_time()
Player.jumpSpeed += 1100 * GameWindow.window.delta_time()
if (not Player.jumpSpeed > 0):
Player.jumping = False
Player.jumpingTimer.stopTimer()
Player.jumpingTimer.resetTimer()
if(Player.standing and Input.getKeyDown("i") and Player.attackOffCooldown):
Player.attacking = True
if Player.direction == 1:
Player.sprite.x -= 25
else:
Player.sprite.x += 25
Player.attackOffCooldown = False
if(Player.attacking):
Player.attackingTimer.resumeTimer()
Player.attackingTimer.executeTimer()
if(Player.attackingTimer.time >= 0.4):
Player.attacking = False
JuliusAnim.lockAttackAnim = False
Player.attackingTimer.stopTimer()
Player.attackingTimer.resetTimer()
if(not Player.attackOffCooldown):
Player.attackCooldownTimer.resumeTimer()
Player.attackCooldownTimer.executeTimer()
if(Player.attackCooldownTimer.time >= 0.5):
Player.attackOffCooldown = True
Player.attackCooldownTimer.stopTimer()
Player.attackCooldownTimer.resetTimer()
#JuliusAnim.setAnims()
JuliusAnim.animationController()
@staticmethod
def verifyColissionEnemies(direction):
for enemy in allEnemies:
if direction == 1 and Player.sprite.x <= enemy.sprite.x and not(Player.sprite.y + Player.sprite.height <= enemy.sprite.y):
return False
if direction == 2 and Player.sprite.x >= enemy.sprite.x and not(Player.sprite.y + Player.sprite.height <= enemy.sprite.y):
return False
return True
""" @staticmethod
def verifyColissionEnemies(direction):
for enemy in allEnemies:
if Player.sprite.collided_perfect(enemy.sprite):
return False """
## WALKING
if(not Player.still and Player.direction == 2 and Player.standing and not Player.collidedWall): #and verifyColissionEnemies(Player.direction)):
if(Player.sprite.x < Level.scrollingLimit + 1 or Level.reachedLimitRight):
Player.sprite.x += Player.walkSpeed * GameWindow.window.delta_time()
#if (Player.sprite.collided_perfect(Level1area1.tiles)):
#Player.groundLevelX += Player.walkSpeed * GameWindow.window.delta_time()
elif(not Player.still and Player.direction == 1 and Player.standing and not Player.collidedWall): #and verifyColissionEnemies(Player.direction)):
if(Player.sprite.x > Level.scrollingLimit - 1 or Level.reachedLimitLeft):
Player.sprite.x -= Player.walkSpeed * GameWindow.window.delta_time()
#if (Player.sprite.collided_perfect(Level1area1.tiles)):
#Player.groundLevelX -= Player.walkSpeed * GameWindow.window.delta_time()
if(Player.sliding and Player.direction == 2):
if (Player.sprite.x < Level.scrollingLimit or Level.reachedLimitRight):
Player.sprite.x += Player.dashSpeed * GameWindow.window.delta_time()
#Player.currentSpeed = Player.dashSpeed
elif(Player.sliding and Player.direction == 1):
if (Player.sprite.x > Level.scrollingLimit or Level.reachedLimitLeft):
Player.sprite.x -= Player.dashSpeed * GameWindow.window.delta_time()
elif(not Player.sliding):
Player.dashSpeed = Player.tempDashSpeed
if(Player.jumping):
Player.sprite.y -= Player.jumpSpeed * GameWindow.window.delta_time()
if(Player.jumpSpeed > 0):
Player.jumpSpeed -= (Player.jumpSpeedDivision * GameWindow.window.delta_time())
else:
Player.jumpSpeed = Player.tempJumpSpeed
Player.jumpSpeedDivision = Player.tempJumpSpeedDivision
if(not Player.still and not Player.sliding):
Player.currentSpeed = Player.walkSpeed
elif(not Player.still and Player.sliding):
Player.currentSpeed = Player.dashSpeed
if(Player.dashSpeed > 0):
Player.dashSpeed -= 1000 * GameWindow.window.delta_time()
Player.scrollLevel(Game.Game.currentLevel)
Player.setGravity()
#Player.setCollision()
@staticmethod
def setCollision():
## COLISAO COM AS PAREDES: ainda ta mt cru
#if (Player.groundLevelX >= 0.03):
#Player.sprite.x -= 1000 * GameWindow.window.delta_time()
#if (Player.groundLevelX <= - 6):
#Player.sprite.x += 2000 * GameWindow.window.delta_time()
if(Player.groundLevelX >= 0.03 or Player.groundLevelX < -0.03):
Player.collidedWall = True
else:
Player.collidedWall = False
if (not Player.still and Player.direction == 2 and Player.sprite.collided_perfect(Game.Game.currentLevel.tiles) and Player.groundLevelX < 0.03):
Player.groundLevelX += 1 * GameWindow.window.delta_time()
if (not Player.still and Player.direction == 1 and Player.sprite.collided_perfect(Game.Game.currentLevel.tiles) and Player.groundLevelX > -0.03):
Player.groundLevelX -= 1 * GameWindow.window.delta_time()
if (not Player.sprite.collided_perfect(Game.Game.currentLevel.tiles)):
Player.groundLevelX = 0
## COLISAO COM O CHAO: funciona ok
if ((not Player.still and Player.grounded and Player.sprite.collided_perfect(
Game.Game.currentLevel.tiles) and Player.groundLevelY < 0.01 and not Player.collidedWall)):
Player.sprite.y -= 500 * GameWindow.window.delta_time()
Player.groundLevelY += 1 * GameWindow.window.delta_time()
else:
Player.groundLevelY = 0
@staticmethod
def setGravity():
#groupTiles = pygame.sprite.Group()
#groupTiles.add(Levels.Level1area1.tiles)
#if(not pygame.sprite.collide_mask(Levels.Level1area1.tiles, Player.sprite)):
#Player.sprite.y < Player.levelGroundFloor and not Player.jumping
if(Player.falling):
Player.freeFallingTimer.resumeTimer()
Player.freeFallingTimer.executeTimer()
if(Player.freeFallingTimer.time >= 0.1):
Player.freeFalling = True
Player.freeFallingTimer.stopTimer()
Player.freeFallingTimer.resetTimer()
else:
Player.freeFalling = False
Player.freeFallingTimer.stopTimer()
Player.freeFallingTimer.resetTimer()
if ((not Game.Game.currentLevel.tiles.collided_perfect(
Player.sprite) and not Player.jumping) or Player.groundLevelY > 0.1):
Player.sprite.y += Player.fallSpeed * GameWindow.window.delta_time()
Player.fallSpeed += 200 * GameWindow.window.delta_time()
Player.falling = True
else:
Player.fallSpeed = Player.tempFallSpeed
Player.falling = False
if ((Player.falling and Player.freeFalling) or Player.jumping):
Player.grounded = False
else:
Player.grounded = True
"""if(not Player.still and Player.grounded and Player.sprite.collided_perfect(Level1area1.tiles)):
Player.groundLevelX += Player.walkSpeed * GameWindow.window.delta_time()
else:
Player.groundLevelX = 0"""
"""if(Player.sprite.rect.bottom >= Level1area1.tiles.rect.top):
Player.collidedGround = True
else:
Player.collidedGround = False"""
@staticmethod
def scrollLevel(Level):
if (Level.tiles.x >= 0):
Level.reachedLimitLeft = True
else:
Level.reachedLimitLeft = False
if (Level.tiles.x <= -Level.tiles.width / 2):
Level.reachedLimitRight = True
else:
Level.reachedLimitRight = False
if (Player.direction == 2):
if (Player.sprite.x > Level.scrollingLimit and not Player.still and not Player.collidedWall and not Level.reachedLimitRight):
Level.tiles.x -= Player.currentSpeed * GameWindow.window.delta_time()
Level.background.x -= (Player.currentSpeed / 2) * GameWindow.window.delta_time()
elif (Player.direction == 1):
if (Player.sprite.x < Level.scrollingLimit and not Player.still and not Player.collidedWall and not Level.reachedLimitLeft):
Level.tiles.x += Player.currentSpeed * GameWindow.window.delta_time()
Level.background.x += (Player.currentSpeed / 2) * GameWindow.window.delta_time()
class JuliusAnim():
animatedSprites = []
idleAnim = AnimatedSprite()
idleAnim.addSprite("sprites/player/right/julius-idle", 15)
animatedSprites.append(idleAnim)
walkAnimation = AnimatedSprite()
walkAnimation.addSprite("sprites/player/right/julius-walk", 16)
animatedSprites.append(walkAnimation)
duckAnim = AnimatedSprite()
duckAnim.addSprite("sprites/player/right/julius-duck", 8)
animatedSprites.append(duckAnim)
slideAnim = AnimatedSprite()
slideAnim.addSprite("sprites/player/right/julius-slide", 11)
animatedSprites.append(slideAnim)
fallAnim = AnimatedSprite()
fallAnim.addSprite("sprites/player/right/julius-fall", 10)
animatedSprites.append(fallAnim)
jumpAnim = AnimatedSprite()
jumpAnim.addSprite("sprites/player/right/julius-jump", 2)
animatedSprites.append(jumpAnim)
jumpStillAnim = AnimatedSprite()
jumpStillAnim.addSprite("sprites/player/right/julius-jumpstill", 2)
animatedSprites.append(jumpStillAnim)
attackAnim = AnimatedSprite()
attackAnim.addSprite("sprites/player/right/julius-attack", 7)
animatedSprites.append(attackAnim)
attackAirAnim = AnimatedSprite()
attackAirAnim.addSprite("sprites/player/right/julius-attackair", 7)
animatedSprites.append(attackAirAnim)
lockAttackAnim = False
@staticmethod
def idling(direction):
if (Player.still and
Player.standing and
not Player.jumping and
not Player.freeFalling and
not Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def walking(direction):
if (not Player.still and
Player.standing and
not Player.jumping and
not Player.freeFalling and
not Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def ducking(direction):
if (not Player.standing and
not Player.sliding and
not Player.jumping and
not Player.freeFalling and
not Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def sliding(direction):
if (not Player.standing and
Player.sliding and
not Player.jumping and
not Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def jumpingStill(direction):
if (Player.standing and
Player.still and
not Player.sliding and
Player.jumping and
not Player.falling and
not Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def jumpingMoving(direction):
if (Player.standing and
not Player.still and
not Player.sliding and
Player.jumping and
not Player.freeFalling and
not Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def falling(direction):
if (Player.standing and
not Player.sliding and
not Player.jumping and
Player.freeFalling and
not Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def attackingGround(direction):
if (Player.standing and
not Player.sliding and
Player.grounded and
Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def attackingAir(direction):
if (Player.standing and
not Player.sliding and
not Player.grounded and
Player.attacking and
Player.direction == direction):
return True
else:
return False
@staticmethod
def animationController():
if (JuliusAnim.idling(2)):
JuliusAnim.idleAnim.playAnimation(Player.sprite, 15, JuliusAnim.animatedSprites)
# JuliusAnim.idle1_right = False
elif (JuliusAnim.idling(1)):
JuliusAnim.idleAnim.playAnimationFlipped(Player.sprite, 15, JuliusAnim.animatedSprites)
# JuliusAnim.idle1_left = False
elif (JuliusAnim.walking(2)):
JuliusAnim.walkAnimation.playAnimation(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.walking(1)):
JuliusAnim.walkAnimation.playAnimationFlipped(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.ducking(2)):
JuliusAnim.duckAnim.playAnimation(Player.sprite, 14, JuliusAnim.animatedSprites, False)
elif (JuliusAnim.ducking(1)):
JuliusAnim.duckAnim.playAnimationFlipped(Player.sprite, 14, JuliusAnim.animatedSprites, False)
elif (JuliusAnim.sliding(2)):
JuliusAnim.slideAnim.playAnimation(Player.sprite, 24, JuliusAnim.animatedSprites, False)
elif (JuliusAnim.sliding(1)):
JuliusAnim.slideAnim.playAnimationFlipped(Player.sprite, 24, JuliusAnim.animatedSprites, False)
elif (JuliusAnim.jumpingStill(2)):
JuliusAnim.jumpStillAnim.playAnimation(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.jumpingStill(1)):
JuliusAnim.jumpStillAnim.playAnimationFlipped(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.jumpingMoving(2)):
JuliusAnim.jumpAnim.playAnimation(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.jumpingMoving(1)):
JuliusAnim.jumpAnim.playAnimationFlipped(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.falling(2)):
JuliusAnim.fallAnim.playAnimation(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.falling(1)):
JuliusAnim.fallAnim.playAnimationFlipped(Player.sprite, 20, JuliusAnim.animatedSprites)
elif (JuliusAnim.attackingGround(2) and not JuliusAnim.lockAttackAnim):
JuliusAnim.attackAnim.playAnimation(Player.sprite, 20, JuliusAnim.animatedSprites)
JuliusAnim.lockAttackAnim = True
elif (JuliusAnim.attackingGround(1) and not JuliusAnim.lockAttackAnim):
JuliusAnim.attackAnim.playAnimationFlipped(Player.sprite, 20, JuliusAnim.animatedSprites)
JuliusAnim.lockAttackAnim = True
elif (JuliusAnim.attackingAir(2)) and not JuliusAnim.lockAttackAnim:
JuliusAnim.attackAirAnim.playAnimation(Player.sprite, 20, JuliusAnim.animatedSprites)
JuliusAnim.lockAttackAnim = True
elif (JuliusAnim.attackingAir(1) and not JuliusAnim.lockAttackAnim):
JuliusAnim.attackAirAnim.playAnimationFlipped(Player.sprite, 20, JuliusAnim.animatedSprites)
JuliusAnim.lockAttackAnim = True
else:
for animation in JuliusAnim.animatedSprites:
if(animation.active):
animation.playAnimation(Player.sprite, 20, JuliusAnim.animatedSprites)
elif(animation.activeFlipped):
animation.playAnimationFlipped(Player.sprite, 20, JuliusAnim.animatedSprites)