Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm trying to time how long a key is held down using vPython
    text
    copied!<p>I'm writing a program for my physics final. Throughout the semester we have used vPython to model situations and get exact answers, etc. Our final project is to create a game using vPython that includes some type of physics.</p> <p>I chose to remake Bowman except with tanks. So you have a tank on the right hand of the screen and left hand of the screen with a wall in the middle. The object is to aim your cannon and shoot the right velocity to hit your opponent's tank. I have the a good chunk of the program complete however I am stuck on a few different things.</p> <p>First, how can I time a keystroke? I have it so I shoot from the each cannon however I want to be able to hold down a key and dependent on how long it's held down for the faster the initial velocity will be.</p> <p>Second, where would I incorporate gravity into the program? I have a general idea of how to but I just don't know which function to put it into.</p> <p>Lastly, I have a wall height being generated randomly each time the program is run. However sometimes the wall is so small you can't see it. Is there a way I can set a range of values for this?</p> <p>Here is my code:</p> <pre><code>from visual import* from random import* scene.autoscale=False scene.width = 1500 scene.height = 800 scene.title='Tanks' def moveaup(gun): theta=arctan(gun.axis.y/gun.axis.x) dtheta=.1 if (theta&lt;pi/2): theta=theta+dtheta if not (theta&gt;pi/2): gun.axis=(cos(theta),sin(theta),0) else: gun.axis=vector(0,1,0) def moveadown(gun): theta=arctan(gun.axis.y/gun.axis.x) dtheta=.1 if (theta&gt;0): theta=theta-dtheta gun.axis=(cos(theta),sin(theta),0) def movebup(gun): theta=arctan(gun.axis.y/gun.axis.x)+pi dtheta=.1 if (theta&gt;pi/2): theta=theta-dtheta if not (theta&lt;pi/2): gun.axis=(cos(theta),sin(theta),0) else: gun.axis=vector(0,1,0) def movebdown(gun): theta=arctan(gun.axis.y/gun.axis.x)+pi dtheta=.1 if (theta&lt;pi): theta=theta+dtheta gun.axis=(cos(theta),sin(theta),0) def shoota(gun): vel = vector(1,1,0) bullet = sphere(pos=(gun.pos.x+gun.axis.x,gun.pos.y+gun.axis.y,0),radius=(.0785),color=color.yellow) bullet.v = vector(0,0,0) bullet.v = bullet.v+vel bulletlist.append(bullet) def shootb(gun): vel = vector(-1,1,0) bullet = sphere(pos=(gun.pos.x+gun.axis.x,gun.pos.y+gun.axis.y,0),radius=(.0785),color=color.green) bullet.v = vector(0,0,0) bullet.v = bullet.v+vel bulletlist.append(bullet) def bulletlistupdate(bulletlist): dt=.01 for a in bulletlist: a.pos=a.pos+a.v*dt def checks(agun,bgun): if scene.kb.keys: key=scene.kb.getkey() if key=='a': moveaup(agun) if key=='s': moveadown(agun) if key=='l': movebup(bgun) if key=='k': movebdown(bgun) if key=='d': shoota(agun) if key=='j': shootb(bgun) #enviroment ground = box(pos=(0,-8,0),size=(50,5,0),color=color.red) wall = box(pos=(0,-8,0),size=(.25,20*random(),0),color=color.red) #playerA abody = box(pos=(-11,-5.25,0),size=(.5,.5,0),color=color.blue) agun = cylinder(pos=(-11,-5.1,0),axis=(.8,.8,0),radius=(.08),color=color.blue) #playerB bbody= box(pos=(11,-5.25,0),size=(.5,.5,0),color=color.yellow) bgun = cylinder(pos=(11,-5.1,0),axis=(-.8,.8,0),radius=(.08),color=color.yellow) bulletlist = [] while True: rate(1000) checks(agun,bgun) bulletlistupdate(bulletlist) </code></pre> <p>Any and all help is welcome!</p> <p>Thanks much!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload