mirror of
https://github.com/kristoferssolo/School.git
synced 2025-10-21 20:10:38 +00:00
added scoreboard
This commit is contained in:
parent
3bb445cf47
commit
d862e22189
@ -29,7 +29,6 @@ def main_menu() -> None:
|
||||
run = True
|
||||
pygame.display.set_caption("Snake - Menu")
|
||||
while True:
|
||||
print(run)
|
||||
WINDOW.fill(BLACK)
|
||||
mouse_pos = pygame.mouse.get_pos()
|
||||
menu_text = set_font(100).render("SNAKE GAME", 1, WHITE)
|
||||
@ -38,22 +37,23 @@ def main_menu() -> None:
|
||||
|
||||
play_button = Button((MID_WIDTH, MID_HEIGHT - 50), "PLAY", 75, GRAY, WHITE)
|
||||
options_button = Button((MID_WIDTH, MID_HEIGHT + 50), "OPTIONS", 75, GRAY, WHITE)
|
||||
quit_button = Button((MID_WIDTH, MID_HEIGHT + 150), "QUIT", 75, GRAY, WHITE)
|
||||
buttons = [play_button, options_button, quit_button]
|
||||
score_button = Button((MID_WIDTH, MID_HEIGHT + 150), "SCORE", 75, GRAY, WHITE)
|
||||
quit_button = Button((MID_WIDTH, MID_HEIGHT + 250), "QUIT", 75, GRAY, WHITE)
|
||||
buttons = [play_button, options_button, score_button, quit_button]
|
||||
|
||||
for button in buttons:
|
||||
button.change_color(mouse_pos)
|
||||
button.update()
|
||||
on_hover(buttons)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
quit()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if play_button.check_input(mouse_pos):
|
||||
from snake import main
|
||||
main()
|
||||
if options_button.check_input(mouse_pos):
|
||||
options()
|
||||
if score_button.check_input(mouse_pos):
|
||||
scoreboard()
|
||||
if quit_button.check_input(mouse_pos):
|
||||
quit()
|
||||
|
||||
@ -80,25 +80,21 @@ def options() -> None:
|
||||
# walls
|
||||
if walls: walls_state = "on"
|
||||
else: walls_state = "off"
|
||||
# speed
|
||||
if FPS == 5: speed_state = "Slow"
|
||||
elif FPS == 10: speed_state = "Normal"
|
||||
elif FPS == 15: speed_state = "Fast"
|
||||
|
||||
speed_button = Button((MID_WIDTH, MID_HEIGHT - 100), f"SPEED - {speed_state}", 75, GRAY, WHITE)
|
||||
speed_state = {5: "Slow", 10: "Normal", 15: "Fast"}
|
||||
|
||||
speed_button = Button((MID_WIDTH, MID_HEIGHT - 100), f"SPEED - {speed_state[FPS]}", 75, GRAY, WHITE)
|
||||
multiplayer_button = Button((MID_WIDTH, MID_HEIGHT), f"MULTIPLAYER - {multiplayer_state}", 75, GRAY, WHITE)
|
||||
walls_button = Button((MID_WIDTH, MID_HEIGHT + 100), f"WALLS - {walls_state}", 75, GRAY, WHITE)
|
||||
back_button = Button((MID_WIDTH, MID_HEIGHT + 200), "BACK", 75, GRAY, WHITE)
|
||||
buttons = [speed_button, multiplayer_button, walls_button, back_button]
|
||||
|
||||
for button in buttons:
|
||||
button.change_color(mouse_pos)
|
||||
button.update()
|
||||
on_hover(buttons)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
quit()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if speed_button.check_input(mouse_pos):
|
||||
if FPS == 5: FPS = 10
|
||||
elif FPS == 10: FPS = 15
|
||||
@ -111,3 +107,36 @@ def options() -> None:
|
||||
main_menu()
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
def scoreboard() -> None:
|
||||
while True:
|
||||
mouse_pos = pygame.mouse.get_pos()
|
||||
WINDOW.fill(BLACK)
|
||||
top_text = set_font(100).render("TOP 10", 1, WHITE)
|
||||
top_rect = top_text.get_rect(center=(MID_WIDTH, 55))
|
||||
WINDOW.blit(top_text, top_rect)
|
||||
back_button = Button((MID_WIDTH, MID_HEIGHT + 250), "BACK", 75, GRAY, WHITE)
|
||||
on_hover([back_button])
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
quit()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if back_button.check_input(mouse_pos):
|
||||
main_menu()
|
||||
|
||||
csv_file = read_score(BASE_PATH)
|
||||
for i, line in enumerate(sort(csv_file, reverse=True)[:11]):
|
||||
for j, text in enumerate(line):
|
||||
score_text = set_font(30).render(text, 1, WHITE)
|
||||
score_rect = score_text.get_rect(center=(MID_WIDTH - 150 + 300 * j, 150 + 30 * i))
|
||||
WINDOW.blit(score_text, score_rect)
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
def on_hover(buttons: list) -> None:
|
||||
for button in buttons:
|
||||
button.change_color(pygame.mouse.get_pos())
|
||||
button.update()
|
||||
@ -1,5 +1,4 @@
|
||||
import csv
|
||||
import operator
|
||||
from os.path import join, exists
|
||||
|
||||
fields = ["Name", "Score"]
|
||||
@ -20,12 +19,19 @@ def write_score(name: str, score: int, base_path: str) -> None:
|
||||
def read_score(path: str):
|
||||
lines = []
|
||||
path = join(path, "score.csv")
|
||||
with open(path, 'r', encoding='UTF-8') as file:
|
||||
for line in csv.reader(file):
|
||||
lines.append(line)
|
||||
return lines
|
||||
try:
|
||||
with open(path, 'r', encoding='UTF-8') as file:
|
||||
for line in csv.reader(file):
|
||||
lines.append(line)
|
||||
return lines
|
||||
except FileNotFoundError:
|
||||
return [fields]
|
||||
|
||||
|
||||
def sort(data, reverse: bool = False):
|
||||
data = sorted(data, key=operator.itemgetter(1), reverse=reverse)
|
||||
def sort(data, reverse: bool):
|
||||
if reverse == None: reverse = False
|
||||
header = data[0]
|
||||
data.remove(header) # remove header
|
||||
data = sorted(data, key=lambda x: int(x[1]), reverse=reverse) # sort data
|
||||
data.insert(0, header) # add header
|
||||
return data
|
||||
@ -1,8 +1,7 @@
|
||||
# Author - Kristiāns Francis Cagulis
|
||||
# Date - 16.04.2022
|
||||
# Date - 18.04.2022
|
||||
# Title - Snake
|
||||
|
||||
from glob import glob
|
||||
import pygame
|
||||
from random import randint
|
||||
from os.path import abspath, dirname
|
||||
@ -79,7 +78,7 @@ def main() -> None:
|
||||
|
||||
while run:
|
||||
clock.tick(FPS)
|
||||
pygame.time.delay(10)
|
||||
pygame.time.delay(0)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user