Hi I was wondering if anyone can help me. I am trying to create a game in visual python and have come to a complete stop. I need to be able to run many while loops at the same time, i have looked into threading but can't get my head around it. Any help would be appreciated, thank you.

I'm guessing that what you want is an event loop:

while True:
    check for events
    respond to events

and then all of your many while loops could be separate events. What do you have in mind?

Jeff

I am trying to create a 3d blasterball like game. So far I have a box with a ball and bat inside. The ball bounces around in the box and you click and drag the bat were you want it inside the box. I cant get them running at the same time.

I've never used VPython, but the documentation looks accessible. Can you post your code?

Jeff

Here is the code I have so far:

from visual import*
import time

side= 1
thk= 0.02
dt= 0.005

'------------------------------Box------------------------------'
'------------------------Back-Of-Box---------------------------)'
L=box(pos=((0.5*side)-.25,side-0.125,-0.25),length=1,height=thk,width=thk)          
R=box(pos=((0.5*side)-.25,0.125,-0.25),length=1,height=thk,width=thk)             
T=box(pos=((0)-.25,0.5*side,-0.25),length=thk,height=0.75+thk,width=thk)         
F=box(pos=(side-.25,.5*side,-0.25),length=thk,height=0.75+thk,width=thk)

'------------------------Front-Of-Box---------------------------)'
Lz=box(pos=((.5*side)-.25,side-0.125,1.25),length=1,height=thk,width=thk)
Rz=box(pos=((.5*side)-.25,0.125,1.25),length=1,height=thk,width=thk)
Tz=box(pos=((0)-.25,.5*side,1.25),length=thk,height=0.75+thk,width=thk)
Fz=box(pos=(side-.25,.5*side,1.25),length=thk,height=0.75+thk,width=thk)

'------------------------Middle-Of-Box---------------------------)'
Ls=box(pos=(0-.25,side-0.125,.5*side),length=thk,height=thk,width=1.5+thk)
Rs=box(pos=(side-.25,0.125,.5*side),length=thk,height=thk,width=1.5+thk)
Ts=box(pos=(side-.25,side-0.125,.5*side),length=thk,height=thk,width=1.5+thk)
Fs=box(pos=(0-.25,0.125,.5*side),length=thk,height=thk,width=1.5+thk)
'------------------------------Box------------------------------'



'-------------------------Ball-loop-----------------------------'



ball=sphere(pos=(.5,.5,.5),radius= .025)
ball.velocity = vector(0.35,0.25,0.15)

while(1==1):
    rate(1000)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.x+.05>Rs.x:
        ball.velocity.x= -ball.velocity.x
    if ball.x-.05<Ls.x:
        ball.velocity.x= -ball.velocity.x
    if ball.y+.05>Ts.y:
        ball.velocity.y= -ball.velocity.y
    if ball.y-.05<Fs.y:
        ball.velocity.y= -ball.velocity.y
    if ball.z+.05>Tz.z:
        ball.velocity.z= -ball.velocity.z
    if ball.z-.05<F.z:
        ball.velocity.z= -ball.velocity.z
    time.sleep(.01)

    


'--------------------------Drag-loop----------------------------'


nodes = []
for t in arange(1):
    Bat = box(pos=(0,0.6,0),length=0.2,height=0.125,width=0.02)
    Bat.color = Bat.icolor =(0.5,0.5,1)
    
    nodes.append(Bat)



drag = None
while 1:
  if scene.mouse.events:
    c = scene.mouse.getevent()
    if drag and (c.drop or c.click):   # drop the selected object
      dp = c.project(normal=scene.up)
      if dp: drag.pos = dp
      drag.color = drag.icolor
      drag = None
    elif c.pick and hasattr(c.pick,"icolor"):   # pick up the object
      drag = c.pick
      drag.color = color.white
  if drag:
   dp = scene.mouse.project(normal=scene.up)
   if dp: drag.pos = dp

I've used Visual Python once before (for about an hour?), but never got the hang of it well. What I have to ask is if Visual Python has some kind of function that automatically updates objects like Pygame does? If so, could just sub-class the box/sphere objects?

Otherwise, threading would be your best bet. I found this threading tutorial to be very helpful. Very clear and easy to understand! Hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.