# File: Xiang/move2.py # # Red Ball moves on the screen. Changes directions for hitting the border from graphics import * MAXX,MAXY = 500,500 def initGraphics(): win = GraphWin( "Moving Red Ball R=25" , MAXX, MAXY ) return win def main(): win = initGraphics() deltaX, deltaY = 3,3 message = Text( Point(300,15), "Moving Red Ball r=15" ) message.draw( win ) r=15 # radius ball = Circle( Point(100,100), r ) # start from P(100,100) ball.setFill( "red" ) # ball is red ball.draw( win ) for i in range( 500): ball.undraw() if not ( 0 <= ball.getCenter().getX() <= MAXX ): # check x,y in frame deltaX = -deltaX #change direction if not ( 0 <= ball.getCenter().getY() <= MAXY ): deltaY = -deltaY ball.move( deltaX, deltaY ) ball.draw( win ) win.getMouse() win.close() main()