lua 中 C# 中的 "goto" 循环是否有等效项? (必须兼容 Love2D)

Is there an equivalent for the "goto" loop in C# in lua? (Must be compatible with Love2D)

我目前正在为一个学校项目编写游戏,一种 Space Invader 类型的游戏。我目前正在尝试制作一个显示 "Press R to restart" 的屏幕,以便当玩家按下 R 时游戏会回到开始。就像在 C# 示例中一样:开始:(所有代码)转到开始。所以我的问题是有等效的吗?我在互联网上找不到相关信息。

我已经尝试过 return 循环,但它甚至在开始之前就让游戏崩溃了。我看到 Lua 在 5.2 版本中实际上有一个 goto 循环。但是 Love2D 只支持 Lua 5.1 所以现在我尝试了 repeat ... until (condition) 但它仍然不起作用

代码开头:

repeat

score = 0
enemykills = 0
local start = love.timer.step( )

代码结束:

    love.graphics.setColor(255, 255, 255)
    for _,b in pairs(player.bullets) do
      love.graphics.rectangle("fill", b.x, b.y, 2, 2)
    end
end
until not love.keyboard.isDown("r")

我希望游戏在我按 R 时重新启动,但游戏崩溃或什么都不做。

Love2D 将重复调用您的 love.updatelove.draw 函数。你不需要有这样的循环。您需要做的是记住您的游戏处于 "wait for user to press 'r' to restart" 状态。所以你的代码看起来像这样:

local current_state = "normal"

function love.update(dt)
    if(current_state == "wait") then
        if(love.keyboard.isDown("r")) then
            current_state == "normal"
        end
    else
        --[[Do normal processing]]
    end
end