我的移动队列有什么问题为什么输入会重复

  • 本文关键字:问题 队列 移动 lua love2d
  • 更新时间 :
  • 英文 :


我正试图用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

抱歉阅读时间过长!

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

不过我做了一些调整,看起来它们稍微改进了它的功能。我使用elseif而不是if(这样一次只能按下一个键(,并将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

最新更新