有没有办法启动诅咒仅占据屏幕的一部分,同时允许在屏幕上方进行常规打印?
因此,在此提供的示例中,所有正常的打印语句将进入常规终端,并在程序完成后继续存在。
import curses
from curses import wrapper
from time import sleep
import random
def main(stdscr):
stdscr.nodelay(True)
stdscr.clear()
participants = 5
positions = dict()
for i in range(participants):
positions[i] = 0
completed = 0
game_winner = -1
while True:
_c = stdscr.getch()
round_winner = random.randint(0, participants-1)
winner_position = positions[round_winner]
if winner_position < 10:
positions[round_winner] = winner_position + 1
stdscr.clear()
for i in range(participants):
position = positions[i]
stdscr.addstr(i, 0, str(i+1) + ": ")
stdscr.addstr(i, position+3, 'x')
if position == 10:
if game_winner < 0:
game_winner = i + 1
print(str(i + 1) + " completed the race!")
positions[round_winner] = position + 1
completed += 1
sleep(1)
if completed >= len(positions):
break
print("The winner is " + str(game_winner) + "!")
wrapper(main)