kamiakze

CC_Snake

May 27th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | None | 0 0
  1.     -- 0 = UP, 1 = RIGHT, 2 = DOWN, 3 = LEFT
  2.    
  3.     local ecran = peripheral.wrap("right")
  4.     term.redirect(ecran)
  5.     local i = 0
  6.     local direction = 1
  7.     local temp = 0
  8.     local isPoint = 0
  9.     local pointX = 0
  10.     local pointY = 0
  11.    
  12.     snakeX = {}
  13.     snakeY = {}
  14.     tempX = {}
  15.     tempY = {}
  16.     diff = 0.5
  17.     for i=1, 100 do
  18.         snakeX[i] = 0
  19.         snakeY[i] = 0
  20.         tempX[i] = 0
  21.         tempY[i] = 0
  22.     end
  23.     local snakeLength = 1
  24.     local continue = 0
  25.     local game = 1
  26.  
  27.  
  28.  
  29.     while game == 1 do
  30.        
  31.         continue = 0
  32.         snakeLength = 0
  33.         direction = 1
  34.  
  35.     while continue == 0 do
  36.        
  37.     paintutils.drawFilledBox(0,0,73,26,1)
  38.         term.setCursorPos(36, 10)
  39.         term.setTextColor(64)
  40.         term.write("NEW GAME ?")
  41.         temp = redstone.getAnalogInput("bottom")
  42.         if temp == 2 then
  43.             continue = 1
  44.         end
  45.         sleep(1)
  46.     end
  47.  
  48.     snakeX[1] = 3
  49.     snakeY[1] = 3
  50.    
  51. while continue == 1 do
  52.     paintutils.drawFilledBox(0,0,73,26,1)
  53.     temp = redstone.getAnalogInput("bottom")
  54.    
  55.     if temp == 3 and direction ~= 3 then
  56.         direction = 1
  57.     elseif temp == 5 and direction ~= 0 then
  58.         direction = 2
  59.     elseif temp == 7 and direction ~= 1 then
  60.         direction =  3
  61.     elseif temp == 12 and direction ~= 2 then
  62.         direction = 0
  63.     end
  64.    
  65.    
  66.     if direction == 0 then
  67.         snakeY[1] = snakeY[1] - 1
  68.     elseif direction == 1 then
  69.         snakeX[1] = snakeX[1] + 1
  70.     elseif direction == 2 then
  71.         snakeY[1] = snakeY[1] + 1
  72.     elseif direction == 3 then
  73.         snakeX[1] = snakeX[1] - 1
  74.     end
  75.    
  76.         for i=2, snakeLength, 1
  77.         do
  78.             paintutils.drawPixel(snakeX[i],snakeY[i], 256)
  79.         end
  80.    
  81.     if snakeX[1] > 70 or snakeX[1] < 1 or snakeY[1] > 25 or snakeY[1] < 1 then
  82.         continue = 0
  83.     end
  84.  
  85.    
  86.    
  87.     for i=1, snakeLength, 1
  88.         do
  89.             tempX[i] = snakeX[i]
  90.             tempY[i] = snakeY[i]
  91.         end
  92.     for i=2, snakeLength, 1
  93.         do
  94.             snakeX[i] = tempX[i-1]
  95.             snakeY[i] = tempY[i-1]
  96.         end
  97.    
  98.     if isPoint == 0 then
  99.         isPoint = 1
  100.         pointX = math.random(71)
  101.         pointY = math.random(26)
  102.     end
  103.  
  104.    
  105.        
  106.    
  107.     paintutils.drawPixel(pointX,pointY, 16384)
  108.    
  109.     if snakeX[1] == pointX and snakeY[1] == pointY then
  110.         snakeLength = snakeLength + 1
  111.         diff = diff - 0.05
  112.         isPoint = 0
  113.     end
  114.    
  115.     paintutils.drawPixel(snakeX[1],snakeY[1], 32768)
  116.    
  117.     for i=2, snakeLength, 1 do
  118.         if snakeX[1] == tempX[i] and snakeY[1] == tempY[i] then
  119.             continue = 0
  120.         end
  121.     end
  122.     sleep(diff)
  123. end
  124.  
  125.  
  126.     paintutils.drawFilledBox(0,0,73,26,1)
  127.     term.setCursorPos(36, 10)
  128.     term.setTextColor(64)
  129.     term.write("GAME OVER")
  130.     sleep(5)
  131.     paintutils.drawFilledBox(0,0,73,26,32768)
  132. end
Add Comment
Please, Sign In to add comment