我正在创建一个文本冒险。我怎样才能添加静态文本呢? 我的意思是一些文本始终保留在窗口的左侧。即使所有其他文本都向下滚动。另外我怎样才能把这个文本变成红色。
最佳答案
这是一个示例,显示一些红色的静态文本(始终位于顶部):
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
)。这是必需的,因为窗口的框是在第一行和第一列上绘制的。
关于python - (python) 如何在curses中创建静态文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122268/