From 4cfc2162046d24702f4092e6e83e46dedd6a5485 Mon Sep 17 00:00:00 2001 From: Antoine Stevan Date: Sat, 10 Jul 2021 12:36:33 +0200 Subject: [PATCH] User prompting (username). --- src/game.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/main.py | 4 +++- src/utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/game.py b/src/game.py index a67af4a..35b06b6 100644 --- a/src/game.py +++ b/src/game.py @@ -1,7 +1,9 @@ import curses +import os import time from src.Snake import Snake +from src.utils import prompt_user _wall = 10 @@ -194,3 +196,45 @@ def blit(stdscr, borders, snake, apples, score, game_state, debug, debug_msg, fp # refresh and wait. stdscr.refresh() time.sleep(1 / fps) + + +def get_user_name(stdscr): + """ + Gets the username, either by reading the user file or by prompting the user. + + Args + ---- + stdscr : _curses.window + the screen object. + + Returns + ------- + user_name : str + the username to user during next game. + """ + # some basic variables. + user_file = ".user" + user_name = '' + prompt = not os.path.exists(user_file) + msg = "Enter username: (hit Ctrl-G or enter to confirm)" + + # the suer file exists. + if not prompt: + # read the file. + with open(user_file, 'r') as file: + user_name = file.readlines() + if len(user_name) != 1: # if content has strange format, prompt the user. + prompt = True + msg = "user file has been corrupted... " + msg + else: + user_name = user_name[0].strip() + + # the user needs to be prompted (unknown user or corrupted user file.) + if prompt: + while user_name == '': # do not take empty strings into account. + user_name = prompt_user(stdscr, msg=msg).strip() + # save the username in the user file. + with open(user_file, 'w') as file: + file.write(user_name) + + return user_name diff --git a/src/main.py b/src/main.py index 8ed4f88..f893513 100644 --- a/src/main.py +++ b/src/main.py @@ -22,6 +22,7 @@ from src.game import menu from src.game import play from src.game import blit +from src.game import get_user_name @error_handler_wrapper @@ -33,6 +34,7 @@ def main(stdscr): nb_apples = 2 init_length = 2 swiftness = 1 + user_name = get_user_name(stdscr) # check if everything can work properly. if (curses.LINES < sh + 2) or (curses.COLS < sw + 2): @@ -90,7 +92,7 @@ def main(stdscr): game_debug_msg = new_debug_msg if new_debug_msg else game_debug_msg # blit all the objects on the screen. - blit(stdscr, borders, snake, apples, game_score, game_state, game_debug, game_debug_msg, fps) + blit(stdscr, borders, snake, apples, game_score, game_state, game_debug, user_name + game_debug_msg, fps) if __name__ == "__main__": diff --git a/src/utils.py b/src/utils.py index 403b651..4597af3 100644 --- a/src/utils.py +++ b/src/utils.py @@ -3,6 +3,9 @@ import numpy as np +from curses.textpad import rectangle +from curses.textpad import Textbox + # directories initializations. _root = os.path.dirname(os.path.realpath(__file__)) + "/.." _log = os.path.join(_root, ".log") @@ -70,6 +73,41 @@ def init_color_palette(r_bits=3, g_bits=3, b_bits=2): curses.init_color(color, reds[r], greens[g], blues[b]) curses.init_pair(color, curses.COLOR_BLACK, color) + # def paint(stdscr, y, x, _r, _g, _b, bits=3): # color = (_r << (2 * bits)) + (_g << bits) + _b # stdscr.addstr(y, x, ' ', curses.color_pair(8 + color)) + + +def prompt_user(stdscr, msg='', box=(1, 15, 1, 0)): + """ + Prompts the user by creating an editable box and returning the resulting input string of characters. + + Args + ---- + stdscr : _curses.window + the screen object. + msg : str, optional + a message explaining the prompt. + box : (int, int, int, int), optional + the rectangle representing the geometry of the prompt box. Namely, (h, w, y, x) with y and x starting at 0. + + Returns + ------- + input : str + a string given by the user as an input. + """ + # print the message above the box. + stdscr.addstr(box[2] - 1, 0, msg) + + h, w, y, x = box + editwin = curses.newwin(h, w, y + 1, x + 1) # curses.newwin starts positions at 1. + rectangle(stdscr, y, x, box[2] + box[0] + 1, box[3] + box[1] + 1) + stdscr.refresh() + + # create the box and edit it. + box = Textbox(editwin) + box.edit() + + # return the content of the box as the result. + return box.gather()