-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player FunctionalCollision but not perfect.py
582 lines (452 loc) · 19.3 KB
/
Player FunctionalCollision but not perfect.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
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
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
sprite = Sprite("sprites/player/right/julius-idle/1.png")
sprite.set_sequence_time(0, 14, 80, True)
levelGroundFloor = 465
sprite.set_position(200, 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 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.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
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.setAnims2()
if (Player.groundLevelX >= 5):
Player.sprite.x -= 2000 * GameWindow.window.delta_time()
if (Player.groundLevelX <= - 5):
Player.sprite.x += 2000 * GameWindow.window.delta_time()
## WALKING
if(not Player.still and Player.direction == 2 and Player.standing):
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):
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(not Player.sprite.collided_perfect(Level1area1.tiles)):
Player.groundLevelX = 0
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.setGravity()
@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 >= 1):
Player.freeFalling = True
Player.freeFallingTimer.stopTimer()
Player.freeFallingTimer.resetTimer()
else:
Player.freeFalling = False
if(not Player.still and Player.grounded and Player.sprite.collided_perfect(Level1area1.tiles) and Player.groundLevelY < 5):
Player.sprite.y -= 2000 * GameWindow.window.delta_time()
Player.groundLevelY += 4000 * GameWindow.window.delta_time()
else:
Player.groundLevelY = 0
"""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
if((not Level1area1.tiles.collided_perfect(Player.sprite) and not Player.jumping) or Player.groundLevelY > 5):
Player.sprite.y += Player.fallSpeed * GameWindow.window.delta_time()
Player.fallSpeed += 800 * GameWindow.window.delta_time()
Player.falling = True
else:
Player.fallSpeed = Player.tempFallSpeed
Player.falling = False
if(Player.falling or Player.jumping):
Player.grounded = False
else:
Player.grounded = True
class JuliusAnim():
animPaths = {
"idle1_right": "sprites/player/right/julius-idle1-right.png",
"idle1_left": Misc.strDirectionRtoL("sprites/player/right/julius-idle1-right.png"),
"walk_right": "sprites/player/right/julius-walk-right.png",
"walk_left": Misc.strDirectionRtoL("sprites/player/right/julius-walk-right.png"),
"duck_right": "sprites/player/right/julius-duckstart-right2.png",
"duck_left": Misc.strDirectionRtoL("sprites/player/right/julius-duckstart-right2.png"),
"duckslide_right": "sprites/player/right/julius-duckslide-right2.png",
"duckslide_left": Misc.strDirectionRtoL("sprites/player/right/julius-duckslide-right2.png"),
"jumpmid_right": "sprites/player/right/julius-jumpmid-right.png",
"jumpmid_left": Misc.strDirectionRtoL("sprites/player/right/julius-jumpmid-right.png"),
"jumpmidstill_right": "sprites/player/right/julius-jumpmid-still-right.png",
"jumpmidstill_left": Misc.strDirectionRtoL("sprites/player/right/julius-jumpmid-still-right.png"),
"fall_right": "sprites/player/right/julius-fallstart-right.png",
"fall_left": Misc.strDirectionRtoL("sprites/player/right/julius-fallstart-right.png"),
"attack_right": "sprites/player/right/julius-attack-right.png",
"attack_left": Misc.strDirectionRtoL("sprites/player/right/julius-attack-right.png"),
"attackair_right": "sprites/player/right/julius-attackair-right.png",
"attackair_left": Misc.strDirectionRtoL("sprites/player/right/julius-attackair-right.png"),
}
anims = {
"idle1_right": False,
"idle1_right_animChanged": False,
"idle1_left": False,
"idle1_left_animChanged": False,
"walkstart_right": False,
"walkstart_right_animChanged": False,
"walkstart_left": False,
"walkstart_left_animChanged": False,
"walk_right": False,
"walk_right_animChanged": False,
"walk_left": False,
"walk_left_animChanged": False,
"duck_right": False,
"duck_right_animChanged": False,
"duck_left": False,
"duck_left_animChanged": False,
"duckslide_right": False,
"duckslide_right_animChanged": False,
"duckslide_left": False,
"duckslide_left_animChanged": False,
"jumpmid_right": False,
"jumpmid_right_animChanged": False,
"jumpmid_left": False,
"jumpmid_left_animChanged": False,
"jumpmidstill_right": False,
"jumpmidstill_right_animChanged": False,
"jumpmidstill_left": False,
"jumpmidstill_left_animChanged": False,
"fall_right": False,
"fall_right_animChanged": False,
"fall_left": False,
"fall_left_animChanged": False,
"attack_right": False,
"attack_right_animChanged": False,
"attack_left": False,
"attack_left_animChanged": False,
"attackair_right": False,
"attackair_right_animChanged": False,
"attackair_left": False,
"attackair_left_animChanged": False,
}
animLists = {
"idle1_right": [],
"idle1_right_animChanged": False,
"idle1_left": [],
"idle1_left_animChanged": False,
}
idleAnim = AnimatedSprite()
idleAnim.addSprite("sprites/player/right/julius-idle", 15)
walkAnimation = AnimatedSprite()
walkAnimation.addSprite("sprites/player/right/julius-walk", 16)
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.freeFalling 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
timeElapsed = 0
currentX = 0
currentY = 0
@staticmethod
def changeAnim():
pass
@staticmethod
def setAnims2():
if (JuliusAnim.idling(2)):
JuliusAnim.idleAnim.playAnimation(Player.sprite, 15)
# JuliusAnim.idle1_right = False
elif (JuliusAnim.idling(1)):
JuliusAnim.idleAnim.playAnimationFlipped(Player.sprite, 15)
# JuliusAnim.idle1_left = False
elif (JuliusAnim.walking(2)):
JuliusAnim.walkAnimation.playAnimation(Player.sprite, 20)
elif (JuliusAnim.walking(1)):
JuliusAnim.walkAnimation.playAnimationFlipped(Player.sprite, 20)
else:
JuliusAnim.idleAnim.playAnimation(Player.sprite, 15)
@staticmethod
def setAnims():
if (JuliusAnim.idling(2)):
JuliusAnim.changeAnimation("idle1_right", 15, 80)
# JuliusAnim.idle1_right = False
elif (JuliusAnim.idling(1)):
JuliusAnim.changeAnimation("idle1_left", 15, 80)
# JuliusAnim.idle1_left = False
elif (JuliusAnim.walking(2)):
JuliusAnim.walkAnim("right")
elif (JuliusAnim.walking(1)):
JuliusAnim.walkAnim("left")
JuliusAnim.timeElapsed += GameWindow.window.delta_time()
elif (JuliusAnim.ducking(2)):
JuliusAnim.changeAnimation("duck_right", 8, 70, False)
elif (JuliusAnim.ducking(1)):
JuliusAnim.changeAnimation("duck_left", 8, 70, False)
elif (JuliusAnim.sliding(2)):
JuliusAnim.changeAnimation("duckslide_right", 11, 30, False)
elif (JuliusAnim.sliding(1)):
JuliusAnim.changeAnimation("duckslide_left", 11, 30, False)
elif (JuliusAnim.jumpingStill(2)):
JuliusAnim.changeAnimation("jumpmidstill_right", 2, 70)
elif (JuliusAnim.jumpingStill(1)):
JuliusAnim.changeAnimation("jumpmidstill_left", 2, 70)
elif (JuliusAnim.jumpingMoving(2)):
JuliusAnim.changeAnimation("jumpmid_right", 2, 70)
elif (JuliusAnim.jumpingMoving(1)):
JuliusAnim.changeAnimation("jumpmid_left", 2, 70)
elif (JuliusAnim.falling(2)):
JuliusAnim.changeAnimation("fall_right", 10, 30, False)
elif (JuliusAnim.falling(1)):
JuliusAnim.changeAnimation("fall_left", 10, 30, False)
elif (JuliusAnim.attackingGround(2)):
if (not JuliusAnim.lockAttackAnim):
JuliusAnim.changeAnimation("attack_right", 7, 50, False)
JuliusAnim.lockAttackAnim = True
elif (JuliusAnim.attackingGround(1)):
if (not JuliusAnim.lockAttackAnim):
JuliusAnim.changeAnimation("attack_left", 7, 50, False)
JuliusAnim.lockAttackAnim = True
elif (JuliusAnim.attackingAir(2)):
if (not JuliusAnim.lockAttackAnim):
JuliusAnim.changeAnimation("attackair_right", 7, 50, False)
JuliusAnim.lockAttackAnim = True
elif (JuliusAnim.attackingAir(1)):
if (not JuliusAnim.lockAttackAnim):
JuliusAnim.changeAnimation("attackair_left", 7, 50, False)
JuliusAnim.lockAttackAnim = True
@staticmethod
def changeAnimation(animation, maxFrames, animTime=10, loop=True):
JuliusAnim.anims[animation] = True
if (JuliusAnim.anims[animation] and not JuliusAnim.anims[animation + "_animChanged"]):
currentX = Player.sprite.x
currentY = Player.sprite.y
if(maxFrames > 1):
Player.sprite = Sprite(JuliusAnim.animPaths[animation], maxFrames)
Player.sprite.set_sequence_time(0, maxFrames, animTime, loop)
else:
Player.sprite = Sprite(JuliusAnim.animPaths[animation])
Player.sprite.set_position(currentX, currentY)
JuliusAnim.anims[animation + "_animChanged"] = True
JuliusAnim.anims[animation] = False
for anim in JuliusAnim.anims:
if(anim != animation + "_animChanged"):
JuliusAnim.anims[anim] = False
@staticmethod
def walkAnim(directionStr):
timeElapsed = 0
JuliusAnim.changeAnimation("walk_" + directionStr, 16, 40)
JuliusAnim.changeAnimation("walk_" + directionStr, 16, 40)