From 6354532665a4babae64f8c20ea38968990cc739d Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 Mar 2024 15:35:18 +0200 Subject: [PATCH] something --- main.py | 41 +++++++++++++++++++++++++++++++---------- score.scr | 0 2 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 score.scr diff --git a/main.py b/main.py index ae2e685..7d55691 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import random import math from bisect import insort import button - +import os.path pygame.init() screen = pygame.display.set_mode((1000, 700)) @@ -27,6 +27,8 @@ over_font = pygame.font.Font("KodeMono-Medium.ttf", 96) TEXT_COL = (255, 255, 255) +SCORE_FILE_NAME = "score.scr" + new_game_img = pygame.image.load("new_game.png") difficulty_img = pygame.image.load("difficulty.png") @@ -82,11 +84,6 @@ def reset_enemies(difficulty): }) return enemies -# add new score to best_score -def add_new_score(score, best_score): - - return best_score[-5:] - # draw some text with given parameters def draw_text(text, font, text_col, x, y): @@ -130,10 +127,34 @@ def player(x, y): def enemy(x, y): screen.blit(enemy_image, (x, y)) + + + +def update_score_table(scores): + if os.path.isfile(SCORE_FILE_NAME): + with open(SCORE_FILE_NAME, "w") as f: + f.write(" ".join(list(map(str, scores)))) + else: + with open(SCORE_FILE_NAME, "x") as f: + f.write(" ".join(list(map(str, scores)))) + +def read_score_table(): + if not os.path.isfile(SCORE_FILE_NAME): + update_score_table([0, 0, 0, 0]) + with open(SCORE_FILE_NAME) as f: + return [int(score) for score in f.read().split()] +def add_score(score): + current = read_score_table() + current.append(score) + current.sort() + updated = current[1:5] + print(score, updated) + update_score_table(updated) + + #scoreboard for each mode individually -best_score = [0, 0, 0, 0] score_val = 0 textX = 10 textY = 10 @@ -173,7 +194,7 @@ while running: scene = "best_score" case "best_score": screen.blit(hangar, (0, 0)) - print_best_score(best_score) + print_best_score(read_score_table()) # if pressed, go back to menu if back_button.draw(screen): scene = "menu" @@ -209,7 +230,7 @@ while running: for j in range(num_of_enemies): enemies[j]["Y"] = 2000 game_over_text() - best_score = add_new_score(score_val, best_score) + add_score(score_val) score_val = 0 #if game is lost, print some text like wanna start again? press R to continue game_lost = True @@ -263,7 +284,7 @@ while running: temp = "hard" case 3: temp = "insane" - print_best_score(best_score) + print_best_score(read_score_table()) draw_text("Press TAB to resume", smaller_font, TEXT_COL, 350, 550) case _: screen.blit(space, (0, 0)) diff --git a/score.scr b/score.scr new file mode 100644 index 0000000..e69de29