Posted 05 February 2013 - 11:01 AM
I am new to lua, as you will be able to tell from my code :)/>/>. I am trying to make the classic game snake/worm. My problem is that i would like to read the key, but if no key is pressed in time the key stays the same as last key pressed. I have managed to sort of get this working. I am getting some odd results, the problem is the snake seems to get faster and faster. Any help would be appreciated.
If you run the code, move the snake arround with cursor keys for a 20 seconds, you will see my problem
function locate(x,y)
term.setCursorPos(x,y)
end
function clear()
term.clear()
term.setCursorPos(1,1)
end
function sizetable(tabledata)
for k,v in pairs(tabledata) do
lastkey = k
end
return lastkey
end
function InitSnake()
pos={}
pos["acr"] = 10
pos["dwn"] = 2
table.insert(snake_,1,pos)
pos={}
pos["acr"] = 10
pos["dwn"] = 3
table.insert(snake_,1,pos)
pos={}
pos["acr"] = 10
pos["dwn"] = 4
table.insert(snake_,1,pos)
pos={}
pos["acr"] = 10
pos["dwn"] = 5
table.insert(snake_,1,pos)
pos={}
pos["acr"] = 10
pos["dwn"] = 6
table.insert(snake_,1,pos)
end
function head(acr,dwn,len)
pos = {}
pos["acr"] = acr
pos["dwn"] = dwn
table.insert(snake_,1,pos)
if len ~= nil then
table.remove(snake_,len+1)
end
end
function DisplaySnake()
for i = sizetable(snake_),1,-1 do
locate(snake_[i]["acr"],snake_[i]["dwn"])
term.write("0")
end
end
function TrapSnake(acr,dwn)
if acr > right then
acr = left
end
if acr < left then
acr = right
end
if dwn > bottom then
dwn = top
end
if dwn < top then
dwn = bottom
end
if acr >= left and acr <= right then
acr = acr
end
if dwn >= top and dwn <= bottom then
dwn = dwn
end
return acr,dwn
end
top = 1
bottom = 17
left = 1
right = 51
snake_ = {}
acr_ = 10
dwn_ = 6
clear()
InitSnake()
DisplaySnake()
move = 208
while true do
timerID = os.startTimer(1)
event,p1 = os.pullEvent()
if event == "key" then
move = p1
p1 = "timer"
end
if event == "timer" then
if move == 200 then -- up
dwn_ = dwn_ - 1
acr_,dwn_ = TrapSnake(acr_,dwn_)
elseif move == 208 then -- dwn
dwn_ = dwn_ + 1
acr_,dwn_ = TrapSnake(acr_,dwn_)
elseif move == 203 then -- left
acr_ = acr_ - 1
acr_,dwn_ = TrapSnake(acr_,dwn_)
elseif move == 205 then -- right
acr_ = acr_ + 1
acr_,dwn_ = TrapSnake(acr_,dwn_)
end
clear()
head(acr_,dwn_,5)
DisplaySnake()
end
end
If you run the code, move the snake arround with cursor keys for a 20 seconds, you will see my problem