Curses question
mike.baranski at gmail.com
mike.baranski at gmail.com
Thu Mar 9 13:15:59 EST 2006
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()
More information about the Python-list
mailing list