# Caitrin Eaton # CS152 Project 8: Designing classes # 18 April 2018 # # A bucket is a container with a floor and two walls import graphics as gr import physics_objects as po import time import random import sys def main( argv ): if len(argv) < 3: print("Usage: python3 bucket.py ") print("Using default value(s):") if len(argv) < 2: print("\tnum_balls = 3") num_balls = 3 else: num_balls = int(argv[1]) print("\tloss_factor = 0.95") loss_factor = 0.95 else: num_balls = int(argv[1]) loss_factor = float(argv[2]) win = gr.GraphWin( "The ball should fall...", 500, 600 ) # Create a floor object for the balls to collide with floor = po.Floor( win, length=50, thickness=5, x0=0, y0=5 ) floor.draw() # Create wall objects for the balls to collide with wallL = po.Wall( win, height=50, thickness=5, x0=5, y0=0 ) wallL.draw() wallR = po.Wall( win, height=50, thickness=5, x0=45, y0=0 ) wallR.draw() # Create ball objects to drop balls = [] for i in range( num_balls ): m = random.random()*10 x = random.random()*30 + 10 y = random.random()*30 + 20 vx = random.random()*50 - 25 vy = random.random()*5 - 2.5 b = po.Ball( win, m ) b.setPosition( [x, y] ) b.setVelocity( [vx, vy] ) b.draw() balls.append( b ) dt = 0.1 dt_collision = 0.01 t = 0 minV = 1.0 while win.checkMouse() == None: for ball in balls: ball.update( dt ) [x, y] = ball.getPosition() t += dt v = ball.getVelocity() # Check for a floor collision. Bounce only if the y-velocity # of the ball is significant if floor.collision( ball ): ball.setVelocity( [v[0], abs(v[1])*loss_factor] ) v = ball.getVelocity() # Bounce while floor.collision( ball ) and abs(v[1])>minV: ball.update( dt_collision, colliding=True ) v = ball.getVelocity() t += dt_collision # If y-velocity after impact is insignificant, let it rest if abs(v[1])<=minV: ball.setVelocity([v[0],0.0]) ball.setAcceleration([ball.getAcceleration()[0],0.0]) # Check for a wall collision. if wallL.collision( ball ) or wallR.collision( ball ): ball.setVelocity( [-v[0]*loss_factor, v[1]] ) v = ball.getVelocity() # Bounce while the x-velocity of the ball is significant while (wallL.collision( ball ) or wallR.collision( ball )) and abs(v[0])>minV: ball.update( dt_collision, colliding=True ) v = ball.getVelocity() t += dt_collision # If x-velocity after impact is insignificant, let it rest if abs(v[0])<=minV: ball.setVelocity([0.0,v[1]]) ball.setAcceleration([0.0,ball.getAcceleration()[1]]) win.update() time.sleep( dt ) win.close() if __name__=="__main__": main( sys.argv )