-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.py
728 lines (593 loc) · 25.8 KB
/
game.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
import pygame
import constants as c
import sys
import math
from player import Player
from primitives import Pose
from particle import RewindParticle, SunExplosion, SunTint, WarningParticle, BigBoom, SunExplosionLong
from enemy import Orb, Scuttle
from Button import Button
import random
class Game:
def __init__(self):
pygame.init()
self.fullscreen = True
self.screen = pygame.display.set_mode((c.WINDOW_WIDTH, c.WINDOW_HEIGHT))
pygame.display.set_icon(pygame.image.load("images/kunai_ui.png"))
self.colorblind_mode = True
self.clock = pygame.time.Clock()
pygame.display.set_caption("Eleventh Hour")
self.config_menu()
self.last_distance = None
self.intro()
self.directions()
while True:
self.init()
self.main()
def victory_screen(self, prev_surf):
shade = pygame.Surface((c.WINDOW_WIDTH, c.WINDOW_HEIGHT))
shade.fill((255, 255, 255))
alpha = 0
age = 0
while alpha < 255:
events, dt = self.get_events()
age += dt
self.screen.fill((0, 0, 0))
self.screen.blit(prev_surf, (0, 0))
alpha += 255 * dt
shade.set_alpha(alpha)
self.screen.blit(shade, (0, 0))
self.update_display()
back = pygame.image.load("images/win.png")
age = 0
alpha = 255
should_Break = False
etc = pygame.image.load("images/etc_light.png")
while not should_Break:
events, dt = self.get_events()
age += dt
self.screen.blit(back, (0, 0))
if age > 3 and age%1 < 0.7:
self.screen.blit(etc, (c.WINDOW_WIDTH//2 - etc.get_width()//2, c.WINDOW_HEIGHT - etc.get_height()))
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
should_Break = True
shade.set_alpha(alpha)
self.screen.blit(shade, (0, 0))
alpha -= 255 * dt
self.update_display()
alpha = 0
age = 0
shade.fill((0, 0, 0))
while alpha < 255:
events, dt = self.get_events()
age += dt
self.screen.fill((0, 0, 0))
self.screen.blit(back, (0, 0))
alpha += 255 * dt
shade.set_alpha(alpha)
self.screen.blit(shade, (0, 0))
self.update_display()
pygame.quit()
sys.exit()
def directions(self):
shade = pygame.Surface((c.WINDOW_WIDTH, c.WINDOW_HEIGHT))
shade.fill((0, 0, 0))
back = pygame.image.load("images/instructions.png")
age = 0
alpha = 255
should_Break = False
etc = pygame.image.load("images/enter_to_continue.png")
while not should_Break:
events, dt = self.get_events()
age += dt
self.screen.blit(back, (0, 0))
if age > 3 and age%1 < 0.7:
self.screen.blit(etc, (c.WINDOW_WIDTH//2 - etc.get_width()//2, c.WINDOW_HEIGHT - etc.get_height()))
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
should_Break = True
shade.set_alpha(alpha)
self.screen.blit(shade, (0, 0))
alpha -= 255 * dt
self.update_display()
alpha = 0
age = 0
while alpha < 255:
events, dt = self.get_events()
age += dt
self.screen.fill((0, 0, 0))
self.screen.blit(back, (0, 0))
shade.set_alpha(alpha)
self.screen.blit(shade, (0, 0))
alpha += 500 * dt
self.update_display()
def intro(self):
section = pygame.Surface((c.WINDOW_WIDTH, c.WINDOW_HEIGHT//3 + 15))
sections = [
section.copy() for _ in range(3)
]
for section in sections:
section.fill((0,0,0))
age = 0
alphas = [
255 + 500,
255 +2000,
255 +3500,
]
sections[1] = pygame.transform.scale(sections[1], (c.WINDOW_WIDTH, c.WINDOW_HEIGHT//4))
back = pygame.image.load("images/intro.png")
while True:
events, dt = self.get_events()
age += dt
self.screen.blit(back, (0, 0))
for i, alpha in enumerate(alphas):
alphas[i] -= 500 * dt
sections[i].set_alpha(alpha)
x = 0
y = 0
for section in sections:
if age > 13:
section.set_alpha(255 * (age - 13))
for section in sections:
self.screen.blit(section, (x, y))
y += c.WINDOW_HEIGHT//3
if y > c.WINDOW_HEIGHT//2:
y -= 100
self.update_display()
if age > 14:
break
def toggle_colorblind_mode(self):
self.colorblind_mode = not self.colorblind_mode
def toggle_fullscreen_mode(self):
self.fullscreen = not self.fullscreen
def start(self):
self.started = True
def config_menu(self):
self.started = False
self.screen = pygame.display.set_mode((400, 200))
width = 400
height = 240
fullscreen_button = Button(
pygame.image.load("images/fullscreen_enabled.png"),
pos=(width//2 + 92, height - 100),
disabled_surf=pygame.image.load("images/fullscreen_disabled.png"),
enabled=False,
pulse=0,
on_click=self.toggle_fullscreen_mode,
)
colorblind_button = Button(
pygame.image.load("images/contrast_enabled.png"),
pos=(width//2 - 92, height - 100),
disabled_surf=pygame.image.load("images/contrast_disabled.png"),
enabled=False,
pulse=0,
on_click=self.toggle_colorblind_mode,
)
start_button = Button(
pygame.image.load("images/start_button.png"),
pos=(width//2, height - 170),
on_click=self.start,
pulse=0,
)
while not self.started:
events, dt = self.get_events()
self.screen.fill((0, 0, 0))
colorblind_button.update(dt, events)
fullscreen_button.update(dt, events)
start_button.update(dt, events)
fullscreen_button.enabled = self.fullscreen
colorblind_button.enabled = self.colorblind_mode
fullscreen_button.draw(self.screen)
colorblind_button.draw(self.screen)
start_button.draw(self.screen)
self.update_display()
if self.fullscreen:
self.screen = pygame.display.set_mode((c.WINDOW_WIDTH, c.WINDOW_HEIGHT), flags=pygame.FULLSCREEN)
else:
self.screen = pygame.display.set_mode((c.WINDOW_WIDTH, c.WINDOW_HEIGHT))
def update_display(self):
pygame.display.flip()
def get_events(self):
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
dt = self.clock.tick(90)/1000
return events, dt
def spawn_scuttle(self, left=True):
y = c.WINDOW_HEIGHT * 0.73
if not left:
self.enemies.append(Scuttle(self, (c.WINDOW_WIDTH + 1000, y), (-1, 0)))
self.particles.append(WarningParticle((c.WINDOW_WIDTH - 50, y)))
else:
self.enemies.append(Scuttle(self, (-1000, y), (1, 0)))
self.particles.append(WarningParticle((50, y)))
def spawn_orb(self, left=True):
if left:
self.enemies.append(Orb(self, (-50, -random.random() * 200 - 50), direction=(1, 0)))
else:
self.enemies.append(Orb(self, (c.WINDOW_WIDTH + 50, -random.random() * 200 - 50), direction=(-1, 0)))
def game_start(self):
self.game_started = True
self.start_pos = self.xpos
pygame.mixer.music.play()
def init(self):
self.day = 1
self.xpos = 0
self.speed = 500
self.since_orb = 0
self.ccs_surf = None
self.start_pos = 0
self.alef_14 = pygame.font.Font("fonts/alef.ttf", 14)
self.shade = pygame.Surface((c.GAME_WIDTH, c.GAME_HEIGHT))
self.shade.fill((0, 0, 0))
self.shade_alpha = 255
self.really_lost = False
self.game_started = False
self.music = pygame.mixer.music.load("sounds/music.ogg")
self.player_hurt = pygame.mixer.Sound("sounds/hurt.wav")
self.mrew = pygame.mixer.Sound("sounds/mrew.wav")
self.mrew.set_volume(0.0)
self.title = pygame.image.load("images/title.png")
self.title.set_colorkey((255, 0, 0))
self.title_pos = int(c.WINDOW_HEIGHT * 0.3)
self.nope_sound = pygame.mixer.Sound("sounds/nope.wav")
self.nope_sound.set_volume(0.2)
self.restart_sound = pygame.mixer.Sound("sounds/restart.wav")
self.restart_sound.set_volume(0.5)
self.rewind_sound = pygame.mixer.Sound("sounds/rewind.wav")
self.pickup_battery = pygame.mixer.Sound("sounds/pickup_battery.wav")
self.pickup_battery.set_volume(0.1)
self.pickup_kunai = pygame.mixer.Sound("sounds/pickup_kunai.wav")
self.pickup_kunai.set_volume(0.1)
self.laser_sound = pygame.mixer.Sound("sounds/laser.wav")
self.laser_sound.set_volume(0.75)
self.explosion_sound = pygame.mixer.Sound("sounds/explosion.wav")
self.shoot_kunai_sound = pygame.mixer.Sound("sounds/throw_kunai.wav")
self.shoot_kunai_sound.set_volume(0.2)
self.laser_aim = pygame.mixer.Sound("sounds/laser_aim.wav")
self.laser_aim.set_volume(0.2)
self.kunai_hit = pygame.mixer.Sound("sounds/kunai_hit.wav")
self.kunai_hit.set_volume(0.8)
self.jump_sound = pygame.mixer.Sound("sounds/jump.wav")
self.jump_sound.set_volume(0.3)
self.press_e= pygame.image.load("images/press_e.png")
self.horizon = c.GAME_HEIGHT * 0.6
self.floor = c.GAME_HEIGHT - 140
self.game_time = 0
self.player = Player(self)
self.player.position = Pose((c.GAME_WIDTH//2, -100))
self.particles = []
self.enemies = []
self.water_texture = pygame.image.load("images/water.png")
self.background = pygame.image.load("images/bakground.png")
self.background_reflection = pygame.transform.flip(self.background, 0, 1)
self.background_buildings = pygame.image.load("images/background_buildings.png")
self.background_buildings.set_colorkey((255, 255, 255))
self.bb_reflection = pygame.transform.flip(self.background_buildings, 0, 1)
self.bb_reflection.set_colorkey((255, 255, 255))
self.water_shade = pygame.Surface((c.GAME_WIDTH, c.GAME_HEIGHT - self.horizon))
self.water_shade.fill((0, 0, 0))
self.foreground_buildings = pygame.image.load("images/foreground_buildings.png")
self.foreground_buildings.set_colorkey((255, 255, 255))
self.sun = pygame.image.load("images/sun.png")
self.train = pygame.image.load("images/train.png")
self.blue = pygame.image.load("images/blue.png")
self.blue = pygame.transform.scale(self.blue, (c.GAME_WIDTH, c.GAME_HEIGHT))
self.blue_color = self.blue.get_at((0, 0))
self.clock = pygame.time.Clock()
self.fps_font = pygame.font.SysFont("monospace", 12, bold=False)
self.fpss = [0]
self.lightener = pygame.Surface((c.GAME_WIDTH, c.GAME_HEIGHT))
self.lightener.fill((255, 255, 255))
self.lightener.set_alpha(30)
self.day_when_rewind = 1
self.rewinding = False
self.shake_amp = 0
self.shake_direction = Pose((1, 0))
self.since_shake = 0
self.kunai_ui = pygame.image.load("images/kunai_ui.png")
self.pickups = []
self.quad_ui = pygame.image.load("images/charge_quadrant_one.png")
self.quad_color_ui = pygame.image.load("images/charge_quad_color.png")
self.center_ui = pygame.image.load("images/charge_center.png")
self.center_color_ui = pygame.image.load("images/charge_center_color.png")
self.charge_back_ui = pygame.image.load("images/charge_background.png")
self.charge_glow = pygame.image.load("images/charge_glow.png")
self.since_scuttle = 0
def get_offset(self):
if self.shake_direction.magnitude() <1:
return (0, 0)
return (self.shake_direction * (1/self.shake_direction.magnitude()) * self.shake_amp * math.cos(self.since_shake*20)).get_position()
def update_background(self, dt, events):
self.since_shake += dt
self.shake_amp *= 0.01**dt
self.shake_amp = max(0, self.shake_amp - 5*dt)
self.xpos += self.speed * dt
self.game_time += dt - dt*0.8*self.rewinding
if self.game_started:
if not self.rewinding:
self.day -= (1/36.5)*dt
else:
pace = (1 - self.day_when_rewind)/3
self.day += pace*dt
if self.rewinding and self.day >= 1:
self.stop_rewinding()
if self.day < 0:
self.day = 0
lightness = self.day
bc = tuple([max(0, int(item * lightness)) for item in self.blue_color])
self.blue.fill(bc)
if self.rewinding and self.speed > 100:
self.speed = max(self.speed - 2000 * dt, 50)
if not self.rewinding:
self.speed = min(self.speed + 2000 * dt, 500)
def update_fps(self, dt, events):
self.fpss.append(1/dt)
self.fpss = self.fpss[-100:]
def draw_fps(self, surface):
return
fps = int(round(sum(self.fpss)/len(self.fpss), 0))
color = (0, 0, 0)
if self.day < 0.2:
color = (255, 255, 255)
label = self.fps_font.render("FPS: MIN AVG MAX", 0, color)
maxfps = max(self.fpss)
minfps = min(self.fpss)
text = f"{int(minfps)} | {fps} | {int(maxfps)}"
surf = self.fps_font.render(text, 0, color)
surface.blit(label, (c.GAME_WIDTH - label.get_width() - 10, 10))
surface.blit(surf, (c.GAME_WIDTH - surf.get_width() - 10, 10 + label.get_height()))
def draw_background(self, surf, offset=(0, 0)):
bgh = self.background.get_height()
margin = max(0, bgh - c.GAME_HEIGHT)
bgy = -margin * self.day
ba = pygame.Rect((0, -bgy, c.GAME_WIDTH, self.horizon))
surf.blit(self.background, (0, 0), area=ba)
bra = pygame.Rect((0, margin * (1 - self.day) + (c.GAME_HEIGHT - self.horizon)/2, c.GAME_WIDTH, c.GAME_HEIGHT - self.horizon))
surf.blit(self.background_reflection, (0, self.horizon), area=bra)
sun_peak_height = self.sun.get_height() * 1.5
sx = c.GAME_WIDTH//2 - self.sun.get_width()//2 + offset[0]
sy = int(self.horizon - sun_peak_height * self.day + offset[1])
sa = (0, 0, self.sun.get_width(), int(sun_peak_height * self.day))
surf.blit(self.sun, (sx, sy), area=sa)
bbw = self.background_buildings.get_width()
bbh = self.background_buildings.get_height()
bbx = int((-self.xpos * 0.1) % bbw - bbw + offset[0])
bby = int(self.horizon + offset[1])
while bbx < c.GAME_WIDTH:
surf.blit(self.background_buildings, (bbx, bby - bbh))
surf.blit(self.bb_reflection, (bbx, bby))
bbx += bbw
self.water_shade.set_alpha(60 + 20 * self.day)
surf.blit(self.water_shade, (0, self.horizon + offset[1]))
surf.blit(self.water_texture, (0, self.horizon + offset[1]), special_flags= pygame.BLEND_ADD)
fbw = self.foreground_buildings.get_width()
fbh = self.foreground_buildings.get_height()
fbx = int((-self.xpos * 0.7) % fbw - fbw + offset[0])
fby = int(c.GAME_HEIGHT + offset[1] - fbh + 30)
while fbx < c.GAME_WIDTH:
surf.blit(self.foreground_buildings, (fbx, fby))
fbx += fbw
surf.blit(self.blue, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
train_center_x = c.GAME_WIDTH//2
train_spacing = 5
train_width = self.train.get_width()
train_start_x = train_center_x - self.train.get_width()//2 + offset[0] - train_spacing - train_width
if self.colorblind_mode:
surf.blit((self.lightener), (0, 0))
for number in range(3):
xo, yo = self.get_train_offset(number)
train_x = int(train_start_x + number * train_spacing + number * train_width + xo)
train_y = int(self.floor - 5 + yo + offset[1])
surf.blit(self.train, (train_x, train_y), special_flags=pygame.BLEND_MULT)
def get_train_number(self, x):
train_center_x = c.GAME_WIDTH // 2
train_width = self.train.get_width()
if x < train_center_x - train_width//2 - 38:
return 0
elif x < train_center_x + train_width//2 - 35:
return 1
else:
return 2
def get_train_offset(self, number):
xo = -math.sin(self.game_time * 10 + number * 10) * 1.5
yo = math.cos(self.game_time * 10 + number * 10) * 1.5
return xo, yo
def start_rewind(self):
pygame.mixer.music.fadeout(250)
self.rewind_sound.play()
self.rewinding = True
self.player.start_tractor_beam()
self.day_when_rewind = self.day
if self.day_when_rewind == 1:
self.day_when_rewind = 0.999
for i in range(1000):
self.particles.append(RewindParticle(self))
self.particles.append(SunTint(duration=3, alpha=100))
self.destroy_all_enemies(silent=True)
def destroy_all_enemies(self, silent=False):
for enemy in self.enemies:
if not enemy.destroyed:
enemy.reward = 0
enemy.destroy(silent=silent)
def stop_rewinding(self):
pygame.mixer.music.rewind()
pygame.mixer.music.play()
self.mrew.play()
self.restart_sound.play()
self.rewinding = False
self.day = 1
self.player.end_tractor_beam()
self.particles.append(SunExplosion(self))
self.player.charge = 0
def lose(self):
if self.lost:
return
self.particles.append(SunExplosionLong(self, duration=6, color=(255, 0, 0), callback=self.really_lose))
self.lost = True
self.last_distance = self.xpos
def get_train_offset_from_x(self, x):
return self.get_train_offset(self.get_train_number(x))
def draw_hud(self, surf, offset=(0, 0)):
x = c.GAME_WIDTH//2 - (self.player.ammo - 1) * 10 - 18
y = c.GAME_HEIGHT - 45
for i in range(self.player.ammo):
surf.blit(self.kunai_ui, (x, y))
x += 20
charge_per_section = 25
max_charge = charge_per_section * 5
quads = self.player.charge//charge_per_section
remainder = self.player.charge%charge_per_section
x = c.WINDOW_WIDTH//2 - self.charge_back_ui.get_width()//2
y = c.WINDOW_HEIGHT - 105 - self.charge_back_ui.get_height()//22
quad = self.quad_ui.copy()
quad_color = self.quad_color_ui.copy()
quad_color.set_colorkey((0, 0, 0))
center_color = self.center_color_ui.copy()
center_color.set_colorkey((0, 0, 0))
if quads >= 5:
glow = self.charge_glow.copy()
dark = pygame.Surface(glow.get_size())
dark.fill((0, 0, 0))
dark.set_alpha(50 + 128 * math.sin(self.game_time*6))
glow.blit(dark, (0, 0))
surf.blit(glow, (x - glow.get_width()//2 + quad.get_width()//2, y), special_flags=pygame.BLEND_ADD)
if not self.rewinding and self.game_time%1 < 0.7:
pos = c.WINDOW_WIDTH//2 - self.press_e.get_width()//2, y - 28
surf.blit(self.press_e, pos)
surf.blit(self.charge_back_ui, (x, y))
for i in range(4):
if i >= quads:
break
surf.blit(quad, (x, y))
quad = pygame.transform.rotate(quad, -90)
quad_color = pygame.transform.rotate(quad_color, -90)
if quads < 4:
quad_color.set_alpha(remainder/charge_per_section * 255)
surf.blit(quad_color, (x, y))
elif quads < 5:
center_color.set_alpha(remainder/charge_per_section*255)
surf.blit(center_color, (x, y))
if quads >= 5:
surf.blit(self.center_ui, (x, y))
pixels = self.xpos
if not self.game_started and self.last_distance:
pixels = self.last_distance
miles = round((100000 - pixels + self.start_pos)/1000, 1)
if not self.game_started and not self.last_distance:
miles = 100
city_center_string = f"{miles} miles to city center"
context_string = f"Last run:"
ccs_string = self.alef_14.render(f"{miles}", 1, (255, 255, 255))
if not self.ccs_surf:
self.ccs_surf = self.alef_14.render(" miles to city center", 1, (255, 255, 255))
surf.blit(ccs_string, (c.WINDOW_WIDTH - ccs_string.get_width() - self.ccs_surf.get_width() - 5, c.WINDOW_HEIGHT - ccs_string.get_height() - 3))
surf.blit(self.ccs_surf, (c.WINDOW_WIDTH - self.ccs_surf.get_width() - 5, c.WINDOW_HEIGHT - self.ccs_surf.get_height() - 3))
if self.last_distance and not self.game_started:
cx_string = self.alef_14.render(context_string, 1, (255, 255, 255))
surf.blit(cx_string, (c.WINDOW_WIDTH - cx_string.get_width() - 5, c.WINDOW_HEIGHT - cx_string.get_height() - 19))
def get_multiplier(self):
return ((self.xpos - self.start_pos)/14000 + 3)/3
def main(self):
self.lost = False
self.clock.tick(60)
while True:
events, dt = self.get_events()
if dt > 1/20:
dt = 1/20
self.update_background(dt, events)
self.update_fps(dt, events)
if not self.game_started:
self.speed = 100
self.shade_alpha -= 1500 * dt
if self.game_started:
keep_particles = []
for particle in self.particles:
particle.update(dt, events)
if not particle.destroyed:
keep_particles.append(particle)
self.particles = keep_particles
if self.lost:
dt *= 0.01
events = [event for event in events if event.type != pygame.KEYDOWN and event.type != pygame.MOUSEBUTTONDOWN]
self.player.update(dt, events)
for pickup in self.pickups[:]:
pickup.update(dt, events)
for enemy in self.enemies[:]:
enemy.update(dt, events)
if (enemy.position - self.player.position).magnitude() > c.WINDOW_WIDTH * 3:
self.enemies.remove(enemy)
self.since_scuttle += dt
self.since_orb += dt
if self.since_scuttle > 3.5 / self.get_multiplier():
self.since_scuttle = 0
self.spawn_scuttle(random.choice([0, 1]))
if self.get_multiplier() > 1.4:
if self.since_orb > 8 / self.get_multiplier():
has_left = False
has_right = False
for enemy in self.enemies:
if type(enemy) == Orb:
if enemy.direction.x < 0:
has_right = True
else:
has_left = True
if not has_left and not has_right:
self.spawn_orb(random.choice((1, 0)))
self.since_orb = 0
elif not (has_left and has_right):
self.spawn_orb(has_right)
self.since_orb = 0
offset = self.get_offset()
self.draw_background(self.screen, offset)
self.draw_hud(self.screen)
for pickup in self.pickups:
pickup.draw(self.screen, offset)
for enemy in self.enemies:
enemy.draw(self.screen, offset)
for particle in self.particles[:]:
particle.draw(self.screen, offset)
self.player.draw(self.screen, offset)
self.draw_fps(self.screen)
x = c.GAME_WIDTH//2 - self.title.get_width()//2
y = int(c.GAME_HEIGHT * 0.3) - self.title.get_height()//2
self.screen.blit(self.title, (x, y))
if self.game_started:
self.title_pos -= 1000 * dt
self.title.set_alpha(self.title_pos * 255 / c.GAME_HEIGHT / 0.3)
if self.shade_alpha > 0:
self.shade.set_alpha(self.shade_alpha)
self.screen.blit(self.shade, (0, 0))
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if not self.game_started:
self.game_start()
if self.day <= 0:
self.lose()
self.update_display()
if self.really_lost:
break
if self.xpos >= 100000:
self.win()
def win(self):
self.victory_screen(self.screen.copy())
def really_lose(self):
self.really_lost = True
def shake(self, direction=None, amt=15):
if amt < self.shake_amp:
return
else:
self.shake_amp = amt
if direction and Pose(direction).magnitude() > 0:
self.shake_direction = Pose(direction)
else:
self.shake_direction = Pose((1, 1))
self.since_shake = 0
if __name__=="__main__":
Game()