from visual import * class Billiards: from visual import * class Billiards: def __init__(self, attribute1, attribute2): self.attribute1 = attribute1 self.attribute2 = attribute2 # Some other things happening a = Billiards(1,2) a.attribute1 = 3 # if you check the value of the a.attribute1, it would be 3 def ball_con(): """ A ball moving at a constant speed """ box1 = box(pos = (0,0,0), height = 0.1, width = 10, length = 10, color = color.blue) ball = sphere(pos = (0,1,0), radius = 0.5, vel = vector(0, 0.01,0), color = color.green) dt = 0.005 rate(100) while abs(ball.pos - box1.pos) < 5: ball.pos += ball.vel * dt def ball_acc(): """ A ball moving at an accelerating rate """ box1 = box(pos = (0,0,0), height = 0.1, width = 30, length = 30, color = color.blue) ball = sphere(pos = (0,1,0), radius = 0.5, vel = vector(0, 0.01,0), acc = vector(0,0.001,0), color = color.green) dt = 0.0005 rate(100) while abs(ball.pos - box1.pos) < 10: ball.vel += ball.acc * dt ball.pos += ball.vel * dt def main(): s0 = sphere(pos=vector(0,0,5), color=color.white, radius=1) s0.vel = vector(0,0,0) s1 = sphere(pos=vector(6,0,0), color=color.yellow, radius=1) s1.vel = vector(0,0,0) # www.vpython.org/contents/docs_vp5/visual/display.html scene.range = 15 scene.forward = vector(0,-1,-1) # look downward scene.autoscale = False box(length=10,width=10,height=0.25,color=(.7,.7,.7)) # sphere list L = [s0, s1] RATE = 50 dt = 1.0/RATE while True: rate(RATE) # physics here! for s in L: s.pos += dt*s.vel # collision checking and response: ideal, elastic collisions for i in range(len(L)): for j in range(i+1,len(L)): # diff = the vector between the two diff = L[i].pos - L[j].pos dtan = rotate( diff, radians(90), vector(0,1,0) ) # if they're too close... if mag( diff ) < 2: # get the two velocities vi = L[i].vel vj = L[j].vel # undo the last time step L[i].pos -= L[i].vel*dt L[j].pos -= L[j].vel*dt # find the radial and tangent parts vi_rad = proj(vi, diff) vi_tan = proj(vi, dtan) vj_rad = proj(vj, -diff) vj_tan = proj(vj, dtan) # swap the radials and keep the tangents L[i].vel = vj_rad + vi_tan L[j].vel = vi_rad + vj_tan # mouse and keyboard here: if scene.mouse.clicked != 0: # is there a mouse click? print ("mouse click!") event = scene.mouse.getclick() # remove event scene.mouse.events = 0 # then reset mouse events if scene.kb.keys: # is there a keyevent? s = scene.kb.getkey() # get keypress #print ("keypress is", s) if s == " ": # if you press a space... print ("starting spheres") s0.vel = vector(0,0,-3) s1.vel = vector(-3,0,0) if __name__ == "__main__": main()
When I try to run the code as it is, I get a grey square with a yellow and white ball outside the square. What I need now is to figure out how to actually build the pool table and get the balls to be placed on the table. Then I'd like to be able to hit the numbered balls with the cue ball. Any help with figuring out how to accomplish that with using the code I've got so far is greatly appreciated.