1 回答
TA贡献1784条经验 获得超9个赞
您有三种可能的解决方案:
在用户输入后重新打印您希望显示的整个屏幕
创建一个 GUI,您可以在其中更好地控制显示的内容
如果在 Unix 上你可以尝试使用
curses
既然您已经尝试过 tkinter,那么 GUI 路由目前对您来说似乎是一条不可行的路线。所以你可能也有类似的诅咒问题。因此,我建议您坚持只根据需要重新打印屏幕。您最终会得到之前棋盘和猜测的历史记录,但这是一种控制屏幕外观的简单方法。
例如。
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!')
你最终会得到类似的东西:
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!
如果你想尝试curses这里是一个让你开始的基本脚本:
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)
添加回答
举报