Allow score saving

This commit is contained in:
Gabriel Augendre 2018-04-30 21:43:49 +02:00
parent a8f02ce099
commit 04b341b046
No known key found for this signature in database
GPG key ID: F360212F958357D4
2 changed files with 23 additions and 7 deletions

View file

@ -10,4 +10,6 @@ RESOLUTION = MAP_RESOLUTION[0], MAP_RESOLUTION[1] + 50
INITIAL_SNAKE_SIZE = 3 INITIAL_SNAKE_SIZE = 3
MAX_HIGH_SCORES_COUNT = 10
SCORES_FILE = 'scores.snake' SCORES_FILE = 'scores.snake'

28
main.py
View file

@ -1,13 +1,14 @@
import logging import logging
import os import os
import pickle import pickle
from tkinter import Tk, simpledialog, messagebox
import pygame import pygame
from pygame import locals as pglocals from pygame import locals as pglocals
from config import (RESOLUTION, MAP_RESOLUTION, from config import (RESOLUTION, MAP_RESOLUTION,
SNAKE_COLOR, BACKGROUND_COLOR, FONT_COLOR, SNAKE_COLOR, BACKGROUND_COLOR, FONT_COLOR,
INITIAL_SNAKE_SIZE, SCORES_FILE) INITIAL_SNAKE_SIZE, SCORES_FILE, MAX_HIGH_SCORES_COUNT)
from objects import Snake, Apple from objects import Snake, Apple
from utils import get_score_text, Direction from utils import get_score_text, Direction
@ -33,7 +34,7 @@ def main():
with open(SCORES_FILE, 'rb') as scores_file: with open(SCORES_FILE, 'rb') as scores_file:
scores = pickle.load(scores_file) scores = pickle.load(scores_file)
logger.info(f'High scores : {scores}') logger.info(f'Previous high scores : {scores}')
while True: while True:
screen.fill(BACKGROUND_COLOR) screen.fill(BACKGROUND_COLOR)
@ -76,11 +77,6 @@ def main():
dirty_rects = snake.move(screen, apple) dirty_rects = snake.move(screen, apple)
if snake.dead: if snake.dead:
logger.info(f'Vous avez perdu ! Score : {score}') logger.info(f'Vous avez perdu ! Score : {score}')
scores.append({'player': 'PLAYER 1', 'score': score})
scores = sorted(scores, key=lambda x: x['score'], reverse=True)[:20]
logger.info(f'High scores : {scores}')
with open(SCORES_FILE, 'wb') as scores_file:
pickle.dump(scores, scores_file)
break break
if snake.head.colliderect(apple.rect): if snake.head.colliderect(apple.rect):
@ -112,6 +108,21 @@ def main():
screen.blit(text, text_rect) screen.blit(text, text_rect)
pygame.display.flip() pygame.display.flip()
Tk().wm_withdraw() # to hide the main window
username = simpledialog.askstring(
"Enregistrer le score",
f"Entrez votre nom d'utilisateur ou laissez vide pour ne pas enregistrer."
)
if username:
scores.append({'player': username, 'score': score})
logger.info(f'Saving {username}: {score}')
scores = sorted(scores, key=lambda x: x['score'], reverse=True)[:MAX_HIGH_SCORES_COUNT]
logger.info(f'New high scores : {scores}')
with open(SCORES_FILE, 'wb') as scores_file:
pickle.dump(scores, scores_file)
else:
logger.debug('Not saving score')
restart = False restart = False
while not restart: while not restart:
for event in pygame.event.get(): for event in pygame.event.get():
@ -123,6 +134,9 @@ def main():
if event.key == pglocals.K_r: if event.key == pglocals.K_r:
logger.info('Restarting game') logger.info('Restarting game')
restart = True restart = True
elif event.key == pglocals.K_s:
scores_str = '\n'.join(map(lambda s: f"{s['player']} : {s['score']}", scores))
messagebox.showinfo("Tableau des scores", scores_str)
clock.tick(5) clock.tick(5)