Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何运行python的秒数?_Python_Arrays_Python 3.x_Time_Pygame - Fatal编程技术网

如何运行python的秒数?

如何运行python的秒数?,python,arrays,python-3.x,time,pygame,Python,Arrays,Python 3.x,Time,Pygame,我有我的pygame游戏我有电源产卵。当玩家收集它们时,它会得到一个特殊的能量。如何使通电仅运行特定的秒数?因为我不想让他们永远跑下去 这就是问题所在 for p in p_power: if man.hitbox[0] < p.hitbox[0] + p.hitbox[2] < man.hitbox[0] + man.hitbox[2] or man.hitbox[0] + man.hitbox[2] > p.hitbox[0] > man.hitbox[0]:

我有我的pygame游戏我有电源产卵。当玩家收集它们时,它会得到一个特殊的能量。如何使通电仅运行特定的秒数?因为我不想让他们永远跑下去

这就是问题所在

for p in p_power:
    if man.hitbox[0] < p.hitbox[0] + p.hitbox[2] < man.hitbox[0] + man.hitbox[2] or man.hitbox[0] + man.hitbox[2] > p.hitbox[0] > man.hitbox[0]:
        if man.hitbox[1] <= p.hitbox[1] + p.hitbox[3]:
            if p.index == 0:  # health power up, ads one to the health
                if man.health < 10:
                    man.health += 1
                p_power.pop(p_power.index(p))

            elif p.index == 1:
                while p_end > 0: # Problem! tries to run for 10 seconds. Makes the players gun cooldown less so that it can shoot faster for 10 secs
                    delay.cooldown = 75
p在p_电源中的p
:
如果man.hitbox[0]p.hitbox[0]>man.hitbox[0]:
if man.hitbox[1]0:#问题!尝试运行10秒。使玩家的枪冷却时间减少,这样它可以在10秒内更快地射击
延迟。冷却时间=75

不太熟悉
pygame
,而且这个片段没有提供令人难以置信的上下文

也许是这样的

import datetime

for p in p_power:
    if man.hitbox[0] < p.hitbox[0] + p.hitbox[2] < man.hitbox[0] + man.hitbox[2] or man.hitbox[0] + man.hitbox[2] > p.hitbox[0] > man.hitbox[0]:
        if man.hitbox[1] <= p.hitbox[1] + p.hitbox[3]:
            if p.index == 0:  # health power up, ads one to the health
                if man.health < 10:
                    man.health += 1
                p_power.pop(p_power.index(p))

            elif p.index == 1:
                p_start = datetime.datetime.now()
                while (datetime.datetime.now() - p_start).seconds => 10: 
                    # Do the powerup
                    # Don't know if this belongs in a while loop though, maybe should be an if statement
                    # Is this called in the main game loop synchronously?
                    pass
导入日期时间
对于p/U功率中的p:
如果man.hitbox[0]p.hitbox[0]>man.hitbox[0]:
if man.hitbox[1]10:
#通电
#不知道这是否属于while循环,也许应该是if语句
#这是在主游戏循环中同步调用的吗?
通过

正如我在代码中所评论的,我不确定这是否真的属于
while
循环。这段代码是否在主游戏循环中的某个点同步调用?如果是这样的话,可能应该使用
If
语句对其进行类似的检查。

您可以使用
time

import time
将函数定义为
start\u time=time.time()
,您可以在函数中的任何位置使用该函数,然后使用
(start\u time-time.time())
可以检查变量
start\u time
启动以来经过了多少时间,并将其与
if
while
一起使用以设置通电

for p in p_power:
    if man.hitbox[0] < p.hitbox[0] + p.hitbox[2] < man.hitbox[0] + man.hitbox[2] or man.hitbox[0] + man.hitbox[2] > p.hitbox[0] > man.hitbox[0]:
        if man.hitbox[1] <= p.hitbox[1] + p.hitbox[3]:
            if p.index == 0:  # health power up, ads one to the health
                if man.health < 10:
                    man.health += 1
                p_power.pop(p_power.index(p))

            elif p.index == 1:
                start_time = time.time()
                while p_end > 0: # Problem! tries to run for 10 seconds. Makes the players gun cooldown less so that it can shoot faster for 10 secs
                    while (time.time()-start_time) <= 10:
                        delay.cooldown = 75
p在p_电源中的p
:
如果man.hitbox[0]p.hitbox[0]>man.hitbox[0]:
if man.hitbox[1]0:#问题!尝试运行10秒。使玩家的枪冷却时间减少,这样它可以在10秒内更快地射击

而(time.time()-start_time)如果您在浏览器中搜索“Python定时器”,您会找到比我们在这里管理的更好的解释这一点的参考资料。这可以通过使用[pygame.time.set_timer]来完成[.您可以启动通电,并创建一个延迟事件,该事件将在时间过去后停止通电。遗憾的是,无法为您提供更多帮助,因为您提供的代码片段我不清楚。