2

我正在创建一个文字冒险。我怎么能在其中添加静态文本。我的意思是一些文本总是停留在窗口的左侧。即使所有其他文本都在向下滚动。另外我怎么能让这个文本变红。

4

2 回答 2

4

这是一个以红色显示一些静态文本的示例(始终位于顶部):

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

于 2013-06-15T10:18:15.993 回答
0

您可以通过将静态文本放在单独的窗口中来创建它。为所有文本创建一个足够大的窗口,然后为动态文本创建一个较小的窗口。通过将各种COLOR_*常量作为文本属性传递给文本着色。

于 2013-06-15T09:51:21.453 回答