""" ChapterAnimation/manyCircleKeyMove.py MM Bubble eater - you have to eat the Bubbles in a certain amount of time """ from graphics import * from math import * import winsound import time winW, winH=300,300 def distance(c1,c2): #distance between 2 circles return sqrt((c1.getCenter().getX()-c2.getCenter().getX())**2+(c1.getCenter().getY()-c2.getCenter().getY())**2) def main(): win = GraphWin( "Move with keys", winW,winH) timeStart = time.time() #to check print(timeStart) radius = 25 # radius of the eater rsmall = 10 #radius of the bubbles myc = Circle( Point(100,100), radius) #the bubble eater myc.setFill( '#ffff00' ) myc.setOutline("blue") myc.setWidth(5) myc.draw( win ) c1 = Circle( Point(50,150), rsmall) #small bubbles c2 = Circle( Point(170,78), rsmall) c3 = Circle( Point(78,200), rsmall) c4 = Circle( Point(200,80), rsmall) clist=[c1,c2,c3,c4] for c in clist: # make the bubbles and draw them c.setFill("red") c.draw(win) while True: for c in clist: # check all bubbles if (distance(myc,c) < radius + rsmall): # check collision circles c.undraw() winsound.Beep(440,200) clist.remove(c) # remove from list otherwise it is still there k = win.getKey() if (k == 'Up'): # up myc.move(0,-3) elif ( k == 'Down'): # down myc.move(0,3) elif (k == 'Right'): # right myc.move(3,0) elif ( k == 'Left'): # left myc.move(-3,0) if ( time.time() > timeStart + 20 ): ## After 20 sec game is finished! print(time.time()) #to check break main()