diff --git a/src/game.py b/src/game.py index 35b06b6..aea0cce 100644 --- a/src/game.py +++ b/src/game.py @@ -10,6 +10,7 @@ MENU = 1 PLAY = 2 LOST = 3 +WAITING = 4 def init_borders(y, x, h, w): @@ -59,7 +60,8 @@ def handle_input(c, game_state, scene, snake, apples): Returns ------- quit, reset, new_game_state, swicth_debug : bool, int, int, bool - a signal telling if the game is over, a reset integer for the score, the new state of the game and a debug switching signal. + a signal telling if the game is over, a reset integer for the score, the new state of the game and a debug + switching signal. """ # quit the game. if c in [27, ord('q')]: @@ -68,7 +70,7 @@ def handle_input(c, game_state, scene, snake, apples): # respawn the objects if 'r' is pressed whilst not playing reset, new_game_state, switch_debug = 0, game_state, False if c == ord('r'): - if game_state == LOST: + if game_state in [LOST, WAITING]: snake.spawn(*scene) for apple in apples: apple.spawn(*scene) diff --git a/src/main.py b/src/main.py index f893513..400352f 100644 --- a/src/main.py +++ b/src/main.py @@ -4,6 +4,8 @@ import sys # allows the user to play from anywhere in the system. +from datetime import datetime + _root = os.path.dirname(os.path.realpath(__file__)) + "/.." sys.path.append(_root) @@ -17,6 +19,8 @@ from src.game import _wall from src.game import PLAY from src.game import MENU +from src.game import LOST +from src.game import WAITING from src.game import init_borders from src.game import handle_input from src.game import menu @@ -70,6 +74,7 @@ def main(stdscr): game_debug = False game_debug_msg = '' game_score = 0 + game_scores = [] # the game loop. while True: @@ -78,6 +83,9 @@ def main(stdscr): # handle the game input. quit_game, reset, game_state, switch_debug = handle_input(c, game_state, scene, snake, apples) if quit_game: + with open("scores.csv", "w") as file: + content = '\n'.join(map(str, game_scores)) + file.write(content) break game_debug ^= switch_debug game_score *= reset @@ -90,6 +98,10 @@ def main(stdscr): game_score = new_score if new_score else game_score game_state = new_game_state if new_game_state else game_state game_debug_msg = new_debug_msg if new_debug_msg else game_debug_msg + elif game_state == LOST: + game_state = WAITING + data = [datetime.now().strftime("%d/%m/%Y %H:%M:%S"), game_score, user_name] + game_scores.append(';'.join(map(str, data))) # blit all the objects on the screen. blit(stdscr, borders, snake, apples, game_score, game_state, game_debug, user_name + game_debug_msg, fps)