Deathscreen done.
This commit is contained in:
parent
59efcba73e
commit
9fbeee827b
2 changed files with 98 additions and 7 deletions
1
highscores.json
Normal file
1
highscores.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"hard": 32}
|
||||||
104
main.py
104
main.py
|
|
@ -1,5 +1,7 @@
|
||||||
import random
|
import random
|
||||||
import pygame
|
import pygame
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
|
|
@ -165,6 +167,24 @@ def new_sugar():
|
||||||
random.randint(0, HEIGHT - 20), 50, 50) #20,s
|
random.randint(0, HEIGHT - 20), 50, 50) #20,s
|
||||||
|
|
||||||
|
|
||||||
|
SAVE_PATH = Path(__file__).with_name("highscores.json")
|
||||||
|
|
||||||
|
def load_best(mode):
|
||||||
|
try:
|
||||||
|
scores = json.loads(SAVE_PATH.read_text())
|
||||||
|
except (FileNotFoundError, json.JSONDecodeError):
|
||||||
|
return 0
|
||||||
|
return scores.get(mode, 0)
|
||||||
|
|
||||||
|
def save_best(mode, score):
|
||||||
|
try:
|
||||||
|
scores = json.loads(SAVE_PATH.read_text())
|
||||||
|
except (FileNotFoundError, json.JSONDecodeError):
|
||||||
|
scores = {}
|
||||||
|
scores[mode] = score
|
||||||
|
SAVE_PATH.write_text(json.dumps(scores))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -178,14 +198,15 @@ def game_screen(mode):
|
||||||
sugar = new_sugar()
|
sugar = new_sugar()
|
||||||
lives = cfg["lives"]
|
lives = cfg["lives"]
|
||||||
hit_timer = 0
|
hit_timer = 0
|
||||||
|
start_ticks = pygame.time.get_ticks()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
return "quit", score
|
return "quit", {}
|
||||||
|
|
||||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||||
return "menu", score
|
return "menu", {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -214,7 +235,11 @@ def game_screen(mode):
|
||||||
hit_timer = 90
|
hit_timer = 90
|
||||||
break
|
break
|
||||||
if lives <= 0:
|
if lives <= 0:
|
||||||
return "gameover", score
|
return "gameover", {
|
||||||
|
"score": score,
|
||||||
|
"snapshot": screen.copy(),
|
||||||
|
"seconds": (pygame.time.get_ticks() - start_ticks) // 1000,
|
||||||
|
}
|
||||||
|
|
||||||
#keep player in
|
#keep player in
|
||||||
player.clamp_ip(screen.get_rect())
|
player.clamp_ip(screen.get_rect())
|
||||||
|
|
@ -388,11 +413,77 @@ def mode_screen():
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
clock.tick(60)
|
clock.tick(60)
|
||||||
|
|
||||||
|
def game_over_screen(mode, result):
|
||||||
|
snapshot = result["snapshot"]
|
||||||
|
score = result["score"]
|
||||||
|
seconds = result.get("seconds", 0)
|
||||||
|
|
||||||
|
best = load_best(mode)
|
||||||
|
new_best = score > best
|
||||||
|
if new_best:
|
||||||
|
save_best(mode, score)
|
||||||
|
best = score
|
||||||
|
|
||||||
|
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
|
||||||
|
fade = 0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return "quit", {}
|
||||||
|
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key in (pygame.K_RETURN, pygame.K_r):
|
||||||
|
return "game", {}
|
||||||
|
elif event.key == pygame.K_m:
|
||||||
|
return "mode", {}
|
||||||
|
elif event.key == pygame.K_ESCAPE:
|
||||||
|
return "menu", {}
|
||||||
|
elif event.key == pygame.K_q:
|
||||||
|
return "quit", {}
|
||||||
|
|
||||||
|
if fade < 180:
|
||||||
|
fade = min(180, fade + 12)
|
||||||
|
|
||||||
|
screen.blit(snapshot, (0, 0))
|
||||||
|
overlay.fill((0, 0, 0, fade))
|
||||||
|
screen.blit(overlay, (0, 0))
|
||||||
|
|
||||||
|
title = big_font.render("GAME OVER", True, (255, 240, 8))
|
||||||
|
screen.blit(title, title.get_rect(center=(WIDTH // 2, 130)))
|
||||||
|
|
||||||
|
score_text = font.render(f"Score: {score}", True, (255, 255, 255))
|
||||||
|
screen.blit(score_text, score_text.get_rect(center=(WIDTH // 2, 205)))
|
||||||
|
|
||||||
|
if new_best:
|
||||||
|
best_text = small_font.render("NEW BEST", True, (255, 240, 8))
|
||||||
|
else:
|
||||||
|
best_text = small_font.render(f"best {best}", True, (255, 255, 255))
|
||||||
|
screen.blit(best_text, best_text.get_rect(center=(WIDTH // 2, 245)))
|
||||||
|
|
||||||
|
stats = small_font.render(
|
||||||
|
f"{mode} · {seconds}s survived", True, (255, 255, 255)
|
||||||
|
)
|
||||||
|
screen.blit(stats, stats.get_rect(center=(WIDTH // 2, 285)))
|
||||||
|
|
||||||
|
prompts = [
|
||||||
|
"ENTER-RETRY",
|
||||||
|
"M-change mode",
|
||||||
|
"esc-main menu",
|
||||||
|
"Q-quit",
|
||||||
|
]
|
||||||
|
for i, text in enumerate(prompts):
|
||||||
|
prompt = font.render(text, True, (255, 255, 255))
|
||||||
|
screen.blit(prompt, prompt.get_rect(center=(WIDTH // 2, 345 + i * 36)))
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
state = "menu"
|
state = "menu"
|
||||||
mode = None
|
mode = None
|
||||||
score = 0
|
payload = {}
|
||||||
|
|
||||||
while state != "quit":
|
while state != "quit":
|
||||||
if state == "menu":
|
if state == "menu":
|
||||||
|
|
@ -400,9 +491,8 @@ if __name__ == "__main__":
|
||||||
elif state == "mode":
|
elif state == "mode":
|
||||||
state, mode = mode_screen()
|
state, mode = mode_screen()
|
||||||
elif state == "game":
|
elif state == "game":
|
||||||
state, score = game_screen(mode)
|
state, payload = game_screen(mode)
|
||||||
elif state == "gameover":
|
elif state == "gameover":
|
||||||
print(f"Game Over - score: {score}")
|
state, payload = game_over_screen(mode, payload)
|
||||||
state = "menu"
|
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue