Question

I wrote a curses code:

import curses
import time
message = raw_input('Enter a word or phrase: ')
q, vertical, horizontal = -1, 1, 1
y, x = 0, 0
screen = curses.initscr()
screen.nodelay(1)
curses.noecho()
dims = screen.getmaxyx()
while q < 0:
    q = screen.getch()
    screen.clear()
    screen.addstr(y, x, message)
    y += vertical
    x += horizontal
    if y == dims[0]-1:
        vertical = -1
    elif y == 0:
        vertical = 1
    if x == dims[1]-len(message)-1:
        horizontal = 1
    elif x == 0:
        horizontal = 1
    time.sleep(0.06)
curses.endwin()

When I run it it works fine until it reaches the right side of the screen, when it glitches. I'm fairly new to curses, so I might of missed something. What is the codes problem?

Was it helpful?

Solution

You're going to hate yourself for this :), you just forgot the negative when switching x directions. It's still trying to move right and attempting to draw offscreen, thus the error.

...
if x == dims[1] - len(message) - 1:
    horizontal = -1
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top