#bouncing_ball.py import random import time from ezgraphics import GraphicsWindow width = int(input("Enter width of graphics window: ")) height = int(input("Enter height of graphics window: ")) ball_size = int(input("Enter size of the ball: ")) r = ball_size/2 move_size = int(input("Enter move size: ")) num_moves = int(input("Enter number of moves: ")) pause = float(input("Enter pause time: ")) print("Go click in the graphics window to start the ball rolling") win = GraphicsWindow(width,height) win.setTitle("Bouncing ball") win.showStatus() #win.setStatus("#moves: 0") win.setStatus("click mouse to start") canvas = win.canvas() #x = random.randint(1,width) #random starting center point #y = random.randint(1,height) dir = random.choice(['ne','se','sw','nw']) moveDB = {'ne':(1,-1, 1,'nw', 0,'se'), #xd,yd, 1=max, 0=min 'se':(1, 1, 1,'sw', 1,'ne'), 'sw':(-1,1, 0,'se', 1,'nw'), 'nw':(-1,-1,0,'ne', 0,'sw')} x,y = win.getMouse() #wait until a mouseclick. starting center point for i in range(num_moves): canvas.setFill("white") #blank-out the circle canvas.setOutline("white") canvas.drawOval(x-r,y-r, ball_size,ball_size) ''' if dir == 'ne': x += move_size y -= move_size if x+r >= width: dir = 'nw' elif y-r <= 0: dir = 'se' elif dir == 'se': x += move_size y += move_size if x+r >= width: dir = 'sw' elif y+r >= height: dir = 'ne' elif dir == 'sw': x -= move_size y += move_size if x-ball_size/2 <= 0: dir = 'se' elif y+ball_size/2 >= height: dir = 'nw' elif dir == 'nw': x -= move_size y -= move_size if x-r <= 0: dir = 'ne' elif y-r <= 0: dir = 'sw' ''' x += moveDB[dir][0] * move_size y += moveDB[dir][1] * move_size if (dir=='ne' or dir=='se') and x+r>width or\ (dir=='nw' or dir=='sw') and x-r<0: dir = moveDB[dir][3] elif (dir=='sw' or dir=='se') and y+r>height or\ (dir=='nw' or dir=='ne') and y-r<0: dir = moveDB[dir][5] canvas.setFill("red") #turn it on in its new location canvas.setOutline("red") canvas.drawOval(x-ball_size/2,y-ball_size/2, ball_size,ball_size) time.sleep(pause) #let the red circle burn onto our retinas # becomes Not Responding?????????? after a few iterations win.wait()