-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb.py
More file actions
631 lines (585 loc) · 25.8 KB
/
Copy pathb.py
File metadata and controls
631 lines (585 loc) · 25.8 KB
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
"""
CSE Pro Arcade — 'Project by Preet (CSE-style)'
Single-file Python game using pygame.
Features: menu/settings/pause, enemies, waves, boss, powerups, shop, achievements, persistent highscore/settings.
"""
import pygame, sys, random, math, json, os
from pathlib import Path
# ---------------- Config ----------------
WIDTH, HEIGHT = 1000, 640
TITLE = "CSE Pro Arcade"
FPS = 60
DATA_FILE = "cse_pro_save.json"
# Gameplay tuning
PLAYER_MAX_SPEED = 380
PLAYER_ACCEL = 2500
PLAYER_DRAG = 12
BULLET_SPEED = 980
BULLET_COOLDOWN = 0.16
POWERUP_DURATION = 6.0 # seconds
SHOP_OPTIONS = [
{"name": "Max Health +1", "cost": 30, "key":"hp"},
{"name": "Bullet Damage +1", "cost": 50, "key":"dmg"},
{"name": "Cooldown -10%", "cost": 80, "key":"cd"},
]
FONT_NAME = None
# ---------------- Helpers ----------------
def clamp(v, a, b): return max(a, min(b, v))
def load_save():
p = Path(DATA_FILE)
if not p.exists():
return {"highscore":0, "settings":{"sound":True}, "coins":0}
try:
with p.open('r') as f:
return json.load(f)
except Exception:
return {"highscore":0, "settings":{"sound":True}, "coins":0}
def save_save(data):
try:
with open(DATA_FILE, 'w') as f:
json.dump(data, f)
except Exception:
pass
# ---------------- Game Objects ----------------
class Particle:
def __init__(self,pos,vel,life,radius,color):
self.pos = pygame.Vector2(pos)
self.vel = pygame.Vector2(vel)
self.life = life
self.maxlife = life
self.radius = radius
self.color = color
def update(self,dt):
self.pos += self.vel*dt
self.vel *= 0.98
self.life -= dt
def draw(self,s):
if self.life<=0: return
a = clamp(int(255*(self.life/self.maxlife)),0,255)
surf = pygame.Surface((self.radius*2,self.radius*2), pygame.SRCALPHA)
pygame.draw.circle(surf, (*self.color,a),(self.radius,self.radius),int(self.radius))
s.blit(surf,(self.pos.x-self.radius,self.pos.y-self.radius))
class Bullet:
def __init__(self,pos,vel,owner="player",damage=1):
self.pos = pygame.Vector2(pos)
self.vel = pygame.Vector2(vel)
self.radius = 4 if owner=="player" else 7
self.owner = owner
self.damage = damage
self.alive = True
def update(self,dt):
self.pos += self.vel*dt
if self.pos.y < -50 or self.pos.y > HEIGHT+50 or self.pos.x<-50 or self.pos.x>WIDTH+50:
self.alive = False
def draw(self,s):
col = (180,240,255) if self.owner=="player" else (255,150,150)
pygame.draw.circle(s,(col), (int(self.pos.x),int(self.pos.y)), self.radius)
def rect(self):
return pygame.Rect(self.pos.x-self.radius,self.pos.y-self.radius,self.radius*2,self.radius*2)
class Player:
def __init__(self):
self.pos = pygame.Vector2(WIDTH/2, HEIGHT-90)
self.vel = pygame.Vector2(0,0)
self.w, self.h = 44, 34
self.cooldown = 0.0
self.lives = 3
self.max_lives = 3
self.score = 0
self.coins = 0
self.damage = 1
self.rapid = False
self.rapid_timer = 0
self.shield = False
self.shield_timer = 0
self.multiplier = 1.0
self.mult_timer = 0
def rect(self):
return pygame.Rect(self.pos.x-self.w/2, self.pos.y-self.h/2, self.w, self.h)
def update(self,dt,keys):
# movement input
target = pygame.Vector2(0,0)
if keys[pygame.K_a] or keys[pygame.K_LEFT]: target.x = -1
if keys[pygame.K_d] or keys[pygame.K_RIGHT]: target.x = 1
if keys[pygame.K_w] or keys[pygame.K_UP]: target.y = -1
if keys[pygame.K_s] or keys[pygame.K_DOWN]: target.y = 1
if target.length_squared()>0:
target = target.normalize()*PLAYER_MAX_SPEED
acc = (target - self.vel)
self.vel += acc * clamp(dt * (PLAYER_ACCEL/1000.0), 0, 1)
else:
self.vel -= self.vel * clamp(PLAYER_DRAG*dt, 0, 1)
if self.vel.length() > PLAYER_MAX_SPEED:
self.vel.scale_to_length(PLAYER_MAX_SPEED)
self.pos += self.vel*dt
self.pos.x = clamp(self.pos.x, 24, WIDTH-24)
self.pos.y = clamp(self.pos.y, 40, HEIGHT-40)
# cooldown
self.cooldown = max(0, self.cooldown - dt)
# powerup timers
if self.rapid:
self.rapid_timer -= dt
if self.rapid_timer <= 0: self.rapid = False
if self.shield:
self.shield_timer -= dt
if self.shield_timer <= 0: self.shield = False
if self.multiplier>1.0:
self.mult_timer -= dt
if self.mult_timer<=0: self.multiplier=1.0
def draw(self,s):
p = self.pos
# ship body
points = [(p.x, p.y-16),(p.x+20,p.y+10),(p.x+6,p.y+6),(p.x-6,p.y+6),(p.x-20,p.y+10)]
pygame.draw.polygon(s,(200,230,255),points)
pygame.draw.polygon(s,(20,50,150),points,2)
# shield
if self.shield:
pygame.draw.circle(s,(120,180,255,80),(int(p.x),int(p.y)),36,2)
class Enemy:
# type: "normal","fast","tank","boss"
def __init__(self,x,y,etype="normal",speed=80,hp=1):
self.pos = pygame.Vector2(x,y)
self.etype = etype
self.speed = speed
self.hp = hp
self.radius = 18 if etype!="boss" else 60
self.alive = True
self.shoot_timer = random.uniform(1.0,3.0) if etype in ("tank","boss") else None
self.dirx = random.choice([-1,1]) if etype=="fast" else 0
def update(self,dt,player_pos):
# simple AI
if self.etype == "fast":
self.pos.x += self.dirx * self.speed * dt
if self.pos.x < 20 or self.pos.x > WIDTH-20:
self.dirx *= -1
else:
# slight horizontal follow
dx = player_pos.x - self.pos.x
self.pos.x += clamp(dx, -1, 1) * (self.speed*0.12) * dt
self.pos.y += self.speed * dt
if self.pos.y > HEIGHT + 100:
self.alive = False
if self.shoot_timer is not None:
self.shoot_timer -= dt
def draw(self,s):
x,y = int(self.pos.x), int(self.pos.y)
if self.etype == "normal":
pygame.draw.circle(s,(240,120,140),(x,y),self.radius)
pygame.draw.circle(s,(120,20,30),(x,y),self.radius,2)
elif self.etype == "fast":
pygame.draw.polygon(s, (220,180,120), [(x,y-16),(x+18,y+12),(x-18,y+12)])
pygame.draw.polygon(s,(80,40,0),[(x,y-16),(x+18,y+12),(x-18,y+12)],2)
elif self.etype == "tank":
pygame.draw.rect(s,(200,80,120),(x-20,y-14,40,28))
pygame.draw.circle(s,(120,20,40),(x,y),12)
elif self.etype == "boss":
pygame.draw.circle(s,(180,60,200),(x,y),self.radius)
# hp bar
hpw = int(200 * clamp(self.hp/200.0,0,1))
pygame.draw.rect(s,(50,50,50),(x-100,y-self.radius-20,200,10))
pygame.draw.rect(s,(200,40,40),(x-100,y-self.radius-20,hpw,10))
def rect(self):
return pygame.Rect(self.pos.x-self.radius,self.pos.y-self.radius,self.radius*2,self.radius*2)
class Powerup:
# kind: "health","rapid","shield","coin","mult"
def __init__(self,pos,kind):
self.pos = pygame.Vector2(pos)
self.kind = kind
self.radius = 12
self.alive = True
def update(self,dt):
self.pos.y += 40*dt
if self.pos.y > HEIGHT+10: self.alive = False
def draw(self,s):
x,y = int(self.pos.x), int(self.pos.y)
color = {"health":(150,255,150),"rapid":(255,200,80),"shield":(120,180,255),"coin":(255,220,100),"mult":(200,140,255)}.get(self.kind,(200,200,200))
pygame.draw.circle(s,color,(x,y),self.radius)
pygame.draw.circle(s,(30,30,30),(x,y),self.radius,2)
def rect(self): return pygame.Rect(self.pos.x-self.radius,self.pos.y-self.radius,self.radius*2,self.radius*2)
# ---------------- Main Game Class ----------------
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption(TITLE)
self.screen = pygame.display.set_mode((WIDTH,HEIGHT))
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(FONT_NAME, 20)
self.bigfont = pygame.font.Font(FONT_NAME, 38)
self.smallfont = pygame.font.Font(FONT_NAME, 14)
self.running = True
# load save
self.save = load_save()
self.highscore = int(self.save.get("highscore",0))
self.settings = self.save.get("settings",{"sound":True})
self.global_coins = int(self.save.get("coins",0))
# simple procedural sounds
try:
pygame.mixer.init()
self.sfx_shoot = pygame.mixer.Sound(self._tone(600,0.05))
self.sfx_explode = pygame.mixer.Sound(self._tone(160,0.12))
self.sfx_pick = pygame.mixer.Sound(self._tone(880,0.04))
except Exception:
self.sfx_shoot = self.sfx_explode = self.sfx_pick = None
self.reset()
self.state = "menu" # menu, playing, paused, shop, gameover, settings
def _tone(self, freq, seconds=0.1, volume=0.15):
framerate = 44100
n = int(seconds*framerate)
buf = bytearray()
amp = int(32767*volume)
for t in range(n):
v = int(amp*math.sin(2*math.pi*freq*t/framerate))
buf += int.to_bytes(v & 0xffff, 2, 'little', signed=False)
return bytes(buf)
def reset(self):
self.player = Player()
self.bullets = []
self.enemies = []
self.epbullets = []
self.particles = []
self.powerups = []
self.spawn_timer = 0.8
self.spawn_rate = 0.9
self.wave = 1
self.wave_timer = 0.0
self.level_score = 0
self.coins_earned = 0
self.difficulty = 1.0
self.enemy_kill_count = 0
self.achievements = {"first_kill":False,"hundred_score":False}
self.star_layers = self._make_stars()
self.boss = None
def _make_stars(self):
layers=[]
for speed,count,sz in ((20,90,2),(60,45,2),(140,18,1)):
stars=[]
for _ in range(count):
stars.append([random.uniform(0,WIDTH), random.uniform(0,HEIGHT), sz])
layers.append((speed,stars))
return layers
def spawn_enemy(self):
et = random.choices(["normal","fast","tank"], weights=[60,25,15])[0]
x = random.uniform(30,WIDTH-30)
if et=="normal":
self.enemies.append(Enemy(x,-40,"normal",speed=random.uniform(60,110),hp=1))
elif et=="fast":
self.enemies.append(Enemy(x,-40,"fast",speed=random.uniform(140,210),hp=1))
else:
self.enemies.append(Enemy(x,-40,"tank",speed=random.uniform(30,60),hp=3))
def spawn_powerup(self,pos):
kind = random.choices(["health","rapid","shield","coin","mult"], weights=[25,25,20,20,10])[0]
self.powerups.append(Powerup(pos,kind))
def spawn_explosion(self,pos,power=1.0):
for _ in range(int(14*power)):
ang = random.uniform(0,2*math.pi)
spd = random.uniform(60,320)*power
vel = pygame.Vector2(math.cos(ang)*spd, math.sin(ang)*spd)
life = random.uniform(0.4,0.9)
r = random.uniform(2,5)*power
col = (255,200,100)
self.particles.append(Particle(pos,vel,life,r,col))
def handle_input(self,dt):
keys = pygame.key.get_pressed()
self.player.update(dt,keys)
mouse = pygame.mouse.get_pressed()
# shoot
shoot_pressed = keys[pygame.K_SPACE] or mouse[0]
cd = BULLET_COOLDOWN * (0.75 if self.player.rapid else 1.0)
if self.player.cooldown<=0 and shoot_pressed:
self.player.cooldown = cd
bpos = self.player.pos - pygame.Vector2(0,12)
self.bullets.append(Bullet(bpos, pygame.Vector2(0,-BULLET_SPEED), owner="player", damage=self.player.damage))
if self.sfx_shoot and self.settings.get("sound",True):
try: self.sfx_shoot.play()
except: pass
def update(self,dt):
if self.state!="playing":
return
# starfield
for speed,stars in self.star_layers:
for s in stars:
s[1] += speed*dt
if s[1] > HEIGHT: s[1] = -2; s[0] = random.uniform(0,WIDTH)
self.handle_input(dt)
# bullets update
for b in self.bullets: b.update(dt)
for b in self.epbullets: b.update(dt)
self.bullets = [b for b in self.bullets if b.alive]
self.epbullets = [b for b in self.epbullets if b.alive]
# enemies spawn and update
if self.boss is None:
self.spawn_timer -= dt
if self.spawn_timer<=0:
self.spawn_enemy()
self.spawn_timer = clamp(self.spawn_rate * random.uniform(0.6,1.2), 0.25, 2.2)
# difficulty ramp
self.difficulty += dt*0.004
if self.difficulty>10: self.difficulty=10
# boss trigger every 5 waves
if self.wave%5==0 and self.enemy_kill_count >= 12 + (self.wave//5)*2:
# spawn boss
self.boss = Enemy(WIDTH/2, -220, "boss", speed=20, hp=200)
for e in self.enemies: e.update(dt, self.player.pos)
if self.boss:
self.boss.update(dt, self.player.pos)
# boss shooting behavior
if self.boss.shoot_timer is None:
self.boss.shoot_timer = 1.6
if self.boss.shoot_timer <= 0:
# shoot 5 bullets downward spread
for i in range(-2,3):
vel = pygame.Vector2(i*80, 220)
bpos = self.boss.pos + pygame.Vector2(0,40)
self.epbullets.append(Bullet(bpos,vel,owner="enemy",damage=3))
self.boss.shoot_timer = 1.6
else:
self.boss.shoot_timer -= dt
# enemy bullets: simple
for e in self.enemies:
if e.shoot_timer is not None and e.shoot_timer<=0:
# shoot at player
dirv = (self.player.pos - e.pos).normalize()
self.epbullets.append(Bullet(e.pos+pygame.Vector2(0,10), dirv*260, owner="enemy", damage=1))
e.shoot_timer = random.uniform(1.2,3.0)
# collisions bullets vs enemies
for b in self.bullets:
for e in list(self.enemies) + ([self.boss] if self.boss else []):
if e is None: continue
if b.alive and e.alive and b.rect().colliderect(e.rect()):
b.alive = False
e.hp -= b.damage
if e.hp <= 0:
e.alive = False
self.spawn_explosion(e.pos, power=1.2)
self.player.score += int(10 * self.player.multiplier)
self.enemy_kill_count += 1
self.level_score += 10
gained = random.randint(3,10)
self.coins_earned += gained
self.player.coins += gained
if self.sfx_explode and self.settings.get("sound",True):
try: self.sfx_explode.play()
except: pass
# chance powerup
if random.random() < 0.22:
self.spawn_powerup(e.pos)
# boss death
if e is self.boss and not e.alive:
# big reward
self.player.score += 250 * int(self.player.multiplier)
self.coins_earned += 200
self.player.coins += 200
self.spawn_explosion(e.pos, power=3.0)
self.boss = None
self.wave += 1
# enemy bullets vs player
for eb in self.epbullets:
if eb.alive and eb.rect().colliderect(self.player.rect()):
eb.alive = False
if not self.player.shield:
self.player.lives -= 1
self.spawn_explosion(self.player.pos, power=0.8)
if self.player.lives<=0:
# game over
self.state = "gameover"
if self.player.score > self.highscore:
self.highscore = self.player.score
self.save["highscore"] = self.highscore
self.save["coins"] = self.global_coins + self.coins_earned
save_save(self.save)
else:
# shield absorbs
self.player.shield = False
# enemies reaching player
for e in list(self.enemies):
if e.alive and e.rect().colliderect(self.player.rect()):
e.alive = False
self.spawn_explosion(self.player.pos)
if not self.player.shield:
self.player.lives -= 1
if self.player.lives<=0:
self.state = "gameover"
if self.player.score > self.highscore:
self.highscore = self.player.score
self.save["highscore"] = self.highscore
self.save["coins"] = self.global_coins + self.coins_earned
save_save(self.save)
# cleanup
self.enemies = [e for e in self.enemies if e.alive]
self.particles = [p for p in self.particles if p.life>0]
for p in self.particles: p.update(dt)
for pu in self.powerups:
pu.update(dt)
if pu.alive and pu.rect().colliderect(self.player.rect()):
pu.alive=False
self.apply_powerup(pu.kind)
if self.sfx_pick and self.settings.get("sound",True):
try: self.sfx_pick.play()
except: pass
self.powerups = [pu for pu in self.powerups if pu.alive]
# bullets update already done above
# check achievements
if not self.achievements["first_kill"] and self.enemy_kill_count>0:
self.achievements["first_kill"] = True
if not self.achievements["hundred_score"] and self.player.score>=100:
self.achievements["hundred_score"] = True
def apply_powerup(self,kind):
if kind=="health":
self.player.lives = min(self.player.max_lives, self.player.lives+1)
elif kind=="rapid":
self.player.rapid = True
self.player.rapid_timer = POWERUP_DURATION
elif kind=="shield":
self.player.shield = True
self.player.shield_timer = POWERUP_DURATION
elif kind=="coin":
gained = random.randint(8,25)
self.player.coins += gained
self.coins_earned += gained
elif kind=="mult":
self.player.multiplier = 2.0
self.player.mult_timer = POWERUP_DURATION
def draw_ui(self,s):
# score, lives, coins, level
score_s = self.font.render(f"Score: {self.player.score}", True, (230,230,230))
lives_s = self.font.render(f"Lives: {self.player.lives}/{self.player.max_lives}", True, (230,230,230))
coins_s = self.font.render(f"Coins: {self.player.coins}", True, (230,230,180))
wave_s = self.font.render(f"Wave: {self.wave}", True, (200,200,255))
hs_s = self.font.render(f"Highscore: {self.highscore}", True, (180,180,240))
s.blit(score_s,(12,12)); s.blit(lives_s,(12,36)); s.blit(coins_s,(12,60))
s.blit(wave_s,(WIDTH-140,12)); s.blit(hs_s,(WIDTH-140,36))
fps = int(self.clock.get_fps())
fps_s = self.font.render(f"FPS: {fps}", True, (200,200,200))
s.blit(fps_s,(WIDTH-90,60))
def draw(self):
s = self.screen
s.fill((10,12,22))
# starfield
for idx,(speed,stars) in enumerate(self.star_layers):
for x,y,sz in stars:
shade = 200 - idx*60
pygame.draw.circle(s,(shade,shade,shade),(int(x),int(y)),sz)
# draw bullets/enemies/etc
for b in self.bullets: b.draw(s)
for eb in self.epbullets: eb.draw(s)
for e in self.enemies: e.draw(s)
if self.boss: self.boss.draw(s)
for pu in self.powerups: pu.draw(s)
for p in self.particles: p.draw(s)
self.player.draw(s)
self.draw_ui(s)
if self.state=="menu":
self._draw_menu()
elif self.state=="paused":
self._draw_center("Paused - Press P to resume")
elif self.state=="shop":
self._draw_shop()
elif self.state=="settings":
self._draw_settings()
elif self.state=="gameover":
self._draw_gameover()
pygame.display.flip()
def _draw_center(self,txt):
t = self.bigfont.render(txt, True, (240,240,240))
self.screen.blit(t,(WIDTH//2 - t.get_width()//2, HEIGHT//2 - t.get_height()//2))
def _draw_menu(self):
title = self.bigfont.render("CSE Pro Arcade", True, (220,220,255))
sub = self.font.render("Enter: Play S: Settings Esc: Quit", True, (200,200,200))
self.screen.blit(title,(WIDTH//2 - title.get_width()//2, 120))
self.screen.blit(sub,(WIDTH//2 - sub.get_width()//2, 200))
def _draw_shop(self):
box = pygame.Rect(WIDTH//2-320, HEIGHT//2-160, 640, 320)
pygame.draw.rect(self.screen,(20,20,30),box)
pygame.draw.rect(self.screen,(80,80,120),box,2)
t = self.bigfont.render("Upgrade Shop", True, (220,220,255))
self.screen.blit(t,(box.x+20,box.y+12))
desc = self.smallfont.render("Use Left/Right to choose. Enter to buy. Esc to skip.", True, (200,200,200))
self.screen.blit(desc,(box.x+20,box.y+56))
# options
for i,opt in enumerate(SHOP_OPTIONS):
x = box.x + 40 + i*210
y = box.y + 110
rect = pygame.Rect(x,y,180,80)
pygame.draw.rect(self.screen,(40,40,50),rect)
txt = self.font.render(opt["name"],True,(220,220,220))
cost = self.font.render(f"Cost: {opt['cost']}", True, (200,200,120))
self.screen.blit(txt,(x+8,y+8)); self.screen.blit(cost,(x+8,y+44))
coins = self.font.render(f"Your Coins: {self.player.coins}",True,(255,220,100))
self.screen.blit(coins,(box.x+20,box.y+250))
def _draw_settings(self):
t = self.bigfont.render("Settings", True, (220,220,255))
self.screen.blit(t,(WIDTH//2-80, 120))
sound_txt = "Sound: ON" if self.settings.get("sound",True) else "Sound: OFF"
s = self.font.render(f"[M] Toggle {sound_txt} [B] Back", True, (200,200,200))
self.screen.blit(s,(WIDTH//2 - s.get_width()//2, 200))
def _draw_gameover(self):
t = self.bigfont.render("Game Over", True, (240,160,160))
s1 = self.font.render(f"Final Score: {self.player.score}", True, (220,220,220))
s2 = self.font.render(f"Coins Earned: {self.coins_earned}", True, (220,220,160))
s3 = self.font.render("Enter: Play Again Esc: Quit", True, (200,200,200))
self.screen.blit(t,(WIDTH//2 - t.get_width()//2,120))
self.screen.blit(s1,(WIDTH//2 - s1.get_width()//2,200))
self.screen.blit(s2,(WIDTH//2 - s2.get_width()//2,230))
self.screen.blit(s3,(WIDTH//2 - s3.get_width()//2,300))
def _handle_events(self):
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
self.running = False
elif ev.type == pygame.KEYDOWN:
if self.state=="menu":
if ev.key==pygame.K_RETURN:
self.reset()
self.state="playing"
elif ev.key==pygame.K_s:
self.state="settings"
elif ev.key==pygame.K_ESCAPE:
self.running=False
elif self.state=="playing":
if ev.key==pygame.K_p:
self.state="paused"
elif ev.key==pygame.K_ESCAPE:
# quick open shop between waves
self.state="shop"
elif self.state=="paused":
if ev.key==pygame.K_p:
self.state="playing"
elif self.state=="shop":
if ev.key==pygame.K_ESCAPE:
# save coins to global
self.global_coins += self.coins_earned
self.save["coins"] = self.global_coins
save_save(self.save)
self.state="playing"
elif self.state=="settings":
if ev.key==pygame.K_m:
self.settings["sound"] = not self.settings.get("sound",True)
elif ev.key==pygame.K_b:
self.state="menu"
self.save["settings"] = self.settings
save_save(self.save)
elif self.state=="gameover":
if ev.key==pygame.K_RETURN:
# save coins and highscore
self.global_coins += self.coins_earned
self.save["coins"] = self.global_coins
self.save["highscore"] = max(self.highscore, self.player.score)
save_save(self.save)
self.reset()
self.state="playing"
elif ev.key==pygame.K_ESCAPE:
self.running=False
elif ev.type==pygame.MOUSEBUTTONDOWN:
if ev.button==1 and self.state=="playing":
# left click shoot handled by mouse state on update
pass
def run(self):
while self.running:
dt = self.clock.tick(FPS)/1000.0
self._handle_events()
self.update(dt)
self.draw()
pygame.quit()
sys.exit()
# ----------------
# Run ----------------
if __name__ == "__main__":
Game().run()