Question

I am creating a text adventure. How could I to add static text to this. What i mean is some text that always stays on the left side of the window. Even if all the other text is scrolling down. Also how could i make this text red.

Was it helpful?

Solution

Here is an example that shows some static text in red(that always stays on top):

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()

A screenshot of the result:

enter image description here

Remember that windows on top must be refresh()ed last otherwise the windows that should go below are drawn over them.

If you want to change the static text do:

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()

Where the 1, 1 means to start writing from the second character of the second line(remember: coordinates start at 0). This is needed since the window's box is drawn on the first line and first column.

OTHER TIPS

You create static text by putting it in a separate window. Create a window large enough for all the text, and then create a smaller window for the dynamic text. Text is colored by passing the various COLOR_* constants as the text attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top