#Key Balls # #Shows myro graphics objects, as well as getting keystrokes as they happen # by binding a callback function to the window's keyboard event handler. # #example released to the public domain #Fall 2009 # # from myro import * aWindow = GraphWin("Pong", 500,500) xPos = [50,10,25,30] yPos = [50,200,15,17 ] xDelta = [-2,5,8,10] yDelta = [5,3,2,1,7] Balls = [] #this function gets called whenever any key is pressed and our graphWin has #the focus. (Note the call to aWindow.bind_all() below which accomplishes this! def keyFunc(event): global xDelta, yDelta print event.char print "reversing all balls!" for i in range( len(xDelta)): xDelta[i] = -xDelta[i] yDelta[i] = -yDelta[i] index = 0 while index < len(xPos): Ball = Circle( Point(xPos[index], yPos[index]), 10) Ball.draw(aWindow) Balls.append(Ball) index = index + 1 Balls[0].setFill( color_rgb(255,0,0) ) Balls[1].setFill( color_rgb(0,255,0) ) Balls[2].setFill( color_rgb(0,0,255) ) index = 3 while index < len(xPos): Balls[index].setFill( color_rgb(255,0,255) ) index = index + 1 print "Coloring leftover balls!" #Set up a binding to our GraphWindow so that the key function # will be called whenever our window has focus and a key is pressed aWindow.bind_all("", keyFunc) while timeRemaining(20): for index in range( len(xPos) ): if (0 > xPos[index]) or (500 < xPos[index]): xDelta[index] = - xDelta[index] if (0 > yPos[index]) or (500 < yPos[index]): yDelta[index] = - yDelta[index] xPos[index] = xPos[index] + xDelta[index] yPos[index] = yPos[index] + yDelta[index] Balls[index].move(xDelta[index], yDelta[index]) wait(0.01) print "**************************" print "***** Time is up! ********" print "**************************" #End of program.