Skip to content

Commit

Permalink
User prompting (username).
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Jul 10, 2021
1 parent 9f149ff commit 4cfc216
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import curses
import os
import time

from src.Snake import Snake
from src.utils import prompt_user

_wall = 10

Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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__":
Expand Down
38 changes: 38 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()

0 comments on commit 4cfc216

Please sign in to comment.