【发布时间】: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
抱歉读了这么久!
【问题讨论】: