Question about using Vpython?

Justin Shaw wyojustin at hotmail.com
Fri Dec 20 07:48:01 EST 2002


> Could you tell me that can I creating complex interactive 3D scenes with
Vpython?
You can.  Just download and read the example programs to see how easy!

Justin

Here is a 3D Pong game I wrote to test it out!

----------------------------
pong.py -----------------------------------------
from visual import *

ball = sphere(pos=(-1, 0, 0), color=color.red, radius = .05)
paddle1 = box(pos=(0, 0, -1.1), size=(.3, .1 ,.01), color=color.green)
paddle2 = box(pos=(0, 0, 1.1), size=(.3, .1, .01), color=color.blue)
side = 1
thk = 0.01
s2 = 2*side - thk
s3 = 2*side + thk
box(pos=(side, 0, 0), length=thk,  height=s2/3,  width=s2, color =
(0.7,0.7,0.7))
box(pos=(-side, 0, 0), length=thk,  height=s2/3,  width=s2, color =
(0.7,0.7,0.7))
side = side + thk
ball.v = vector(1, 0, 1)
dt = .01
step = vector(.03, 0, 0)
i = 0
nBalls = 3

def paddleCheck():
    if scene.kb.keys:
        s = scene.kb.getkey()
        if s == 'right' :
            paddle1.pos = paddle1.pos + step
        elif s == 'left':
            paddle1.pos = paddle1.pos - step
        elif s == 'a':
            paddle2.pos = paddle2.pos + step
        elif s == 'd':
            paddle2.pos = paddle2.pos - step


def play():
    inPlay = 1
    i = 0
    while inPlay:
        i = i + 1
        i = i % 2
        rate(1./dt)
        ball.pos = ball.pos + dt * ball.v
        if 1:
            if ball.z + ball.radius > paddle2.z:
                ball.v.z = -ball.v.z
            elif ball.z - ball.radius < paddle1.z:
                if (paddle1.x - paddle1.size.x / 2 < ball.x and
                    ball.x < paddle1.x + paddle1.size.x / 2):
                    ball.v.z = -ball.v.z
                else:
                    inPlay = 0
                    winner = 2
            #elif not (side + ball.radius > ball.x > -side - ball.radius):
            elif abs(ball.x) > side - ball.radius:
                ball.v.x = -ball.v.x
            paddleCheck()
    return winner

for n in range(nBalls + 100):
    result = play()
    if result == 2:
        print paddle2.color, 'wins'
        ball.pos = paddle1.pos + vector(0, 0, ball.radius)
        ball.v.z = -ball.v.z
    else:
        print paddle1.color, 'wins'
        ball.pos = paddle2.pos - vector(0, 0, ball.radius)
        ball.v.z = -ball.v.z
    while 1:
        paddleCheck()
        ball.pos = paddle1.pos + vector(0, 0, ball.radius)
        k = scene.kb.getkey()
        if len(k) == 1:
            break
print 'Game Over'




"hong hong" <ssppyy88 at yahoo.com> wrote in message
news:mailman.1040368228.22981.python-list at python.org...
Hello!
Could you tell me that can I creating complex interactive 3D scenes with
Vpython?
Thanks!
Honghong




Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now





More information about the Python-list mailing list