我正在创建一个文本冒险。我怎么能为此添加静态文本。 我的意思是一些文本总是停留在窗口的左侧。即使所有其他文本都向下滚动。我怎么能把这个文字变成红色。
答案 0 :(得分:2)
以下示例显示了一些红色的静态文本(始终位于顶部):
import sys
import curses
curses.initscr()
if not curses.has_colors():
curses.endwin()
print "no colors"
sys.exit()
else:
curses.start_color()
curses.noecho() # don't echo the keys on the screen
curses.cbreak() # don't wait enter for input
curses.curs_set(0) # don't show cursor.
RED_TEXT = 1
curses.init_pair(RED_TEXT, curses.COLOR_RED, curses.COLOR_BLACK)
window = curses.newwin(20, 20, 0, 0)
window.box()
staticwin = curses.newwin(5, 10, 1, 1)
staticwin.box()
staticwin.addstr(1, 1, "test", curses.color_pair(RED_TEXT))
cur_x = 10
cur_y = 10
while True:
window.addch(cur_y, cur_x, '@')
window.refresh()
staticwin.box()
staticwin.refresh()
inchar = window.getch()
window.addch(cur_y, cur_x, ' ')
# W,A,S,D used to move around the @
if inchar == ord('w'):
cur_y -= 1
elif inchar == ord('a'):
cur_x -= 1
elif inchar == ord('d'):
cur_x += 1
elif inchar == ord('s'):
cur_y += 1
elif inchar == ord('q'):
break
curses.endwin()
结果的屏幕截图:
请记住,顶部的窗口必须为refresh()
,否则应将下面的窗口绘制在它们之上。
如果要更改静态文本,请执行以下操作:
staticwin.clear() #clean the window
staticwin.addstr(1, 1, "insert-text-here", curses.color_pair(RED_TEXT))
staticwin.box() #re-draw the box
staticwin.refresh()
1, 1
表示从第二行的第二个字符开始写入(请记住:坐标从0
开始)。这是必需的,因为窗口的框在第一行和第一列上绘制。
答案 1 :(得分:0)
您可以通过将静态文本放在单独的窗口中来创建静态文本。为所有文本创建一个足够大的窗口,然后为动态文本创建一个较小的窗口。通过将各种COLOR_*
常量作为文本属性传递来着色文本。