Advertisement
bryceio

Computercraft Snake

Aug 29th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. term.clear()
  2. segmentx = {2}
  3. segmenty = {2}
  4. score = 0
  5. width, height = term.getSize()
  6. applex = math.random(1, width-1)
  7. appley = math.random(2, 18)
  8. direction = 0
  9. i = 2
  10.  
  11. function controls()
  12.     keytimer = os.startTimer(0.05)
  13.     event, key = os.pullEvent()
  14.     if event == "key" then
  15.         sleep(0.05)
  16.         if key == 200 and direction ~= 3 then
  17.             direction = 1
  18.         end
  19.         if key == 205 and direction ~= 4 then
  20.             direction = 2
  21.         end
  22.         if key == 208 and direction ~= 1 then
  23.             direction = 3
  24.         end
  25.         if key == 203 and direction ~= 2 then
  26.             direction = 4
  27.         end
  28.     elseif event == "timer" and key == keytimer then
  29.     end
  30. end
  31.  
  32.  
  33. --Make the first apple
  34. term.setCursorPos(applex, appley)
  35. print("@")
  36.  
  37. --Make the first snake part
  38. term.setCursorPos(segmentx[1], segmenty[1])
  39. print("0")
  40.  
  41. function endgame()
  42.    
  43. end
  44.  
  45. while true do
  46.     term.setCursorPos(1, 1)
  47.     term.write("Score: "..score.."  =========================================")
  48.     term.setCursorPos(applex, appley)
  49.     term.write("@")
  50.     term.setCursorPos(1, 19)
  51.     term.write("===================================================")
  52.     controls()
  53.     term.clear()
  54.     posx = segmentx[1]
  55.     posy = segmenty[1]
  56.     if direction == 1 then
  57.         segmenty[1] = segmenty[1]-1
  58.     elseif direction == 2 then
  59.         segmentx[1] = segmentx[1]+1
  60.     elseif direction == 3 then
  61.         segmenty[1] = segmenty[1]+1
  62.     elseif direction == 4 then
  63.         segmentx[1] = segmentx[1]-1
  64.     end
  65.     term.setCursorPos(segmentx[1], segmenty[1])
  66.     term.write("0")
  67.     while i < score + 2 do
  68.              pos2X = segmentx[i]
  69.              pos2Y = segmenty[i]
  70.              segmentx[i] = posx
  71.              segmenty[i] = posy
  72.              posx = pos2X
  73.              posy = pos2Y
  74.              if segmentx[1] == segmentx[i] and segmenty[1] == segmenty[i] then
  75.                  error()
  76.              end
  77.              term.setCursorPos(segmentx[i], segmenty[i])
  78.                 if applex == segmentx[i] and appley == segmenty[i] then
  79.                     applex = math.random(1, width-1)
  80.                     appley = math.random(2, 18)
  81.                 end
  82.              term.write("0")
  83.              i = i + 1
  84.     end
  85.     i = 2
  86.     if segmentx[1] == applex and segmenty[1] == appley then
  87.         score = score + 1
  88.         applex = math.random(1, width-1)
  89.         appley = math.random(2, 18)
  90.         if direction == 1 then
  91.             table.insert(segmentx, segmentx[score])
  92.             table.insert(segmenty, segmenty[score]+1)
  93.         elseif direction == 2 then
  94.             table.insert(segmentx, segmentx[score]-1)
  95.             table.insert(segmenty, segmenty[score])
  96.         elseif direction == 3 then
  97.             table.insert(segmentx, segmentx[score])
  98.             table.insert(segmenty, segmenty[score]-1)
  99.         elseif direction == 4 then
  100.             table.insert(segmentx, segmentx[score]+1)
  101.             table.insert(segmenty, segmenty[score])
  102.         end
  103.     elseif segmentx[1] > width or segmentx[1] < 1 or segmenty[1] > 18 or segmenty[1] < 2 then
  104.         error()
  105.     end
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement