-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
661 lines (621 loc) · 26.3 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
from os import path
import pygame
import sys
import math
from pygame import mixer
import random
import shelve
import time
pygame.init()
screen = pygame.display.set_mode((800, 600)) #creates window screen
background = pygame.image.load(path.join("Game Assets","background.png")).convert_alpha() #background
background_level2 = pygame.image.load(path.join("Game Assets","background2.png")).convert_alpha()
background_level3 = pygame.image.load(path.join("Game Assets","background3.png")).convert_alpha()
main_menu = pygame.image.load(path.join("Game Assets","main_menu.png")).convert()
level_select_23locked = pygame.image.load(path.join("Game Assets","level_select_23locked.png")).convert()
level_select_3locked = pygame.image.load(path.join("Game Assets","level_select_3locked.png")).convert()
level_select_full = pygame.image.load(path.join("Game Assets","level_select.png")).convert()
mixer.music.load(path.join("Game Assets","background_music.mp3")) #loads background music
mixer.music.play(-1)
pygame.display.set_caption("Galactiblock") #icon and title
icon = pygame.image.load(path.join("Game Assets","icon.png"))
pygame.display.set_icon(icon)
class Block: #block class
def __init__(self, Img, ImgX, ImgY):
self.Img = Img
self.ImgX = ImgX
self.ImgY = ImgY
def spawn(self, Img, ImgX, ImgY):
screen.blit(Img, (ImgX, ImgY))
#PRESS SPACE TO BEGIN
start = False
press_start_font = pygame.font.Font(path.join("Game Assets","slkscr.ttf"), 42)
def press_to_start():
space_start = press_start_font.render('PRESS SPACE TO BEGIN', True, (255,255,255))
screen.blit(space_start, (130,280))
#UNLOCKED LEVEL CHECK
unlocked = shelve.open('game_data')
#unlocked['level_select'] = 1
#SCORE
score_value = 0
font = pygame.font.Font(path.join("Game Assets","slkscr.ttf"), 36)
textX = 10
textY = 10
def show_score(x, y):
score = font.render("Score: " + str(score_value), True, (0, 255, 0))
screen.blit(score, (x, y))
#RECORD
record = shelve.open('game_data')
highX = 10
highY = 100
def personal_record(x, y, level):
high_s = font.render("Record: " + str(record[level]), True, (255, 255, 0))
screen.blit(high_s, (x, y))
#MISSED
missed_value = 0
missedX = 10
missedY = 55
def show_missed(x, y, num):
missed = font.render("Missed: " + str(missed_value) + "/" + str(num), True, (255, 0, 0))
screen.blit(missed, (x, y))
#GAME OVER
game_over_font = pygame.font.Font(path.join("Game Assets","slkscr.ttf"), 80)
def game_over():
game_over_text = game_over_font.render('GAME OVER', True, (255,0,0))
screen.blit(game_over_text, (160,250))
reset_text = font.render("Press R to restart", True, (255, 255, 255))
screen.blit(reset_text, (200, 350))
return_text = font.render("Press Q to return", True, (255, 255, 255))
screen.blit(return_text, (205, 400))
#INITIAL SPEED
IntervalA = 2.8
IntervalB = 3.8
#RED BLOCKS
redImg = pygame.image.load(path.join("Game Assets","red_block.png")).convert()
redX = 274
redY = random.randint(-1472, -128) #randomizes intial y-position
redChange = random.uniform(IntervalA, IntervalB) #randomizes intial speed
red_block = Block(redImg, redX, redY)
#SEA-FOAM BLOCKS
seafoamImg = pygame.image.load(path.join("Game Assets","seafoam_block.png")).convert()
seafoamX = 462
seafoamY = random.randint(-1472, -128) #randomizes intial y-position
seafoamChange = random.uniform(IntervalA, IntervalB) #randomizes initial speed
seafoam_block = Block(seafoamImg, seafoamX, seafoamY)
#JADE BLOCKS
jadeImg = pygame.image.load(path.join("Game Assets","bluegreen_block.png")).convert()
jadeX = 402
jadeY = random.randint(-1472, -128) #randomizes initial y-position
jadeChange = random.uniform(IntervalA, IntervalB) #randomizes initial speed
jade_block = Block(jadeImg, jadeX, jadeY)
#VIOLET BLOCKS
violetImg = pygame.image.load(path.join("Game Assets","violet_block.png")).convert()
violetX = 530
violetY = random.randint(-1472, -128) #randomizes initial y-position
violetChange = random.uniform(IntervalA, IntervalB) #randomizes initial speed
violet_block = Block(violetImg, violetX, violetY)
#LEFT BUTTON
leftButtonImg = pygame.image.load(path.join("Game Assets","left_button.png")).convert_alpha()
leftButtonImgPressed = pygame.image.load(path.join("Game Assets","left_button_pressed.png"))
leftB = [leftButtonImg, leftButtonImgPressed]
left_pressed = False
#RIGHT BUTTON
rightButtonImg = pygame.image.load(path.join("Game Assets","right_button.png"))
rightButtonImgPressed = pygame.image.load(path.join("Game Assets","right_button_pressed.png"))
rightB = [rightButtonImg, rightButtonImgPressed]
right_pressed = False
#UP BUTTON
upButtonImg = pygame.image.load(path.join("Game Assets","up_button.png"))
upButtonImgPressed = pygame.image.load(path.join("Game Assets","up_button_pressed.png"))
upB = [upButtonImg, upButtonImgPressed]
up_pressed = False
#DOWN BUTTON
downButtonImg = pygame.image.load(path.join("Game Assets","down_button.png"))
downButtonImgPressed = pygame.image.load(path.join("Game Assets","down_button_pressed.png"))
downB = [downButtonImg, downButtonImgPressed]
down_pressed = False
#COLLISION
def Collision(BlockX, BlockY, ButtonX, ButtonY):
distance = math.sqrt((math.pow(BlockX-ButtonX,2)) + (math.pow(BlockY - ButtonY,2)))
if distance < 72:
return True
else:
return False
#CHANGE IN DIFFICULTY
def increase_difficulty():
global IntervalA
global IntervalB
if score_value % 10 == 0:
IntervalA += .45
IntervalB += .45
#RESET
def reset():
global IntervalA, IntervalB, redY, redChange, seafoamY, seafoamChange, jadeChange, jadeY, violetY, violetChange
IntervalA = 2.8
IntervalB = 3.8
redY = random.randint(-1472, -128) #randomizes intial y-position
redChange = random.uniform(IntervalA, IntervalB) #randomizes intial speed
seafoamY = random.randint(-1472, -128) #randomizes intial y-position
seafoamChange = random.uniform(IntervalA, IntervalB) #randomizes intial speed
jadeY = random.randint(-1472, -128)
jadeChange = random.uniform(IntervalA, IntervalB)
violetY = random.randint(-1472, -128)
violetChange = random.uniform(IntervalA, IntervalB)
class GameStates: #master class
def __init__(self):
self.done = False
self.next = None
self.quit = False
self.previous = None
class Menu(GameStates):
def __init__(self):
GameStates.__init__(self)
self.next = "level_select"
def pygame_event(self, event):
play_button = pygame.Rect(330, 265, 145, 50)
quit_button = pygame.Rect(335, 375, 130, 40)
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if play_button.collidepoint(pos): #this checks whether or not the button was pressed
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
self.next = "level_select"
self.done = True
if quit_button.collidepoint(pos):
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
pygame.quit()
def update(self, screen, dt):
self.draw(screen)
def draw(self, screen):
screen.fill((0,0,0))
screen.blit(main_menu, (0, 0))
class LevelSelect(GameStates):
def __init__(self):
GameStates.__init__(self)
self.next = "level1"
def pygame_event(self, event):
level_1 = pygame.Rect(50, 290, 190, 200)
level_2 = pygame.Rect(315, 230, 175, 250)
level_3 = pygame.Rect(575, 165, 160, 310)
reset_prog = pygame.Rect(630, 493, 128, 64)
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if level_1.collidepoint(pos):
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
self.next = "level1"
self.done = True
if level_2.collidepoint(pos):
if unlocked['level_select'] == 2 or unlocked['level_select'] == 3:
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
self.next = "level2"
self.done = True
if level_3.collidepoint(pos):
if unlocked['level_select'] == 3:
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
self.next = 'level3'
self.done = True
if reset_prog.collidepoint(pos):
reset = mixer.Sound(path.join("Game Assets","reset.wav"))
reset.play()
unlocked['level_select'] = 1
record['level1'] = 0
record['level2'] = 0
record['level3'] = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
self.next = "main_menu"
self.done = True
def update(self, screen, dt):
self.draw(screen)
def draw(self, screen):
screen.fill((0,0,0))
if unlocked['level_select'] == 1:
record['level2'] = 0
screen.blit(level_select_23locked, (0, 0))
if unlocked['level_select'] == 2:
screen.blit(level_select_3locked, (0, 0))
if unlocked['level_select'] == 3:
screen.blit(level_select_full, (0, 0))
class Level1(GameStates):
def __init__(self):
GameStates.__init__(self)
self.next = 'level_select'
def pygame_event(self, event):
global start, redY, seafoamY, missed_value, redChange, seafoamChange, score_value, left_pressed, right_pressed
collisionRed = Collision(redX, redY, 306, 532) #checks for collisions between button and blocks
collisionSeafoam = Collision(seafoamX, seafoamY, 494, 532)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
start = True
if event.key == pygame.K_LEFT:
left_pressed = True
if collisionRed:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
redY = random.randint(-320, -128)
increase_difficulty()
redChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_RIGHT:
right_pressed = True
if collisionSeafoam:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
seafoamY = random.randint(-320, -128)
increase_difficulty()
seafoamChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_r: #restart
missed_value = 0
score_value = 0
reset()
if event.key == pygame.K_q:
missed_value = 0
score_value = 0
reset()
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
start = False
self.done = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
left_pressed = False
if event.key == pygame.K_RIGHT:
right_pressed = False
def update(self, screen, dt):
self.draw(screen)
global redY, seafoamY, missed_value, redChange, seafoamChange
if start == False:
press_to_start()
if redY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
redY = random.randint(-1472, -128)
if seafoamY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
seafoamY = random.randint(-1472, -128)
if missed_value >= 4:
game_over()
redY = -64
redChange = 0
seafoamY = -64
seafoamChange = 0
if score_value > record['level1']: #updates record
record['level1'] = score_value
if record['level1'] >= 60:
unlocked['level_select'] = 2
if record['level1'] < 60:
unlocked['level_select'] = 1
while start: #updating positions
redY += redChange
seafoamY += seafoamChange
break
def draw(self, screen):
global left_pressed, right_pressed
screen.fill((0,0,0))
screen.blit(background,(0,0))
red_block.spawn(redImg, redX, redY), seafoam_block.spawn(seafoamImg, seafoamX, seafoamY)
if left_pressed == True:
screen.blit(leftB[1], (274, 532))
elif left_pressed == False:
screen.blit(leftB[0], (274, 532))
if right_pressed == True:
screen.blit(rightB[1], (462, 532))
elif right_pressed == False:
screen.blit(rightB[0], (462, 532))
show_score(textX, textY), show_missed(missedX, missedY, 4), personal_record(highX, highY, 'level1')
class Level2(GameStates):
def __init__(self):
GameStates.__init__(self)
self.next = 'level_select'
def pygame_event(self, event):
global start, redY, seafoamY, missed_value, redChange, seafoamChange, score_value, jadeY, jadeChange, right_pressed, left_pressed, up_pressed
collisionRed = Collision(redX, redY, 306, 532) #checks for collisions between button and blocks
collisionJade = Collision(jadeX, jadeY, 434, 532)
collisionSeafoam = Collision(530, seafoamY, 562, 532)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
start = True
if event.key == pygame.K_LEFT:
left_pressed = True
if collisionRed:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
redY = random.randint(-1472, -128)
increase_difficulty()
redChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_RIGHT:
right_pressed = True
if collisionSeafoam:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
seafoamY = random.randint(-1472, -128)
increase_difficulty()
seafoamChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_UP:
up_pressed = True
if collisionJade:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
jadeY = random.randint(-1472, -128)
increase_difficulty()
jadeChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_r: #restart
missed_value = 0
score_value = 0
reset()
if event.key == pygame.K_q:
missed_value = 0
score_value = 0
reset()
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
start = False
self.done = True
self.next = "level_select"
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
left_pressed = False
if event.key == pygame.K_RIGHT:
right_pressed = False
if event.key == pygame.K_UP:
up_pressed = False
def update(self, screen, dt):
self.draw(screen)
global redY, seafoamY, missed_value, redChange, seafoamChange, jadeY, jadeChange
if start == False:
press_to_start()
if redY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
redY = random.randint(-1472, -128)
if seafoamY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
seafoamY = random.randint(-1472, -128)
if jadeY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
jadeY = random.randint(-1472, -128)
if missed_value >= 6:
game_over()
redY = -64
redChange = 0
seafoamY = -64
jadeY = -64
jadeChange = 0
seafoamChange = 0
if score_value > record['level2']: #updates record
record['level2'] = score_value
if record['level2'] >= 60:
unlocked['level_select'] = 3
while start: #updating positions
redY += redChange
seafoamY += seafoamChange
jadeY += jadeChange
break
def draw(self, screen):
global left_pressed, right_pressed, up_pressed
screen.fill((0,0,0))
screen.blit(background_level2,(0,0))
red_block.spawn(redImg, redX, redY), seafoam_block.spawn(seafoamImg, 530, seafoamY), jade_block.spawn(jadeImg, jadeX, jadeY)
if left_pressed == True:
screen.blit(leftB[1], (274, 532))
elif left_pressed == False:
screen.blit(leftB[0], (274, 532))
if right_pressed == True:
screen.blit(rightB[1], (530, 532))
elif right_pressed == False:
screen.blit(rightB[0], (530, 532))
if up_pressed == True:
screen.blit(upB[1], (402, 532))
elif up_pressed == False:
screen.blit(upB[0], (402, 532))
show_score(textX, textY), show_missed(missedX, missedY, 6), personal_record(highX, highY, 'level2')
class Level3(GameStates):
def __init__(self):
GameStates.__init__(self)
self.next = 'level_select'
def pygame_event(self, event):
global start, redY, seafoamY, missed_value, redChange, seafoamChange, score_value, jadeY, jadeChange, violetY, violetChange, left_pressed, right_pressed, up_pressed, down_pressed
collisionRed = Collision(redX, redY, 306, 532) #checks for collisions between button and blocks
collisionJade = Collision(jadeX, jadeY, 434, 532)
collisionViolet = Collision(violetX, violetY, 562, 532)
collisionSeafoam = Collision(658, seafoamY, 690, 532)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
start = True
if event.key == pygame.K_LEFT:
left_pressed = True
if collisionRed:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
redY = random.randint(-1472, -128)
increase_difficulty()
redChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_RIGHT:
right_pressed = True
if collisionSeafoam:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
seafoamY = random.randint(-1472, -128)
increase_difficulty()
seafoamChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_UP:
up_pressed = True
if collisionJade:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
jadeY = random.randint(-1472, -128)
increase_difficulty()
jadeChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_DOWN:
down_pressed = True
if collisionViolet:
sound = mixer.Sound(path.join("Game Assets","collision.wav"))
sound.play()
score_value += 1
violetY = random.randint(-1472, -128)
increase_difficulty()
violetChange = random.uniform(IntervalA, IntervalB)
if event.key == pygame.K_r: #restart
missed_value = 0
score_value = 0
reset()
if event.key == pygame.K_q:
missed_value = 0
score_value = 0
reset()
select = mixer.Sound(path.join("Game Assets","select.wav"))
select.play()
time.sleep(.25)
start = False
self.done = True
self.next = "level_select"
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
left_pressed = False
if event.key == pygame.K_RIGHT:
right_pressed = False
if event.key == pygame.K_UP:
up_pressed = False
if event.key == pygame.K_DOWN:
down_pressed = False
def update(self, screen, dt):
self.draw(screen)
global redY, seafoamY, missed_value, redChange, seafoamChange, jadeY, jadeChange, violetY, violetChange
if start == False:
press_to_start()
if redY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
redY = random.randint(-1472, -128)
if seafoamY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
seafoamY = random.randint(-1472, -128)
if jadeY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
jadeY = random.randint(-1472, -128)
if violetY >= 574:
missed = mixer.Sound(path.join("Game Assets","missed.wav"))
missed.play()
missed_value += 1
violetY = random.randint(-1472, -128)
if missed_value >= 8:
game_over()
redY = -64
redChange = 0
seafoamY = -64
jadeY = -64
jadeChange = 0
seafoamChange = 0
violetY = -64
violetChange = 0
if score_value > record['level3']: #updates record
record['level3'] = score_value
if record['level3'] >= 60:
unlocked['level_select'] = 3
while start: #updating positions
redY += redChange
seafoamY += seafoamChange
jadeY += jadeChange
violetY += violetChange
break
def draw(self, screen):
global left_pressed, right_pressed, up_pressed, down_pressed
screen.fill((0,0,0))
screen.blit(background_level3,(0,0))
red_block.spawn(redImg, redX, redY), seafoam_block.spawn(seafoamImg, 658, seafoamY), jade_block.spawn(jadeImg, jadeX, jadeY), violet_block.spawn(violetImg, violetX, violetY)
if left_pressed == True:
screen.blit(leftB[1], (274, 532))
elif left_pressed == False:
screen.blit(leftB[0], (274, 532))
if right_pressed == True:
screen.blit(rightB[1], (658, 532))
elif right_pressed == False:
screen.blit(rightB[0], (658, 532))
if up_pressed == True:
screen.blit(upB[1], (402, 532))
elif up_pressed == False:
screen.blit(upB[0], (402, 532))
if down_pressed == True:
screen.blit(downB[1], (530, 532))
elif down_pressed == False:
screen.blit(downB[0], (530, 532))
show_score(textX, textY), show_missed(missedX, missedY, 8), personal_record(highX, highY, 'level3')
class GameControl:
def __init__(self):
self.__dict__.update()
self.done = False
self.screen = pygame.display.set_mode((800,600))
self.clock = pygame.time.Clock()
def setup(self, levels, main_menu):
self.levels = levels
self.level_name = main_menu
self.state = self.levels[self.level_name]
def flip_state(self):
self.state.done = False
previous = self.level_name
self.level_name = self.state.next
self.state = self.levels[self.level_name]
self.state.previous = previous
def update(self, dt):
if self.state.quit:
self.done = True
elif self.state.done:
self.flip_state()
self.state.update(self.screen, dt)
def event_loop(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.done = True
self.state.pygame_event(event)
def main_game_loop(self):
while not self.done:
delta_time = self.clock.tick(120)/1000.0
self.event_loop()
self.update(delta_time)
pygame.display.update()
game = GameControl()
levels = {
'main_menu': Menu(),
'level_select': LevelSelect(),
'level1': Level1(),
'level2': Level2(),
'level3': Level3()
}
game.setup(levels, 'main_menu')
game.main_game_loop()
pygame.quit()
sys.exit()