-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplatformer.py
1363 lines (1154 loc) · 49.4 KB
/
platformer.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from settings import *
from game_clock import *
from pygame import *
from level_class import *
import sys
import level_data_intro as levels_intro
import level_data_dad as levels_dad
import level_data_cora as levels_cora
import level_data_intermediate as levels_intermediate
import level_data_expert as levels_expert
import time
WIN_WIDTH = 800
WIN_HEIGHT = 640
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)
DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
DEPTH = 32
FLAGS = 0
CAMERA_SLACK_X = int(WIN_WIDTH / 5)
CAMERA_SLACK_Y = int(WIN_HEIGHT / 5)
def my_print(message):
if False:
print message
def main():
global DISPLAYSURF, TIMER, levels, screen, SOUND_LIFE, SOUND_BOUNCE_LARGE, SOUND_BOUNCE_MED
global SOUND_HURT2, SOUND_HURT1, SOUND_DIE, SOUND_PIT_DIE, SOUND_JUMP, SOUND_PORTAL
global SOUND_STICKY_ON, SOUND_STICKY_MOVE, SOUND_STICKY_OFF, SOUND_BOUNCE_SMALL
pygame.init()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
pygame.display.set_caption("Little Square, Long Journey! Producers: Link, Mark")
TIMER = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
SOUND_LIFE = pygame.mixer.Sound(SOUND_LIFE_FILE)
SOUND_STICKY_MOVE = pygame.mixer.Sound(SOUND_STICKY_MOVE_FILE)
SOUND_STICKY_MOVE.set_volume(0.3)
SOUND_STICKY_ON = pygame.mixer.Sound(SOUND_STICKY_ON_FILE)
SOUND_STICKY_OFF = pygame.mixer.Sound(SOUND_STICKY_OFF_FILE)
SOUND_HURT1 = pygame.mixer.Sound(SOUND_HURT_FILE1)
SOUND_HURT2 = pygame.mixer.Sound(SOUND_HURT_FILE2)
SOUND_BOUNCE_LARGE = pygame.mixer.Sound(SOUND_BOUNCE_LARGE_FILE)
SOUND_BOUNCE_MED = pygame.mixer.Sound(SOUND_BOUNCE_MEDIUM_FILE)
SOUND_BOUNCE_SMALL = pygame.mixer.Sound(SOUND_BOUNCE_SMALL_FILE)
SOUND_BOUNCE_SMALL.set_volume(0.5)
SOUND_JUMP = pygame.mixer.Sound(SOUND_JUMP_FILE)
SOUND_JUMP.set_volume(0.5)
SOUND_DIE = pygame.mixer.Sound(SOUND_DIE_FILE)
SOUND_PIT_DIE = pygame.mixer.Sound(SOUND_PIT_DIE_FILE)
SOUND_PORTAL = pygame.mixer.Sound(SOUND_PORTAL_FILE)
# TODO show_start_screen
instructions_message()
while True:
levels = level_pack_choice_screen()
start_level_num = level_num_choice_screen(levels)
run_game(levels, start_level_num)
def run_game(levels, start_level_num):
global cameraX, cameraY
global message_life, message_life2
global DISPLAYSURF, BASICFONT
start_the_clock()
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
player = Player(32, 32)
num_levels = len(levels)
for level_number in range(start_level_num, num_levels):
global bg, message_prompt, message_end
player.finished_level = False
player.reset_position(32, 32)
player.last_hurt_time = current_time() - 10.0
level = levels[level_number]
level_layout = level.layout
level_width = len(level_layout[0]) * 32
level_height = len(level_layout) * 32
up = down = left = right = False
bg = Surface((32, 32))
bg.convert()
bg.fill(Color("#000032"))
entities = pygame.sprite.Group()
platforms = []
start_message = 'Level {}: {}'.format(level_number + 1, level.name)
info_message = level.message
message_prompt = 'Press any arrow or space to continue'
message_life2 = 'lives = {}'.format(player.lives - 1)
x = y = 0
cameraX = cameraY = 0
message_life = ""
# build the level
for row in level_layout:
for col in row:
if col == "P":
p = Platform(x, y)
platforms.append(p)
entities.add(p)
if col == "D":
d = PlatformPit(x, y)
platforms.append(d)
entities.add(d)
if col == "H":
h = PlatformHurtFull(x, y)
platforms.append(h)
entities.add(h)
if col == "h":
h = PlatformHurt(x, y)
platforms.append(h)
entities.add(h)
if col == "L":
l = PlatformLife(x, y)
platforms.append(l)
entities.add(l)
if col == "B":
b = PlatformBouncy1(x, y)
platforms.append(b)
entities.add(b)
if col == "S":
s = PlatformSticky(x, y)
platforms.append(s)
entities.add(s)
if col == "M":
m = Platformmovingcarpetright(x, y)
platforms.append(m)
entities.add(m)
if col == "I":
i = PlatformIllusion(x, y)
platforms.append(i)
entities.add(i)
if col == "G":
g = PlatformGlass(x, y)
platforms.append(g)
entities.add(g)
if col == "U":
u = PlatformFly(x, y)
platforms.append(u)
entities.add(u)
if col == "W":
w = Platformmovingcarpetleft(x, y)
platforms.append(w)
entities.add(w)
if col == "F":
f = FakeExitBlock(x, y)
platforms.append(f)
entities.add(f)
if col == "E":
e = ExitBlock(x, y)
platforms.append(e)
entities.add(e)
x += 32
y += 32
x = 0
entities.add(player)
# print "Level {}".format(level_number + 1)
# start interactive part of level
player.running_level = False
while not player.finished_level:
TIMER.tick(60)
for e in pygame.event.get():
if e.type == QUIT: raise SystemExit, "QUIT"
if e.type == KEYDOWN and e.key == K_ESCAPE:
raise SystemExit, "ESCAPE"
if e.type == KEYDOWN and e.key == K_F5:
return
if e.type == KEYDOWN and (e.key == K_UP or e.key == K_SPACE):
up = True
player.running_level = True
if player.up_was_released:
player.last_up_time = current_time()
if e.type == KEYDOWN and e.key == K_DOWN:
down = True
player.running_level = True
if e.type == KEYDOWN and e.key == K_LEFT:
left = True
player.running_level = True
if e.type == KEYDOWN and e.key == K_RIGHT:
right = True
player.running_level = True
if e.type == KEYUP and (e.key == K_UP or e.key == K_SPACE):
up = False
if e.type == KEYUP and e.key == K_DOWN:
down = False
if e.type == KEYUP and e.key == K_RIGHT:
right = False
if e.type == KEYUP and e.key == K_LEFT:
left = False
if e.type == KEYUP and e.key == K_RIGHT:
right = False
# draw background
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
# update player, draw everything else
if player.running_level:
player.update(up, down, left, right, platforms, level_width, level_height)
if player.hp <= 0:
SOUND_DIE.play()
player.lives -= 1
player.hp = NUM_HP
up = left = right = down = False
die_message("Ouch, you lost a life", screen, TIMER, bg)
my_print("Died: lives left = {}".format(player.lives))
player.reset_position(32, 32)
player.running_level = False
pygame.event.get() # clear out event queue
if player.lives <= 0:
end_game_message(screen, TIMER, bg)
my_print("No lives left. Game over!")
pygame.event.get() # clear out event queue
return
# camera movement
for entity in entities:
if isinstance(entity, Platform):
entity.update(cameraX, cameraY)
# redraw entities
entities.draw(screen)
# draw text
if not player.running_level:
SOUND_LIFE.stop()
SOUND_STICKY_MOVE.stop()
SOUND_STICKY_ON.stop()
SOUND_STICKY_OFF.stop()
SOUND_HURT1.stop()
SOUND_HURT2.stop()
SOUND_BOUNCE_SMALL.stop()
SOUND_BOUNCE_MED.stop()
SOUND_BOUNCE_LARGE.stop()
SOUND_JUMP.stop()
SOUND_DIE.stop()
draw_big_message(start_message, color=BLUE, pulse_time=1.0)
draw_small_message(info_message, color=BLUE, pulse_time=1.0)
draw_prompt_message(message_prompt, color=BLUE, pulse_time=1.5)
level_start_time = current_time()
message_time = 'Time = {:3.1f}'.format(elapsed_time(level_start_time))
message_life2 = 'lives = {}'.format(player.lives - 1)
message_life = 'lives = {}'.format(player.lives)
draw_life_message(message_life, color=BLUE, pulse_time=1.5)
draw_hp(player.hp)
draw_time_message(message_time, color=RED, pulse_time=1.5)
pygame.display.update()
# Add the time spent on the just-finished level
this_level_time = elapsed_time(level_start_time)
get_one_up = this_level_time < level.fast_time
player.all_previous_level_times = player.all_previous_level_times + this_level_time
print "Finished level {}, total playing time = {:3.1f}".format(level_number + 1, player.all_previous_level_times)
message_big = "Finished level: time = {:3.1f}".format(this_level_time)
message_small = ["total time = {:3.1f}".format(player.all_previous_level_times),
"press spacebar to continue..."]
if get_one_up:
player.lives += 1
message_big += ' Fast! 1 UP!!'
end_message(message_life, message_big, message_small, TIMER, screen, bg, player.hp)
if level_number + 1 == num_levels:
print "You win!!!!!!"
message_big = "You WIN!!!!!!!"
message_small = ["total time = {:3.1f}".format(player.all_previous_level_times),
"playing {} out of {} levels".format(num_levels-start_level_num, num_levels),
"press spacebar to play again!",
"press escape to terminate the game."]
end_message(message_life, message_big, message_small, TIMER, screen, bg, player.hp)
return
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.player_size = 30
self.image = Surface((self.player_size, self.player_size))
self.image.fill((0, 225, 0))
self.image.convert()
self.rect = Rect(x, y, self.player_size, self.player_size)
self.last_hurt_time = current_time() - 10.0
self.last_fly_time = 0
self.lives = NUM_LIVES
self.hp = NUM_HP
self.running_level = False
self.finished_level = False
self.all_previous_level_times = 0
self.xvel = 0
self.yvel = 0
self.onGround = False
self.onFly = False
self.onSticky = False
self.onIce = False
self.groundSpeed = 0
self.is_jumping = False
self.is_flying = False
self.last_up_time = -10.0
self.last_bounce_time = -10.0
self.up_was_released = True
def reset_position(self, x, y):
global cameraX, cameraY
cameraX = cameraY = 0
self.xvel = 0
self.yvel = 0
self.onGround = False
self.onFly = False
self.onIce = False
self.groundSpeed = 0
self.last_up_time = -10.0
self.last_bounce_time = -10.0
self.rect = Rect(x, y, self.player_size, self.player_size)
self.last_hurt_time = current_time() - 10.0
self.last_fly_time = 0
self.image.fill((0, 225, 0))
def add_jump_boost(self):
# A fresh jump will add some speed up to a point
self.yvel = min(self.yvel, max(-18.0, self.yvel - 3.0))
def update(self, up, down, left, right, platforms, level_width, level_height):
global cameraX, cameraY
if up:
# only jump if on the ground
if self.onSticky:
self.is_jumping = True # This is counter intuitive, but allows the slide up off the stickies.
self.yvel = self.yvel - 0.65
# SOUND_STICKY_MOVE.play()
elif (self.onGround or self.is_flying) and self.up_was_released:
# start a new jump
self.is_jumping = True
self.up_was_released = False
self.yvel -= 10.2
SOUND_JUMP.play()
elif not self.onGround and self.up_was_released and elapsed_time(self.last_bounce_time) < UP_JUMP_TIME:
self.add_jump_boost()
self.is_jumping = True
self.up_was_released = False
else:
self.is_jumping = False
self.up_was_released = True
if down:
if self.onSticky:
self.yvel = self.yvel + 0.65
self.onSticky = False
if left:
if self.xvel > 0.0:
if self.onGround and not self.onIce:
# self.xvel = 0.0
self.xvel = self.xvel * 0.4 # extra help changing direction on ground
else:
self.xvel = self.xvel - 0.3 # extra help changing direction in air
if self.onGround:
self.xvel = self.xvel - 0.65
else:
self.xvel = self.xvel - 0.4
if right:
if self.xvel < 0.0:
if self.onGround and not self.onIce:
# self.xvel = 0.0
self.xvel = self.xvel * 0.3 # extra help changing direction on ground
else:
self.xvel = self.xvel + 0.3 # extra help changing direction in air
if self.onGround:
self.xvel = self.xvel + 0.65
else:
self.xvel = self.xvel + 0.4
# if not self.onGround and not self.onSticky:
if not self.onSticky:
# only accelerate with gravity if in the air
self.yvel += GRAVITY
if not self.is_jumping and self.yvel < 0:
# This allows you to have big and little jumps depending on how long you hold up.
self.yvel += 1.3*GRAVITY
# max falling speed
if self.yvel > 90:
self.yvel = 90
if self.onGround and not(left or right) and not self.onIce:
# this will let you change direction quickly but not immediately
self.xvel = self.xvel*.2
if abs(self.xvel) < 0.2:
self.xvel = 0.
# only move up or down on stickies when you press up or down.
if self.onSticky and not(up or down):
self.yvel = 0
# give the player a little help... let them fight the groundspeed if they jump
# if they are actively pushing in the opposite direction of the carpet, lessen the effect of the carpet
# by shrinking the groundspeed
if not self.onGround and cmp(self.xvel, 0) != cmp(self.groundSpeed, 0) and (left or right):
self.groundSpeed *= 0.975
# increment in x direction
self.rect.left += self.xvel + self.groundSpeed
# reset things that are set in collision detection
last_onSticky = self.onSticky
self.onSticky = False
if elapsed_time(self.last_fly_time) > FLY_TIME:
self.is_flying = False
# do x-axis collisions
self.collide(self.xvel + self.groundSpeed, 0, platforms)
# limit horizontal speed
if self.xvel < -8.0:
self.xvel = -8.0
if self.xvel > 8.0:
self.xvel = 8.0
# increment/move in y direction
if self.yvel > 0:
# This makes sure that gravity (when onGround) will move player enough downward to ensure it
# goes down at least 1 pixel so that collisions will happen and set OnGround appropriately again after
# the reset a few lines below.
self.rect.top = self.rect.top + max(1.0, self.yvel)
else:
self.rect.top = self.rect.top + self.yvel
# assuming we're in the air
if not self.onSticky:
self.onGround = False
# do y-axis collisions
self.collide(0, self.yvel, platforms)
# Limit Vertical Speed
if self.onSticky:
if self.yvel < -5.0:
self.yvel = -5.0
if self.yvel > 5.0:
self.yvel = 5.0
if not last_onSticky and self.onSticky:
SOUND_STICKY_ON.play()
if last_onSticky and not self.onSticky:
SOUND_STICKY_OFF.play()
if self.onSticky and (abs(self.yvel) + abs(self.xvel)) > 2.0:
SOUND_STICKY_MOVE.play()
else:
SOUND_STICKY_MOVE.stop()
# calculate new x camera
old_cameraX = cameraX
if (cameraX + HALF_WIDTH) - (self.rect.centerx + cameraX) > CAMERA_SLACK_X:
cameraX = (self.rect.centerx + cameraX) + CAMERA_SLACK_X - HALF_WIDTH
elif (self.rect.centerx + cameraX) - (cameraX + HALF_WIDTH) > CAMERA_SLACK_X:
cameraX = (self.rect.centerx + cameraX) - CAMERA_SLACK_X - HALF_WIDTH
if cameraX < 0:
cameraX = 0
if cameraX > level_width - WIN_WIDTH:
cameraX = level_width - WIN_WIDTH
# calculate new camera y
old_cameraY = cameraY
if (cameraY + HALF_HEIGHT) - (self.rect.centery + cameraY) > CAMERA_SLACK_Y:
cameraY = (self.rect.centery + cameraY) + CAMERA_SLACK_Y - HALF_HEIGHT
elif (self.rect.centery + cameraY) - (cameraY + HALF_HEIGHT) > CAMERA_SLACK_Y:
cameraY = (self.rect.centery + cameraY) - CAMERA_SLACK_Y - HALF_HEIGHT
if cameraY < 0:
cameraY = 0
if cameraY > level_height - WIN_HEIGHT:
cameraY = level_height - WIN_HEIGHT
# update player, based on camera movement.
self.rect.centerx = self.rect.centerx - (cameraX - old_cameraX)
self.rect.centery = self.rect.centery - (cameraY - old_cameraY)
# making you turn red after hurt
if elapsed_time(self.last_hurt_time) < BANDAID_TIME:
self.image.fill((255, 0, 0, 5))
elif self.is_flying:
self.image.fill(DARKGREEN)
else:
self.image.fill((0, 225, 0))
def bouncy_sound(self, vel):
if abs(vel) >= 15.0:
SOUND_BOUNCE_LARGE.play()
if 8.0 <= abs(vel) < 15.0:
SOUND_BOUNCE_MED.play()
if 3.0 < abs(vel) < 8.0:
SOUND_BOUNCE_SMALL.play()
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
# Handle collision with an Exit Block
if isinstance(p, ExitBlock):
SOUND_PORTAL.play()
self.finished_level = True
if isinstance(p, FakeExitBlock):
self.groundSpeed = 0
self.reset_position(32, 32)
self.cameraX = self.cameraY = 0
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel < 0:
self.rect.top = p.rect.bottom
if yvel > 0:
self.rect.bottom = p.rect.top
self.yvel = 0
if isinstance(p, PlatformGlass):
self.groundSpeed = 0
self.onIce = True
self.onGround = True
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 0
if yvel > 0:
self.rect.bottom = p.rect.top
self.yvel = 0
if isinstance(p, PlatformFly):
self.groundSpeed = 0
self.is_flying = True
self.last_fly_time = current_time()
if xvel > 0:
self.rect.right = p.rect.left
self.xvel = 0
if xvel < 0:
self.rect.left = p.rect.right
self.xvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 0
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
# Handle collision with an Bouncy Block
elif isinstance(p, PlatformBouncy1):
my_print("Bouncy: ")
if xvel > 0:
self.rect.right = p.rect.left
my_print("collide left")
self.xvel = -self.xvel
self.groundSpeed = -self.groundSpeed
self.bouncy_sound(self.xvel)
if xvel < 0:
self.rect.left = p.rect.right
my_print("collide right")
self.xvel = -self.xvel
self.groundSpeed = -self.groundSpeed
self.bouncy_sound(self.xvel)
if yvel < 0:
self.groundSpeed = 0
self.rect.top = p.rect.bottom
self.yvel = -self.yvel
my_print("collide bottom bounce off")
self.bouncy_sound(self.yvel)
if yvel > 0:
self.groundSpeed = 0
self.rect.bottom = p.rect.top
my_print('Bounce start yvel {}'.format(self.yvel))
# lose a bit of energy
self.yvel = -max(0.0, self.yvel - 1.45 * GRAVITY) * 0.85
if elapsed_time(self.last_up_time) < UP_JUMP_TIME:
self.add_jump_boost()
else:
self.last_bounce_time = current_time()
if self.yvel > -3*GRAVITY:
self.yvel = 0.0
self.is_jumping = False
self.onGround = True
# self.up_was_released = True
else:
self.is_jumping = True
self.up_was_released = False
self.onGround = False
self.bouncy_sound(self.yvel)
my_print("collide top bounce off")
elif isinstance(p, PlatformIllusion):
my_print("Illusion: ")
if xvel > 0:
pass
if xvel < 0:
pass
if yvel < 0:
pass
if yvel > 0:
pass
elif isinstance(p, PlatformHurt):
self.is_flying = False
self.last_fly_time = 0
SOUND_HURT1.stop()
SOUND_HURT2.stop()
SOUND_LIFE.stop()
self.groundSpeed = 0
if self.hp == 3:
SOUND_HURT1.play()
if self.hp == 2:
SOUND_HURT2.play()
if elapsed_time(self.last_hurt_time) > BANDAID_TIME:
self.hp -= 1
self.last_hurt_time = current_time()
if xvel > 0:
self.rect.right = p.rect.left
self.xvel = -6.0
my_print("collide left")
if xvel < 0:
self.rect.left = p.rect.right
self.xvel = 6.0
my_print("collide right")
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 6.0
my_print("collide bottom")
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = False
self.yvel = -6.0
self.last_bounce_time = current_time()
my_print("collide top")
elif isinstance(p, PlatformHurtFull):
self.is_flying = False
self.last_fly_time = 0
SOUND_HURT1.stop()
SOUND_HURT2.stop()
SOUND_LIFE.stop()
self.groundSpeed = 0
if self.hp == 3:
SOUND_HURT1.play()
if self.hp == 2:
SOUND_HURT2.play()
if elapsed_time(self.last_hurt_time) > BANDAID_TIME:
self.hp -= 1
self.last_hurt_time = current_time()
if xvel > 0:
self.rect.right = p.rect.left
my_print("collide left")
if xvel < 0:
self.rect.left = p.rect.right
my_print("collide right")
if yvel < 0:
self.rect.top = p.rect.bottom
my_print("collide bottom")
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = False
self.yvel = -6.0
my_print("collide top")
elif isinstance(p, PlatformPit):
self.lives -= 1
self.hp = NUM_HP
self.groundSpeed = 0
self.reset_position(32, 32)
self.running_level = False
SOUND_PIT_DIE.play()
elif isinstance(p, PlatformLife):
self.groundSpeed = 0
self.last_hurt_time = current_time() - 10.0
if self.hp < NUM_HP:
SOUND_HURT1.stop()
SOUND_HURT2.stop()
SOUND_LIFE.stop()
SOUND_LIFE.play()
self.hp = NUM_HP
my_print("Healed: HP = {}".format(self.hp))
if xvel > 0:
self.rect.right = p.rect.left
self.xvel = 0
if xvel < 0:
self.rect.left = p.rect.right
self.xvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 0
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
elif isinstance(p, Platformmovingcarpetleft):
my_print("Left carpet: ")
if xvel > 0:
self.rect.right = p.rect.left
self.xvel = 0
my_print("collide right")
self.groundSpeed = 0
if xvel < 0:
self.rect.left = p.rect.right
self.xvel = 0
my_print("collide left")
self.groundSpeed = 0
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 0
my_print("collide bottom")
self.groundSpeed = 0
if yvel > 0:
self.onGround = True
self.yvel = 0
self.rect.bottom = p.rect.top
self.groundSpeed = -7
my_print("collide top")
elif isinstance(p, Platformmovingcarpetright):
my_print("Right carpet: ")
if xvel > 0:
self.rect.right = p.rect.left
self.xvel = 0
my_print("collide right")
self.groundSpeed = 0
if xvel < 0:
self.rect.left = p.rect.right
self.xvel = 0
my_print("collide left")
self.groundSpeed = 0
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 0
my_print("collide bottom")
self.groundSpeed = 0
if yvel > 0:
self.onGround = True
self.yvel = 0
self.rect.bottom = p.rect.top
self.groundSpeed = 7
my_print("collide top")
elif isinstance(p, PlatformSticky):
my_print("Sticky: ")
self.groundSpeed = 0
self.onGround = True
self.onSticky = True
# See if only collided/overlapped by 1
if self.rect.right == p.rect.left + 1 or self.rect.left == p.rect.right - 1 \
or self.rect.top == p.rect.bottom - 1 or self.rect.bottom == p.rect.top + 1:
pass
# Determine if we are on the top or, say, a side. Only allow side movement when checking
# collisions based on the yvel (not when checking horizontal)
# if self.rect.bottom != p.rect.top + 1 and yvel != 0.0:
# self.yvel = 0.0
# if xvel != 0 and self.rect.top == p.rect.bottom - 1:
# print "Hit the clause"
else:
if xvel > 0:
self.rect.right = p.rect.left + 1
my_print("collide right")
# self.yvel = 0.0
self.xvel = 0.0
if xvel < 0:
self.rect.left = p.rect.right - 1
my_print("collide left")
# self.yvel = 0.0
self.xvel = 0.0
if yvel < 0:
self.rect.top = p.rect.bottom - 1
self.yvel = 0.0
my_print("collide bottom")
if yvel > 0:
self.rect.bottom = p.rect.top + 1
self.yvel = 0.0
my_print("collide top")
else: # Must be a normal platform
my_print("Platform: ")
self.groundSpeed = 0
if xvel > 0:
self.rect.right = p.rect.left
self.xvel = 0
my_print("collide right")
if xvel < 0:
self.rect.left = p.rect.right
self.xvel = 0
my_print("collide left")
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 0
my_print("collide top")
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
class Platform(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color("#888888"))
self.rect = Rect(x, y, 32, 32)
self.x = x
self.y = y
def update(self, camera_x, camera_y):
self.rect.x = self.x - camera_x
self.rect.y = self.y - camera_y
class ExitBlock(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.color = Color("#DD33FF")
self.other_colors = [RED, YELLOW, LIMEGREEN, BLUE]
self.image.fill(Color("#DD33FF"))
self.x = x
self.y = y
def update(self, camera_x, camera_y):
self.rect.x = self.x - camera_x
self.rect.y = self.y - camera_y
# CHANGE pulse time to change width of color band. Change pulse start time to change speed of transition.
use_color = get_pulse_color([self.color] + self.other_colors, pulse_time=0.5, pulse_start_time=-self.x / 64)
self.image.fill(Color(*use_color))
class FakeExitBlock(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.color = Color("#DD33FF")
self.other_colors = [DARKRED, BYELLOW, DARKGREEN]
self.image.fill(Color("#DD33FF"))
self.x = x
self.y = y
def update(self, camera_x, camera_y):
self.rect.x = self.x - camera_x
self.rect.y = self.y - camera_y
# CHANGE pulse time to change width of color band. Change pulse start time to change speed of transition.
use_color = get_pulse_color([self.color] + self.other_colors, pulse_time=1.0, pulse_start_time=-self.x / 64)
self.image.fill(Color(*use_color))
class PlatformIllusion(Platform):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color("#898989"))
self.rect = Rect(x, y, 32, 32)
self.x = x
self.y = y
class PlatformGlass(Platform):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(LIGHTBLUE)
self.rect = Rect(x, y, 32, 32)
self.x = x
self.y = y
class PlatformFly(Platform):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(DARKGREEN)
self.rect = Rect(x, y, 32, 32)
self.x = x
self.y = y
class PlatformBouncy1(Platform):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color("#5533FF"))
self.rect = Rect(x, y, 32, 32)
self.x = x
self.y = y
class PlatformHurt(Platform):
def __init__(self, x, y):
dip_height = 10
Entity.__init__(self)
self.color = RED
self.other_color = DARKERRED
self.image = Surface((32, 32 - dip_height))
self.image.convert()
self.image.fill(Color(*self.color))
self.rect = Rect(x, y + dip_height, 32, 32 - dip_height)
self.x = x
self.y = y + dip_height
def update(self, camera_x, camera_y):
self.rect.x = self.x - camera_x
self.rect.y = self.y - camera_y
# CHANGE pulse time to change width of color band. Change pulse start time to change speed of transition.
use_color = get_pulse_color([self.color, self.other_color], pulse_time=4.0, pulse_start_time=self.y / 128.0)
self.image.fill(Color(*use_color))
class PlatformHurtFull(PlatformHurt):
def __init__(self, x, y):
self.color = RED
self.other_color = DARKERRED
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color("#FF0000"))
self.rect = Rect(x, y, 32, 32)
self.x = x
self.y = y
class PlatformPit(Platform):
def __init__(self, x, y):
dip_height = 8
Entity.__init__(self)
self.image = Surface((32, 32 - dip_height))
self.image.convert()
self.image.fill(Color(0, 0, 0))
self.rect = Rect(x, y + dip_height, 32, 32 - dip_height)
self.x = x
self.y = y + dip_height
class PlatformLife(Platform):
def __init__(self, x, y):
Entity.__init__(self)
self.color = LIFECOLOR
self.other_color = BYELLOW
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color(*self.color))
self.rect = Rect(x, y, 32, 32)
self.x = x
self.y = y
def update(self, camera_x, camera_y):
self.rect.x = self.x - camera_x
self.rect.y = self.y - camera_y