TPS 是否有功能冷却时间

Is there a functional cooldown for TPS

首先让我说我对 python 和 pygame 的了解非常有限。在学习了一些教程之后,我决定根据我找到的一些在线教程,尝试使用 pygame 制作一款自上而下的射击游戏。该程序到目前为止有效,但我正在为重新加载功能而苦苦挣扎,该功能会在弹药用完时启动玩家枪支的冷却几秒钟。我已经搜索了几个小时,寻找与我可以使用的冷却时间相关的任何东西,但我无法使任何东西发挥作用。在此先感谢您提供任何帮助或建议,任何能为我指明正确方向的东西。

这里是 link 游戏所基于的教程: http://www.raywenderlich.com/24252/beginning-game-programming-for-teens-with-python

这是我正在努力处理的代码:

if event.type==pygame.MOUSEBUTTONDOWN:
                shoot.play()
                position=pygame.mouse.get_pos()
                acc[1]+=1
                arrows.append([math.atan2(position[1]-(playerpos1[1]+32), position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos[1]+32])

这会发射射弹,但我想添加弹药计数和弹药为 0 时冷却的功能

我尝试过的一些东西一直在使用 pygame 的定时器功能,还有实用程序 mod,但我无法在我的场景中使用它们。

这是我尝试过的示例:

def fire():
    global ammo # ammo = 10
    if ammo == 0:
        cooldown #  cooldown would be the timer, which i can't figure out
        ammo == 10
    else:
        arrows.append([math.atan2(position[1]-(playerpos1[1]+32), position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos[1]+32])
        ammo-=1

if event.type==pygame.MOUSEBUTTONDOWN:
    shoot.play()
    position=pygame.mouse.get_pos()
    acc[1]+=1
    fire()

感谢您的帮助和建议,如果我的 post 格式不正确,请见谅。

您的 fire() 函数中存在一点语法错误。首先,cooldown(我假设它是一个函数)必须用空括号调用:cooldown()。其次,您没有将弹药重置为 10。相反,您询问弹药是否已经等于 10。这是修复:

def fire():
    global ammo
    if ammo == 0:
        cooldown() # call cooldown function with
        ammo = 10 # reset ammo

希望这对您有所帮助,如果我遗漏了什么,请发表评论,我会进行编辑:)

编辑:你要求一个 cooldown() 例子,所以给你。 在不停止整个游戏的情况下执行此操作的一种方法是创建一个计数器变量 运行 与游戏循环并行。例如:

COUNTER = 0
can_fire = True
while True:
    COUNTER += 1

    def fire():
        global ammo
        if not can_fire:
            if COUNTER == cooldown_count + 50: # Or however many loops
                can_fire = True
                ammo = 10
        else:
            pass
        if ammo == 0 and can_fire:
            cooldown_count = COUNTER
            can_fire = False