Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- segmentx = {2}
- segmenty = {2}
- score = 0
- width, height = term.getSize()
- applex = math.random(1, width-1)
- appley = math.random(2, 18)
- direction = 0
- i = 2
- function controls()
- keytimer = os.startTimer(0.05)
- event, key = os.pullEvent()
- if event == "key" then
- sleep(0.05)
- if key == 200 and direction ~= 3 then
- direction = 1
- end
- if key == 205 and direction ~= 4 then
- direction = 2
- end
- if key == 208 and direction ~= 1 then
- direction = 3
- end
- if key == 203 and direction ~= 2 then
- direction = 4
- end
- elseif event == "timer" and key == keytimer then
- end
- end
- --Make the first apple
- term.setCursorPos(applex, appley)
- print("@")
- --Make the first snake part
- term.setCursorPos(segmentx[1], segmenty[1])
- print("0")
- function endgame()
- end
- while true do
- term.setCursorPos(1, 1)
- term.write("Score: "..score.." =========================================")
- term.setCursorPos(applex, appley)
- term.write("@")
- term.setCursorPos(1, 19)
- term.write("===================================================")
- controls()
- term.clear()
- posx = segmentx[1]
- posy = segmenty[1]
- if direction == 1 then
- segmenty[1] = segmenty[1]-1
- elseif direction == 2 then
- segmentx[1] = segmentx[1]+1
- elseif direction == 3 then
- segmenty[1] = segmenty[1]+1
- elseif direction == 4 then
- segmentx[1] = segmentx[1]-1
- end
- term.setCursorPos(segmentx[1], segmenty[1])
- term.write("0")
- while i < score + 2 do
- pos2X = segmentx[i]
- pos2Y = segmenty[i]
- segmentx[i] = posx
- segmenty[i] = posy
- posx = pos2X
- posy = pos2Y
- if segmentx[1] == segmentx[i] and segmenty[1] == segmenty[i] then
- error()
- end
- term.setCursorPos(segmentx[i], segmenty[i])
- if applex == segmentx[i] and appley == segmenty[i] then
- applex = math.random(1, width-1)
- appley = math.random(2, 18)
- end
- term.write("0")
- i = i + 1
- end
- i = 2
- if segmentx[1] == applex and segmenty[1] == appley then
- score = score + 1
- applex = math.random(1, width-1)
- appley = math.random(2, 18)
- if direction == 1 then
- table.insert(segmentx, segmentx[score])
- table.insert(segmenty, segmenty[score]+1)
- elseif direction == 2 then
- table.insert(segmentx, segmentx[score]-1)
- table.insert(segmenty, segmenty[score])
- elseif direction == 3 then
- table.insert(segmentx, segmentx[score])
- table.insert(segmenty, segmenty[score]-1)
- elseif direction == 4 then
- table.insert(segmentx, segmentx[score]+1)
- table.insert(segmenty, segmenty[score])
- end
- elseif segmentx[1] > width or segmentx[1] < 1 or segmenty[1] > 18 or segmenty[1] < 2 then
- error()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement