WEEK06: indefinite (while) loops, more functions...
---------------------------------------------------
F: more while loops and functions
QUIZ 3 today...
LAB 6 checkMouse() and checkKey():
- here's an example of using the checkMouse() method:
for i in range(1000):
p = win.checkMouse()
if p != None:
print "got a mouse click!!!"
p.draw(win)
sleep(1)
else:
print "no mouse click...i = %d" % (i)
check mouse returns either None if the mouse was not clicked,
or a graphics Point object if it *was* clicked. The above code
checks the mouse and does something appropriate with the result.
- here's an example of using checkKey() to get keyboard input
(without pausing if there were no keys pressed):
i = 0
while True:
i = i + 1
key = win.checkKey() # returns Key string or None
if key != None:
print "got a key click!!!"
print key
if key == "q":
print "quitting..."
break
sleep(1)
else:
print "no key click........%5d" % (i)
win.close()
YOUR TURN:
- use the above checkkey.py program to see what checkKey()
returns when you press different keys
- what is returned when you press the arrow keys?
- write a function to move an object around the window (a Circle
or maybe a Rectangle) based on the arrow keys. If the user presses
the Left Arrow Key, move the object 5 pixels to the left. If they
press the Right Arrow Key, move the object 5 pixels to the right.