This repository has been archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
952 lines (665 loc) · 24.9 KB
/
main.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
import math
import numpy
import pygame
from pygame.locals import *
import pygame_gui
import random
import sys
# Define some colors
RED = (255, 0, 0)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255,255,0)
WHITE = (255, 255, 255)
BRIGHTORANGE = (255, 174, 0)
SILVER = (192,192,192)
GRAY = (128,128,128)
MAROON = (128,0,0)
PURPLE = (128,0,128)
CORN_FLOWER_BLUE = (100,149,237)
DARK_GREEN = (8, 76, 8)
DARK_BLUE = (29, 80, 255)
is_paused = False
# initialize variables
# used to identify where to go when previous screen button pressed
curr_screen = None
prev_screen = None
# pygame initialization
pygame.init()
clock = pygame.time.Clock()
# set window + dock icon
display_height = 800
display_width = 800
display_size = (display_width, display_height)
display = pygame.display.set_mode(display_size, 0, 32)
manager = pygame_gui.UIManager(display_size, 'themes/button_themes.json')
dock_icon = pygame.image.load('images/dock_icon.png')
main_bg = pygame.image.load('images/bg_main.png')
pygame.display.set_caption("Fake Doodle Jump")
pygame.display.set_icon(dock_icon)
pygame.key.set_repeat(10, 10)
# set background
display.fill(GRAY)
# creates surface with same size as window - draw/create shapes on it
background = pygame.Surface(display_size)
display.blit(background, (0, 0))
pygame.display.update()
class MasterSprite(pygame.sprite.Sprite):
def __init__(self, image, x = 0, y = 0):
self.x = x
self.y = y
self.image = pygame.image.load(image)
self.rect = self.image.get_bounding_rect()
self.rect.x = x
self.rect.y = y
self.display_size = pygame.display.get_surface().get_size()
def move_right(self, pixels):
self.x += pixels
self.rect.x += pixels
def move_left(self, pixels):
self.x -= pixels
self.rect.x -= pixels
def move_up(self, pixels):
self.y -= pixels
self.rect.y -= pixels
def move_down(self, pixels):
self.y += pixels
self.rect.y += pixels
def get_rect(self):
return self.rect
def get_surface(self):
return self.image
def set_x(self, x):
self.x = x
self.rect.x = x
def set_y(self, y):
self.y = y
self.rect.y = y
def set_xy(self, x, y):
self.set_x(x)
self.set_y(y)
class Player(MasterSprite):
def __init__(self, x, y):
super().__init__('images/ninja.png')
self.rotate = -15
self.image = pygame.transform.rotate(self.image, self.rotate)
self.image.convert_alpha()
self.og_image = self.image
self.flip_x = False
self.set_xy(x, y)
self.can_jump = True
self.can_fall = False
self.latest_landing_y = self.rect.y
self.latest_landing_x = self.rect.x
self.max_jump_height = self.latest_landing_y - 200
self.latest_landing_x_width = self.latest_landing_x
def validate_jump(self):
if self.rect.y >= self.latest_landing_y and self.rect.x >= self.latest_landing_x and self.rect.x <= self.latest_landing_x_width:
self.can_jump = True
if self.y <= self.max_jump_height:
self.can_jump = False
def checkCollision(self, Player, Platform):
Platform.y -= Platform.h
Platform.rect.y -= Platform.rect.h
col = pygame.sprite.collide_rect(Player, Platform)
if col == True:
Platform.y += Platform.h
Platform.rect.y += Platform.rect.h
return True
else:
Platform.y += Platform.h
Platform.rect.y += Platform.rect.h
return False
def image_transformations(self):
self.image = pygame.transform.flip(self.og_image, self.flip_x, False)
self.image = pygame.transform.rotate(self.image, self.rotate)
def move(self, event, move_size):
self.validate_jump()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if self.can_jump == True:
if self.flip_x == False:
self.rotate = 15
else:
self.rotate = -15
self.image_transformations()
self.move_up(move_size)
elif event.key == pygame.K_DOWN:
if self.can_fall == True:
if self.flip_x == False:
self.rotate = -105
else:
self.rotate = 105
self.image_transformations()
self.move_down(move_size)
elif event.key == pygame.K_LEFT:
self.flip_x = True
self.rotate = 0
self.image_transformations()
self.move_left(move_size)
elif event.key == pygame.K_RIGHT:
self.flip_x = False
self.rotate = 0
self.image_transformations()
self.move_right(move_size)
elif event.type == pygame.KEYUP:
self.rotate = 0
self.image_transformations()
def gravity(self):
self.move_down(8)
def update(self, event, move_size):
self.move(event, move_size)
if self.rect.x > (display_width - 68):
self.rect.x = display_width - 68
self.x = self.rect.x
if self.rect.x < 0:
self.rect.x = 0
self.x = self.rect.x
if self.rect.y < 0:
self.rect.y = 0
self.y = self.rect.y
if self.rect.y > (display_height - 68):
self.rect.y = display_height - 68
self.y = self.rect.y
class Platform(MasterSprite):
def __init__(self, x, y, w, h):
pygame.sprite.Sprite.__init__(self)
super().__init__('images/platform_blue.png')
self.w = w
self.rect.w = self.w
self.h = h
self.rect.h = self.h
self.image = pygame.transform.scale(self.image, (self.w, self.h))
self.image.convert_alpha()
self.og_image = self.image
self.set_xy(x, y)
self.gravity_amt = 0
def change_pic(self, new_img):
self.image = pygame.image.load(new_img)
self.image = pygame.transform.scale(self.image, (self.w, self.h))
def gravity(self, amount):
if self.gravity_amt == 0:
self.gravity_amt = amount
self.move_down(self.gravity_amt)
if self.rect.y > display_height:
self.kill()
# quickly create objects (rects)
def text_objects(text, font, colour):
text_surf = font.render(text, True, colour)
return text_surf, text_surf.get_rect()
# basic game functions
def quit_game():
pygame.quit()
sys.exit()
quit()
# go back to previous window/screen when p pressed on keyboard
def return_to_prev_screen(prev_screen, curr_screen):
if prev_screen is None:
return 'intro', prev_screen, curr_screen
elif prev_screen == 'instructions':
return 'instructions', prev_screen, curr_screen
elif prev_screen == 'intro':
return 'intro', prev_screen, curr_screen
elif prev_screen == 'main' or prev_screen == 'death_screen':
return 'main', prev_screen, curr_screen
else:
return 'intro', prev_screen, curr_screen
# run window/screen based on user's choices
def screen_to_run(wdw, prev_screen, curr_screen):
if wdw is False:
quit_game()
elif wdw == 'intro':
return Introduction(prev_screen, curr_screen).display_screen()
elif wdw == 'instructions':
return Instructions(prev_screen, curr_screen).display_screen()
elif wdw == 'main':
return Main(prev_screen, curr_screen).display_screen()
elif wdw == 'return_to_prev_screen':
return return_to_prev_screen(prev_screen, curr_screen)
elif wdw == 'death_screen':
return Death_Screen(prev_screen, curr_screen).display_screen()
else:
return Introduction(prev_screen, curr_screen).display_screen()
return wdw, prev_screen, curr_screen
# parent class for all screens
class Screen:
def __init__(self, prev_screen, curr_screen):
self.prev_screen = prev_screen
self.curr_screen = curr_screen
def set_prev_curr_screen(self, curr):
try:
self.prev_screen = self.curr_screen
except UnboundLocalError:
self.prev_screen = 'intro'
self.curr_screen = curr
def clear_screen(self):
# set window + clear screen
self.manager = pygame_gui.UIManager(display_size, 'themes/button_themes.json')
def draw_title_text(self, text, size, x, y, colour):
# title Fake Doodle Jump at top of the screen
title_text_font = pygame.font.Font('fonts/Montserrat-Bold.ttf', size)
text_surf, text_rect = text_objects(text, title_text_font, colour)
text_rect.center = (x, y)
display.blit(text_surf, text_rect)
def draw_multiple_text(self, text, size, x, y, colour, text_type = 0, y_padding = 0):
# instructions' text
if text_type == 0:
text_line_font = pygame.font.Font('fonts/Montserrat-Regular.ttf', size)
else:
text_line_font = pygame.font.Font('fonts/Montserrat-Bold.ttf', size)
controls_text = text
# put instructions on the screen
for i in range(len(controls_text)):
display.blit(text_line_font.render(controls_text[i], 1, colour), (x, (y_padding + (y * (i + 1)))))
def clock_sync(self):
# don't load faster than needed
clock.tick(60) / 1000
self.time_delta = clock.tick(60) / 1000
def universal_keyboard_events(self, event):
# red x on top left of every window = quit
if event.type == pygame.QUIT:
quit_game()
if event.type == pygame.KEYDOWN:
# press escape to quit
if event.key == pygame.K_ESCAPE:
quit_game()
# press backspace to go to previous screen/window
if event.key == pygame.K_BACKSPACE:
return ['return_to_prev_screen', self.prev_screen, self.curr_screen]
return [None, None, None]
# screen game opens with
class Introduction(Screen):
def create_buttons(self):
# instructions button under title
self.instructions_btn = pygame_gui.elements.UIButton(
relative_rect = pygame.Rect((300, 200), (200, 100)),
text = 'Instructions', manager = self.manager, object_id = '#instructions')
# play game button
self.play_game_btn = pygame_gui.elements.UIButton(
relative_rect = pygame.Rect((100, 400), (200, 100)),
text = 'Play Game', manager = self.manager, object_id = '#play_game')
# quit button
self.quit_btn = pygame_gui.elements.UIButton(
relative_rect = pygame.Rect((500, 400), (200, 100)),
text = 'Quit', manager = self.manager, object_id = '#quit')
def local_keyboard_events(self, event):
# press i to see instructions
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
return ['instructions', self.prev_screen, self.curr_screen]
return [None, None, None]
def local_button_events(self, event):
if event.type == pygame.USEREVENT:
# where to go when buttons clicked
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == self.instructions_btn:
return ['instructions', self.prev_screen, self.curr_screen]
if event.ui_element == self.play_game_btn:
return ['main', self.prev_screen, self.curr_screen]
if event.ui_element == self.quit_btn:
quit_game()
return [None, None, None]
def display_screen(self):
self.set_prev_curr_screen('intro')
self.clear_screen()
display.fill(GRAY)
self.create_buttons()
self.draw_title_text('NINJA JUMPER', 50, (display_width / 2), (display_height / 8), BLACK)
while True:
self.clock_sync()
for event in pygame.event.get():
universal_k_event = self.universal_keyboard_events(event)
local_k_event = self.local_keyboard_events(event)
local_b_event = self.local_button_events(event)
if universal_k_event[0] != None:
return universal_k_event
if local_k_event[0] != None:
return local_k_event
if local_b_event[0] != None:
return local_b_event
self.manager.process_events(event)
self.manager.update(self.time_delta)
# display.blit(background, (0, 0))
self.manager.draw_ui(display)
pygame.display.flip()
# screen with instruction
class Instructions(Screen):
def local_keyboard_events(self, event):
if event.type == pygame.KEYDOWN:
# press m to go the intro screen
if event.key == pygame.K_m:
return ['intro', self.prev_screen, self.curr_screen]
return [None, None, None]
def draw_small_title_text(self, text, size, x, y, colour):
text_line_font = pygame.font.Font('fonts/Montserrat-Regular.ttf', size)
text_line_0 = text_line_font.render(text, 1, colour)
display.blit(text_line_0, (x, y))
def display_screen(self):
self.set_prev_curr_screen('instructions')
self.clear_screen()
display.fill(DARK_GREEN)
while True:
self.clock_sync()
for event in pygame.event.get():
universal_k_event = self.universal_keyboard_events(event)
local_k_event = self.local_keyboard_events(event)
if universal_k_event[0] != None:
return universal_k_event
if local_k_event[0] != None:
return local_k_event
self.manager.process_events(event)
self.manager.update(self.time_delta)
self.draw_small_title_text('Controls:', 25, 5, 0, YELLOW)
self.draw_multiple_text(['- To quit, either click the red button at the top left, or press', ' esc on the keyboard.', '- To go back to the previous page you were on, press', ' backspace on the keyboard.', '- To open up this page again, press i on the keyboard.', '- To go back to the main page, press m on the keyboard.', '- To move the main sprite, use the arrow keys.', '- To pause the game, press p on the keyboard.', '- You can click on any buttons - buttons always light up when ', ' they are hovered over.'], 25, 5, 27, WHITE)
self.draw_small_title_text('Rules:', 25, 5, 324, RED)
self.draw_multiple_text(['- The aim of the game is to survive for as long as possible.', '- Your score will increase based on how long you stay on a', ' platform for.', '- Your score will be displayed in the top-left corner of the', ' screen.', '- You can only jump 200 pixels up from the last platform you', ' landed on.', '- A new platform will spawn when an old one drops off the', ' edge of the screen.', '- You will fall with the platform you are standing on.', '- The first platform will only fall when you aren\'t on it.', '- If you hit any of the edges of the screen, you die.'], 25, 5, 27, WHITE, 0, 324)
# TODO: CANNOT JUMP ON PLATFORMS UNTIL FIRST GONE
self.manager.draw_ui(display)
pygame.display.flip()
# main gameplay screen
class Main(Screen):
def __init__(self, prev_screen, curr_screen):
super().__init__(prev_screen, curr_screen)
self.is_paused = False
self.is_dead = False
def local_keyboard_events(self, event):
if event.type == pygame.KEYDOWN:
# press m to go the intro screen
if event.key == pygame.K_m:
return ['intro', self.prev_screen, self.curr_screen]
# press i to see instructions
if event.key == pygame.K_i:
return ['instructions', self.prev_screen, self.curr_screen]
return [None, None, None]
def local_button_events(self, event):
if event.type == pygame.USEREVENT:
# where to go when buttons clicked
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == self.continue_btn:
self.unpause()
if event.ui_element == self.quit_btn:
quit_game()
return [None, None, None]
def create_buttons_pause(self):
# continue button
self.continue_btn = pygame_gui.elements.UIButton(
relative_rect = pygame.Rect((100, 400), (200, 100)),
text = 'Continue', manager = self.manager, object_id = '#play_game')
# quit button
self.quit_btn = pygame_gui.elements.UIButton(
relative_rect = pygame.Rect((500, 400), (200, 100)),
text = 'Quit', manager = self.manager, object_id = '#quit')
def pause(self):
if self.is_paused == True:
self.create_buttons_pause()
self.draw_title_text('PAUSED', 50, (display_width / 2), (display_height / 8), BLACK)
while self.is_paused:
for event in pygame.event.get():
universal_k_event = self.universal_keyboard_events(event)
local_k_event = self.local_keyboard_events(event)
local_b_event = self.local_button_events(event)
if universal_k_event[0] != None:
return universal_k_event
if local_k_event[0] != None:
return local_k_event
if local_b_event[0] != None:
return local_b_event
self.manager.process_events(event)
self.manager.update(self.time_delta)
self.manager.draw_ui(display)
pygame.display.flip()
else:
self.unpause()
def unpause(self):
self.is_paused = False
self.clear_screen()
self.manager.update(self.time_delta)
self.manager.draw_ui(display)
pygame.display.flip()
def display_screen(self):
self.set_prev_curr_screen('main')
self.clear_screen()
possible_gravities = []
for x in range(1, 6):
possible_gravities.append(x)
user = Player(368, 680)
first_platform = Platform(325, 745, 150, 15)
platforms = pygame.sprite.Group()
total_platforms = random.randint(5, 7)
platform_counter = 0
platforms_touched = 0
temp_list_x = []
temp_list_y = []
for platform_number in range(total_platforms):
if platform_number == 0:
plat = Platform(325, 605, 150, 15)
else:
plat = Platform(random.randint(0, 550), random.randint((user.rect.y // 4), (user.rect.y - 50)), random.randint(100, 250), 15)
for each in temp_list_x:
if each == plat.x:
if each >= 275:
plat.x = random.randint(0, each // 2)
else:
plat.x = random.randint(275, (275 + (each // 2)))
while plat.y in temp_list_y:
plat.y = random.randint((user.rect.y // 4), (user.rect.y - 50))
temp_list_x.append(plat.x)
temp_list_y.append(plat.y)
platforms.add(plat)
display.blit(main_bg, (0, 0))
while True:
self.clock_sync()
for platform in platforms:
platform.gravity(possible_gravities[random.randint(0, 4)])
if first_platform.y <= display_height:
if user.checkCollision(user, first_platform) == True:
# update jump height and values for gravity to work
user.rect = user.get_rect()
first_platform.rect = first_platform.get_rect()
user.latest_landing_y = first_platform.y
user.max_jump_height = user.y - 200
user.latest_landing_x = first_platform.x
user.latest_landing_x_width = user.latest_landing_x + first_platform.w
user.can_jump = True
user.can_fall = False
platforms_touched += 1
# can't go below first platform
if user.rect.y > first_platform.y:
user.set_y(user.latest_landing_y)
else:
# player and platform fall
# user.can_fall = True
first_platform.gravity(2)
# user.gravity()
platform_counter = 0
any_platform_collide = False
for platform in platforms:
platform_counter += 1
if platform_counter == 1:
if user.checkCollision(user, platform) == True:
any_platform_collide = True
# update jump height and values for gravity to work
user.rect = user.get_rect()
platform.rect = platform.get_rect()
user.latest_landing_y = platform.y
user.max_jump_height = user.y - 200
user.latest_landing_x = platform.x
user.latest_landing_x_width = user.latest_landing_x + platform.w
user.can_jump = True
user.can_fall = False
platforms_touched += 1
# can't go below first platform
if user.rect.y > platform.y:
user.set_y(user.latest_landing_y)
else:
# player and platform fall
user.can_fall = True
platform.gravity(1)
user.gravity()
else:
pass
else:
any_platform_collide = False
platform_counter = 0
for platform in platforms:
platform_counter += 1
if user.checkCollision(user, platform) == True:
any_platform_collide = True
user.rect = user.get_rect()
platform.rect = platform.get_rect()
user.latest_landing_y = platform.y
user.max_jump_height = user.y - 200
user.latest_landing_x = platform.x
user.latest_landing_x_width = user.latest_landing_x + platform.w
user.move_down(platform.gravity_amt)
user.can_jump = True
user.can_fall = False
platforms_touched += 1
# can't go below first platform
if user.y > platform.y:
user.set_y(user.latest_landing_y)
else:
if any_platform_collide == False and (platform_counter == total_platforms or platform_counter == (total_platforms - 1)):
user.can_fall = True
user.gravity()
platform.gravity(platform.gravity_amt)
temp_list_x = []
temp_list_y = []
while platform_counter < total_platforms:
for platform_number in range(total_platforms - platform_counter):
try:
plat = Platform(random.randint(0, 550), random.randint((user.rect.y // 4), (user.rect.y - 50)), random.randint(100, 250), 15)
except ValueError:
plat = Platform(random.randint(0, 550), random.randint(0, 550), random.randint(100, 250), 15)
for each in temp_list_x:
if each == plat.x:
if each >= 275:
plat.x = random.randint(0, each // 2)
else:
plat.x = random.randint(275, (275 + (each // 2)))
while plat.y in temp_list_y:
plat.y = random.randint((user.rect.y // 4), (user.rect.y - 50))
temp_list_x.append(plat.x)
temp_list_y.append(plat.y)
platforms.add(plat)
platform_counter += 1
# you died screen on boundary touch
if user.rect.x >= (display_width - 68) or user.rect.x <= 0 or user.rect.y <= 0 or user.rect.y >= (display_height - 68):
self.is_dead = True
if self.is_dead == True:
return ['death_screen', prev_screen, curr_screen]
for event in pygame.event.get():
temp_list_x = []
temp_list_y = []
while platform_counter < total_platforms:
for platform_number in range(total_platforms - platform_counter):
try:
plat = Platform(random.randint(0, 550), random.randint((user.rect.y // 4), (user.rect.y - 50)), random.randint(100, 250), 15)
except ValueError:
plat = Platform(random.randint(0, 550), random.randint(0, 550), random.randint(100, 250), 15)
for each in temp_list_x:
if each == plat.x:
if each >= 275:
plat.x = random.randint(0, each // 2)
else:
plat.x = random.randint(275, (275 + (each // 2)))
while plat.y in temp_list_y:
plat.y = random.randint((user.rect.y // 4), (user.rect.y - 50))
temp_list_x.append(plat.x)
temp_list_y.append(plat.y)
platforms.add(plat)
platform_counter += 1
user.update(event, 10)
# press p to pause game
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
if self.is_paused == False:
self.is_paused = True
self.pause()
else:
self.unpause()
universal_k_event = self.universal_keyboard_events(event)
local_k_event = self.local_keyboard_events(event)
if universal_k_event[0] != None:
return universal_k_event
if local_k_event[0] != None:
return local_k_event
self.manager.process_events(event)
self.manager.update(self.time_delta)
display.blit(main_bg, (0, 0))
self.draw_multiple_text(['score: ' + str(platforms_touched)], 25, 10, 10, BLACK, 1)
display.blit(user.get_surface(), user.get_rect())
if first_platform.rect.y < display_height:
display.blit(first_platform.get_surface(), first_platform.get_rect())
for platform in platforms:
if platform.rect.y < display_height:
display.blit(platform.get_surface(), platform.get_rect())
else:
platforms.remove(platform)
platform.kill()
self.manager.draw_ui(display)
pygame.display.flip()
# screen displayed when user crashes into window edges
class Death_Screen(Screen):
def local_keyboard_events(self, event):
if event.type == pygame.KEYDOWN:
# press m to go the intro screen
if event.key == pygame.K_m:
return ['intro', self.prev_screen, self.curr_screen]
# press i to see instructions
if event.key == pygame.K_i:
return ['instructions', self.prev_screen, self.curr_screen]
return [None, None, None]
def local_button_events(self, event):
if event.type == pygame.USEREVENT:
# where to go when buttons clicked
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == self.play_again_btn:
return 'main', self.prev_screen, self.curr_screen
if event.ui_element == self.quit_btn:
quit_game()
return [None, None, None]
def create_buttons_dead(self):
# play again button
self.play_again_btn = pygame_gui.elements.UIButton(
relative_rect = pygame.Rect((100, 400), (200, 100)),
text = 'Play Again', manager = self.manager, object_id = '#play_game')
# quit button
self.quit_btn = pygame_gui.elements.UIButton(
relative_rect = pygame.Rect((500, 400), (200, 100)),
text = 'Quit', manager = self.manager, object_id = '#quit')
def display_screen(self):
self.set_prev_curr_screen('instructions')
self.clear_screen()
self.create_buttons_dead()
self.draw_title_text('YOU DIED :(', 50, (display_width / 2), (display_height / 8), BLACK)
while True:
self.clock_sync()
for event in pygame.event.get():
universal_k_event = self.universal_keyboard_events(event)
local_k_event = self.local_keyboard_events(event)
local_b_event = self.local_button_events(event)
if universal_k_event[0] != None:
return universal_k_event
if local_k_event[0] != None:
return local_k_event
if local_b_event[0] != None:
return local_b_event
self.manager.process_events(event)
self.manager.update(self.time_delta)
self.manager.draw_ui(display)
pygame.display.flip()
return [None, None, None]
# used to choose which screen to run
user_wdw, user_prev, user_curr = True, prev_screen, curr_screen
# driver code
if __name__ == "__main__":
# first initialization - start on intro page
user_wdw, user_prev, user_curr = Introduction(user_prev, user_curr).display_screen()
# choose which screen to run as long as not quitting
while user_wdw is not False:
user_wdw, user_prev, user_curr = screen_to_run(user_wdw, user_prev, user_curr)
# close everything
quit_game()