By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,196 Members | 1,699 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,196 IT Pros & Developers. It's quick & easy.

Curses question

P: n/a
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()

Mar 9 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.