【问题标题】:What Is Wrong With My Move Queue Why Do Inputs Get Repeated?我的移动队列出了什么问题 为什么输入会重复?
【发布时间】:2020-12-24 18:57:52
【问题描述】:

我正在尝试使用 love2d 制作一个简单的蛇式游戏,但我似乎无法弄清楚我的代码存在什么问题。运行时的错误是角色移动基于队列系统,并且似乎得到了相同输入的支持。非常感谢任何帮助!

love.window.setMode(704,704)



function love.load(arg)
  x = 0
  y = 0

  timer = 0
  points = 0

  moveQueue = {}

  playersize = 32
  screensize = 704

  Applex = love.math.random(0, screensize / playersize) * playersize
  Appley = love.math.random(0, screensize / playersize) * playersize
end





function love.update(dt)

  --Inputs
  if love.keyboard.isDown( "s" ) and moveQueue[2] ~= 's' then
    table.insert(moveQueue, 's')
  end
  if love.keyboard.isDown( "w" ) and moveQueue[2] ~= 'w' then
    table.insert(moveQueue, 'w')
  end
  if love.keyboard.isDown( "a" ) and moveQueue[2] ~= 'a' then
    table.insert(moveQueue, 'a')
  end
  if love.keyboard.isDown( "d" ) and moveQueue[2] ~= 'd' then
    table.insert(moveQueue, 'd')
  end


  timer = timer + dt
    local timerLimit = 0.15
    if timer >= timerLimit then
        timer = timer - timerLimit

          if moveQueue[1] == 'w' and y > 0 then
            y = y - playersize
            table.remove(moveQueue, 1)
          end
          if moveQueue[1] == 's' and y < screensize - playersize then
            y = y + playersize
            table.remove(moveQueue, 1)
          end
          if moveQueue[1] == 'a' and x > 0 then
            x = x - playersize
            table.remove(moveQueue, 1)
          end
          if moveQueue[1] == 'd' and x < screensize - playersize then
            x = x + playersize
            table.remove(moveQueue, 1)
          end


        if x == Applex and y == Appley then
          points = points + 1

          Applex = love.math.random(0, screensize / playersize) * playersize
          Appley = love.math.random(0, screensize / playersize) * playersize

        end



        print('Tick')
    end



end



function love.draw()

  love.graphics.rectangle("fill", x, y, playersize, playersize)

  love.graphics.setColor(255,0,0)

  love.graphics.rectangle("fill", Applex, Appley, playersize, playersize)

  love.graphics.setColor(255,255,255)


end

抱歉读了这么久!

【问题讨论】:

    标签: lua love2d


    【解决方案1】:

    这真的很难知道,因为我不确定你的游戏的功能应该是什么。如果它应该和蛇完全一样,那么你正在做的一些事情我真的很困惑,比如队列。

    我做了一些调整,虽然看起来他们稍微改进了它的功能。我使用 elseif 而不是 ifs(一次只能按下一个键)并且我将 table.remove(moveQueue, 1) 从 if 语句中移出(这样每次调用 love.update(dt) 时都会从队列中删除一些东西,不管键是否按下。

    我不知道这是否符合您的要求,因为我不确定您的游戏应该如何运行。

    love.window.setMode(704,704)
    
    function love.load(arg)
      x = 0
      y = 0
      timer = 0
      points = 0
      moveQueue = {}
      playersize = 32
      screensize = 704
    
      Applex = love.math.random(0, screensize / playersize) * playersize
      Appley = love.math.random(0, screensize / playersize) * playersize
    
    end
    
    
    function love.update(dt)
    
      --Inputs
        if love.keyboard.isDown( "s" ) and moveQueue[2] ~= 's' then
            table.insert(moveQueue, 's')
        elseif love.keyboard.isDown( "w" ) and moveQueue[2] ~= 'w' then
            table.insert(moveQueue, 'w')
        elseif love.keyboard.isDown( "a" ) and moveQueue[2] ~= 'a' then
            table.insert(moveQueue, 'a')
        elseif love.keyboard.isDown( "d" ) and moveQueue[2] ~= 'd' then
            table.insert(moveQueue, 'd')
        end
    
    
        timer = timer + dt
        local timerLimit = 0.15
        
        if timer >= timerLimit then
            timer = timer - timerLimit
    
              if moveQueue[1] == 'w' and y > 0 then
                y = y - playersize
              elseif moveQueue[1] == 's' and y < screensize - playersize then
                y = y + playersize
              elseif moveQueue[1] == 'a' and x > 0 then
                x = x - playersize
              elseif moveQueue[1] == 'd' and x < screensize - playersize then
                x = x + playersize
              end
              table.remove(moveQueue, 1)
    
    
            if x == Applex and y == Appley then
              points = points + 1
    
              Applex = love.math.random(0, screensize / playersize) * playersize
              Appley = love.math.random(0, screensize / playersize) * playersize
    
            end
    
            print('Tick')
    
        end
    
    end
    
    
    function love.draw()
    
      love.graphics.rectangle("fill", x, y, playersize, playersize)
      love.graphics.setColor(255,0,0)
      love.graphics.rectangle("fill", Applex, Appley, playersize, playersize)
      love.graphics.setColor(255,255,255)
    
    end
    

    【讨论】:

    • 啊抱歉不够清楚我只是想要一个游戏队列系统,稍后我将添加其他功能
    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2017-03-13
    相关资源
    最近更新 更多