# 3D Pong a game for 2 players by Tony Forster January 2005 #requires Vpython http://www.vpython.org/ for visual import - 3D functions #winsound gives beeps from visual import * #import winsound scene.autoscale = 0 scene.title="Tony's 3Dpong" ball=sphere(pos=(-5,0,0), radius=0.5, color=color.red) wallR = box(pos=(6,0,0),size=(0.2,12,12),color=color.yellow) wallL = box(pos=(-6,0,0), size=(0.2,12,12), color=color.yellow) wallT = box(pos=(0,6,0),size=(12,0.2,12),color=color.yellow) wallB = box(pos=(0,-6,0),size=(12,0.2,12),color=color.yellow) batR = box(pos=(5,0,0),size=(0.2,3,3),color=color.blue) batL = box(pos=(-5,0,0),size=(0.2,3,3),color=color.blue) score1=score2=0 messagestart=label(pos=(0,5,6), text="keys: up down left right w s a d", opacity=0, box=0, line=0) scene.kb.getkey() messagestart.visible=0 message=label(pos=(0,5,6), text=str(score1)+" "+str(score2), opacity=0, box=0, line=0) dt = 0.05 ball.velocity = vector(0.2,0.1,0.05) while (1==1): rate(500) ball.pos = ball.pos + ball.velocity*dt if ball.x > wallR.x: ball.velocity.x = -ball.velocity.x #winsound.MessageBeep() score1=score1+1 message.text=str(score1)+" "+str(score2) if ball.x < wallL.x: ball.velocity.x = -ball.velocity.x #winsound.MessageBeep() score2=score2+1 message.text=str(score1)+" "+str(score2) if ball.y > wallT.y: ball.velocity.y = -ball.velocity.y if ball.y < wallB.y: ball.velocity.y = -ball.velocity.y if ball.z > 6: ball.velocity.z = -ball.velocity.z if ball.z < -6: ball.velocity.z = -ball.velocity.z if (ball.x> batR.x)& (ball.y-batR.y>-1.5)&(ball.y-batR.y<1.5)& (ball.z-batR.z>-1.5)& (ball.z-batR.z<1.5): ball.velocity.x = -ball.velocity.x if (ball.x< batL.x)& (ball.y-batL.y>-1.5)&(ball.y-batL.y<1.5)& (ball.z-batR.z>-1.5)& (ball.z-batR.z<1.5): ball.velocity.x = -ball.velocity.x if scene.kb.keys: # is there an event waiting to be processed? s = scene.kb.getkey() # obtain keyboard information if s=="up": batR.y= batR.y+1 if s=="down": batR.y= batR.y-1 if s=="left": batR.z= batR.z-1 if s=="right": batR.z= batR.z+1 if s=="w": batL.y= batL.y+1 if s=="s": batL.y= batL.y-1 if s=="a": batL.z= batL.z+1 if s=="d": batL.z= batL.z-1