""" ChapterAnimation/bubbleMouse.py bounce: move a circle left - right win.checkMouse() is false (none) and is not waiting for the mouse to click """ from graphics import * winW,winH = 400,300 # window Width x Height from math import * #for sqrt def distance(P1,P2): return sqrt((P1.getX()-P2.getX()) ** 2 + (P1.getY()-P2.getY()) ** 2) def main(): win = GraphWin( "Bubble", winW, winH ) radius = 30 myc = Circle( Point(10,70), radius) myc.setFill( '#ffff00' ) myc.draw( win ) speed = 3 while True: myc.move(speed, 0 ) time.sleep(0.01) pc = myc.getCenter() if ( pc.getX()+radius > winW or pc.getX() < 0 ): speed = speed * -1 pm = win.checkMouse() if pm and (distance(pm,pc) < radius) : myc.setFill("red") main()