简体   繁体   English

我正在尝试使用vPython计算密钥的持续时间

[英]I'm trying to time how long a key is held down using vPython

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. 在整个学期中,我们使用vPython来模拟情境并获得确切的答案等。我们的最终项目是使用包含某种物理类型的vPython创建游戏。

I chose to remake Bowman except with tanks. 我选择重拍Bowman,除了坦克。 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. 我有很好的程序完成但是我被困在一些不同的东西上。

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. 我拥有它所以我从每个大炮射击但是我希望能够按住一个键并且取决于它的持续时间,初始速度将越快。

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. 我有一个大致的想法,但我只是不知道将它放入哪个功能。

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? 有没有办法为此设置一系列值?

Here is my 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<pi/2):
        theta=theta+dtheta
        if not (theta>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>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>pi/2):
        theta=theta-dtheta
        if not (theta<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<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)

Any and all help is welcome! 欢迎任何和所有帮助!

Thanks much! 非常感谢!

You can time things in python using the time module 你可以使用time模块在python中time

import time

start = time.time()
finish = time.time()
print start # 1386269106.18
print finish # 1386269111.11
print (finish - start) # 4.9276599884

So when the player first starts pressing the button, save the time. 因此,当玩家第一次开始按下按钮时,请保存时间。 Then save the time again when the player stops pressing the button. 然后当播放器停止按下按钮时再次节省时间。 The difference between these two times is the number of seconds the player held the button. 这两次之间的差异是玩家按住按钮的秒数。

[Edit] If all you can do is check if the key is pressed down, you can get the time inside the main loop and calculate dt (the amount of time that has passed): [编辑]如果您只能检查是否按下了键,您可以在主循环中获取时间并计算dt(已经过的时间):

t = time.time()
while True:
    new_t = time.time()
    dt = new_t - t
    t = new_t

    rate(1000)
    checks(agun,bgun, dt)
    bulletlistupdate(bulletlist)

Then pass dt to checks, and if the key is pressed down, you know the key has been held down for another dt seconds, and you can add it to your running total of time that it has been held down. 然后将dt传递给检查,如果按下该键,则表示该键已被按下另一个dt秒,并且您可以将其添加到您按下该按键的运行总时间。

For the random you would need to enter a command similar to this: 对于随机,您需要输入与此类似的命令:

    random.randrange(5,31) #this would give you a random range between the numbers 4-30

I don't want to do your homework for you as I don't think you are asking for that. 我不想为你做功课,因为我认为你没有要求。 I hope this helps you. 我希望这可以帮助你。

I'm sorry this should be the correct code for you: 对不起,这应该是适合您的正确代码:

    random.randint(7,40)  # this would get you a random integer of 7-40

I apologize for the misinformation. 我为错误的信息道歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果按住某个键,我将如何实施? - How would I implement if a key is held down? 我正在尝试使用vpython创建一个轨道模拟器,但是当我运行它时,我只会看到黑屏 - I'm trying to create an orbital simulator using vpython but when I run it I just get a black screen 按住键时如何使精灵移动 - How can I make a sprite move when key is held down 如何检测Tkinter中是否按住了某个键? - How to detect if a key is being held down in Tkinter? 按住键时如何移动对象? - How to move an object when a key is held down? 如何使用 pygame.KEYDOWN 在按住键的同时每次通过循环执行某些操作? - How to use pygame.KEYDOWN to execute something every time through a loop while the key is held down? 只要按下拍摄按钮,如何继续拍摄 - How to keep shooting as long as the shoot button is being held down 按住方向键时,如何让方块在屏幕上不断移动? - How do I get the square to continually move across the screen as a direction key is held down? 长按射击键如何发射多发子弹 - How to shoot multiple bullets as long as the shoot button is held down 如何使用pyautogui检查特定的组合键和鼠标左键是否按下? - How to check if a specific Key combination and mouse left button is held down using pyautogui?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM