无法理解为什么游戏状态在游戏中没有改变

发布于 2025-01-17 19:37:55 字数 967 浏览 5 评论 0原文

我是编码初学者,目前正在参加哈佛大学的 GD50 课程。我无法弄清楚我的代码有什么问题。我想做的是按下回车键时更新游戏状态。我实现了一个基本菜单,如果您按一个,您将被发送到开始状态,这工作正常,但我不知道如何将其与按不同的键结合起来实际开始游戏(更新到服务状态)等等)。希望我的解释很清楚。任何帮助将不胜感激。这是我的代码(请说如果我需要发布更多):

function love.keypressed(key)
    if key == 'escape' then
        love.event.quit()

    elseif key == 'm' then 
        gameState = 'menu'

    elseif key == '1' then
        if gameState == 'menu' then 
            gameState = 'start'
        end

    elseif key == 'enter' then
        if gameState == 'start' then
            gameState = 'serve'
        elseif gameState == 'serve' then
            gameState = 'play'
        elseif gameState == 'done' then
            gameState = 'serve'

            ball:reset()
        
            player1Score = 0
            player2Score = 0
        
            if winningPlayer == 1 then
                servingPlayer = 2
            else
                servingPlayer = 1
            end
        end
    end
end

I'm a beginner at coding and I am currently doing GD50 by Harvard. I cannot figure out what is wrong with my code here. What i'm trying to do is update the game state when enter is pressed. I implemented a basic menu where if you press one, you are sent to the start state, which is working fine, but i can't figure out how to combine this with pressing a different key to actually start the game(updating to serve state and so on). Hope i was clear with the explanation. Any help would be appreciated. Here's my code(pls say if i need to post more):

function love.keypressed(key)
    if key == 'escape' then
        love.event.quit()

    elseif key == 'm' then 
        gameState = 'menu'

    elseif key == '1' then
        if gameState == 'menu' then 
            gameState = 'start'
        end

    elseif key == 'enter' then
        if gameState == 'start' then
            gameState = 'serve'
        elseif gameState == 'serve' then
            gameState = 'play'
        elseif gameState == 'done' then
            gameState = 'serve'

            ball:reset()
        
            player1Score = 0
            player2Score = 0
        
            if winningPlayer == 1 then
                servingPlayer = 2
            else
                servingPlayer = 1
            end
        end
    end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文