Curses question

M

mike.baranski

So, can someone tell me why the following code is wrong?

When you run it, it will place a . as you arrow key around the screen.
Everything works, until you get to the bottom right corner of the
screen, trying to move off of this square causes addstr to throw and
exception.

Every other spot works. Any idea why?

Thanks:

The Code:

#!/usr/bin/python
import curses
import time
import random
import sys

def cleanup():
curses.nocbreak()
curses.echo()
curses.endwin()

return

def init():
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(2)
return

def main():
pad = curses.newwin(0, 0)
pad.keypad(0)
logfile = open('log.txt', 'w')
# The app goes here...
direction = 66

(y, x) = pad.getmaxyx()

pad.move(0,0)

while True:
pad.refresh()
# time.sleep(1)

direction = pad.getch()
# direction = random.randint(65, 68)
(cy, cx) = pad.getyx()
logfile.write("y=%s, x=%s, cy=%s, cx=%s, direction=%s\n" % (y,
x, cy, cx, direction))
if direction == 65 and cy != 0:
pad.addstr(".")
pad.move(cy - 1, cx)
continue
elif direction == 66 and cy != y-1:
pad.addstr(".")
pad.move(cy + 1, cx)
continue
elif direction == 67 and cx != x-1:
pad.addstr(".")
pad.move(cy, cx + 1)
continue
elif direction == 68 and cx != 0:
pad.addstr(".")
pad.move(cy, cx - 1)
continue
elif chr(direction) == "q" or chr(direction) == "Q":
break
else:
continue

init()
try:
main()
except curses.error, e:
cleanup()
print "Exception [%s]" % e
sys.exit()

cleanup()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top