Reputation: 65
I'm sorry if the title is a little bit vague; I am not sure how to put my concern into words. I am a beginner in Python and I am making a Boggle/Word Search game.
I would like the board to stay in the same place/not get pushed back by the user inputs. Is that possible?
I tried opening a .txt
file in Python via Notepad but the code stops once the txt file is open; any help with this will do.
I wonder if it is also possible to make python open a new window/console to display the board while the other console goes on and executes the original code.
Finally, is there a way kind of 'refresh' the user inputs here?
I would like the line to be empty once the user submits their answer. I've tried making a GUI with tkinter but it was too difficult for me due to the number of pages that I made and that I really have no idea how to use it.
Thank you!
Upvotes: 0
Views: 82
Reputation: 40823
You have three possible solutions:
curses
Since, you've already tried tkinter, then the GUI route seems a no-go route for you currently. So you may have similar problems with curses as well. As such, I'd recommend sticking to just reprinting your screen as you want it. You'll end up with a history of previous boards and guesses, but it's an easy way to control what the screen looks like.
eg.
def mainloop(board, words, time_limit):
correct_guesses = []
print_board(board)
while not solved(words, correct_guesses) and now() < time_limit:
word = get_user_guess()
if word in words and word not in correct_guesses:
correct_guesses.append(word)
print('correct!\n')
else:
print('wrong!\n')
if solved(words, correct_guesses):
print('complete!')
else:
print('times up!')
You'll end up with something like:
E B C D
C F G H
I J K L
M N O P
your guess: mouse
wrong!
E B C D
C F G H
I J K L
M N O P
your guess: mice
correct!
complete!
If you want to try curses
here is a basic script to get you started:
import curses
def main(stdscr):
board = [
['E', 'B', 'C', 'D'],
['C', 'F', 'G', 'H'],
['I', 'J', 'K', 'L'],
['M', 'N', 'O', 'P'],
]
curses.echo() # we want to see user input on screen
user_guess_prompt = 'your guess: '
guess_line = len(board) + 1 + 2
guess_column = len(user_guess_prompt)
max_guess_size = 15
guess = None
while guess != 'q':
# clear the entire screen
stdscr.clear()
# a bit of help on how to quit (unlike print, we have to add our own new lines)
stdscr.addstr('enter q as a guess to exit\n\n')
# print the board
for line in board:
stdscr.addstr(' '.join(line)+'\n')
# tell the use about their previous guess
if guess == 'mice':
stdscr.addstr(guess + ' is correct!\n')
elif guess is None:
stdscr.addstr('\n')
else:
stdscr.addstr(guess + ' is wrong!\n')
# prompt for the user's guess
stdscr.addstr(user_guess_prompt)
# tell curses to redraw the screen
stdscr.refresh()
# get user input with the cursor initially placed at the given line and column
guess = stdscr.getstr(guess_line, guess_column, max_guess_size).decode('ascii')
if __name__ == '__main__':
curses.wrapper(main)
Upvotes: 1
Reputation:
One approach you may use with python built-in means is to refresh all the data on the screen (clear and print again) when it is necessary. Try the sample code (should work in linux terminal and windows console):
import os
import sys
if sys.platform.startswith("win"):
def clear_screen():
os.system('cls')
else:
def clear_screen():
os.system('clear')
def print_board(board):
for row in board:
print(*row)
data = [[1, 1], [1, 1]]
clear_screen()
print_board(data)
diff = input('\nPlease enter difficulty (easy, medium, hard): ')
clear_screen()
print_board(data)
limit = input('\nEnter time limit in minutes: ')
words = list()
counter = 0
while True:
clear_screen()
print_board(data)
print('\nTime limit is {} minutes.'.format(limit))
print('\nInput the words you see.')
words.append(input('>>> '))
counter += 1
if counter == 3:
break
clear_screen()
print_board(data)
print('\nYou have entered the words:')
print(*words, sep=', ')
print('\nEND')
Upvotes: 1