something

main
Your Name 2024-03-01 15:35:18 +02:00
parent 5fbc498b03
commit 6354532665
2 changed files with 31 additions and 10 deletions

41
main.py
View File

@ -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):
@ -132,8 +129,32 @@ 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))

0
score.scr 100644
View File