This is quite simple but i didnt want to add anymore such as if it goes out of bounds and such until i get this working.
function setXY(x,y) -- Sets X and Y position
term.setCursorPos(x,y)
end
function clearScreen() -- Clears Screen
term.clear()
term.setCursorPos(1,1)
end
function drawSnake(x, y, length) -- Draws the snake
for i = 1, length do
setXY(x,y)
term.write("O")
end
end
function keyPress()
local delay = os.startTimer(.1)
local event, pull1 = os.pullEvent()
if event == "key" then
snakeDir = pull1
elseif event == "timer" and pull1 == delay then -- Checks if the event is a timer and if the delay is == to the pull
if snakeDir == 17 then -- Up
posY = posY - 1
elseif snakeDir == 31 then -- Down
posY = posY + 1
elseif snakeDir == 30 then -- Left
posX = posX - 1
elseif snakeDir == 32 then -- Right
posX = posX + 1
end
end
clearScreen()
end
snakeDir = 32
posX = 5
posY = 10
clearScreen()
while true do
keyPress()
drawSnake(posX, posY, 1)
end
So when i run this code without pressing any buttons it move across the screen but when you click a key such a S it will go down or it will stop, it seems to vary randomly.
So basicly the buttons to comtrol the snake do not work.