CCHacker132

Extreme_Snake

Feb 13th, 2020
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.82 KB | None | 0 0
  1. local filename = 'extreme_snake_highscore.dat'
  2.  
  3. if not fs.exists(filename) then
  4.   local file = fs.open(filename,'w')
  5.   file.write('0')
  6.   file.close()
  7. end
  8. local file = fs.open(filename,'r')
  9. local hscore = file.readLine()
  10. if not tonumber(hscore) then
  11.   hscore = 0
  12.   local file = fs.open(filename,'w')
  13.   file.write('0')
  14.   file.close()
  15. end
  16.  
  17. hscore = tonumber(hscore)
  18.  
  19. local length = {}
  20. local fruit = {{13,6,false},{17,8,false}}
  21. local paused = false
  22. for num = 1,6 do
  23.   table.insert(length,{(7-num)+6,6})
  24. end
  25. local speed = 1
  26. local size = 6
  27. local x, y = 12,6
  28. local direction = 0
  29.  
  30. --Settings
  31. local color = colors.lime
  32. local color2 = colors.green
  33. --
  34.  
  35. local timer = 0
  36. local dead = false
  37. local width, height = term.getSize()
  38. local score = -100
  39. term.setBackgroundColor(colors.gray)
  40. term.setCursorPos(1,1)
  41. term.clearLine()
  42.  
  43. local function pauseHandler()
  44.   while true do
  45.     local ev = {os.pullEvent('key')}
  46.     if ev[2] == 28 then
  47.       if paused then
  48.         paused = false
  49.       else
  50.         paused = true
  51.         term.setCursorPos((width/2)-4,height/2)
  52.         term.setTextColor(colors.yellow)
  53.         term.setBackgroundColor(colors.black)
  54.         write('[PAUSED]')
  55.       end
  56.     end
  57.   end
  58. end
  59.  
  60. local function display()
  61.   while true do
  62.   repeat sleep(0.05) until paused == false
  63.   term.setBackgroundColor(colors.black)
  64.   for curheightc = 2,height do
  65.     term.setCursorPos(1,curheightc)
  66.     term.clearLine()
  67.   end
  68.   for i,v in pairs(fruit) do
  69.     if v[3] then
  70.       paintutils.drawPixel(v[1],v[2],colors.yellow)
  71.     else
  72.       paintutils.drawPixel(v[1],v[2],colors.red)
  73.     end
  74.   end
  75.   for i,v in pairs(length) do
  76.     if (i+1)%3 == 0 then
  77.       paintutils.drawPixel(v[1],v[2],color2)
  78.     else
  79.       paintutils.drawPixel(v[1],v[2],color)
  80.     end
  81.   end
  82.   end
  83. end
  84. local function lose()
  85.   repeat sleep(0.05) until dead
  86. end
  87. local function update()
  88.   while true do
  89.     timer = timer + 1
  90.     if timer%speed == 0 then
  91.       if direction == 0 then
  92.         x = x + 1
  93.       elseif direction == 1 then
  94.         y = y + 1
  95.       elseif direction == 2 then
  96.         x = x - 1
  97.       elseif direction == 3 then
  98.         y = y - 1
  99.       end
  100.     if length[size] then
  101.       table.remove(length,size)
  102.     end
  103.     table.insert(length,1,{x,y})
  104.     end
  105.     repeat sleep(0.05) until paused == false
  106.     for i,v in pairs(length) do
  107.       if i ~= 1 then
  108.         if v[1] == x and v[2] == y then
  109.           dead = true
  110.         end
  111.       end
  112.     end
  113.     if x < 1 or x > width or y < 2 or y > height then
  114.       dead = true
  115.     end
  116.     for i,v in pairs(fruit) do
  117.       if v[1] == x and v[2] == y then
  118.         table.remove(fruit,i)
  119.         if v[3] then
  120.           size = size + 5
  121.           score = score + 500
  122.         else
  123.           size = size + 1
  124.           score = score + 100
  125.         end
  126.         local special = math.random(1,5)
  127.         if special == 1 then
  128.           special = true
  129.         else
  130.           special = false
  131.         end
  132.         local xposn = math.random(2,width-1)
  133.         local yposn = math.random(3,height-1)
  134.         for i,v in pairs(fruit) do
  135.           if xposn == v[1] then
  136.             if xposn ~= 2 then
  137.               xposn = xposn - 1
  138.             else
  139.               xposn = xposn + 1
  140.             end
  141.           end
  142.           if yposn == v[2] then
  143.             if yposn ~= 3 then
  144.               yposn = yposn - 1
  145.             else
  146.               yposn = yposn + 1
  147.             end
  148.           end
  149.         end
  150.         table.insert(fruit,{xposn,yposn,special})
  151.       end
  152.     end
  153.   end
  154. end
  155. local function scoreCount()
  156.   while true do
  157.     sleep(0.05)
  158.     term.setTextColor(colors.cyan)
  159.     term.setBackgroundColor(colors.gray)
  160.     term.setCursorPos(width-#('SCORE: '..score),1)
  161.     write('SCORE: '..score)
  162.     term.setCursorPos(2,1)
  163.     write('LENGTH: '..#length)
  164.     term.setCursorPos(20,1)
  165.     write('HIGHSCORE: '..hscore)
  166.   end
  167. end
  168. local function control()
  169.   while true do
  170.     sleep(0.01)
  171.     local ev = {os.pullEvent('key')}
  172.     local num = 0
  173.     if ev[2] == keys.right and direction ~= 2 then
  174.       direction = 0
  175.     elseif ev[2] == keys.down and direction ~= 3 then
  176.       direction = 1
  177.     elseif ev[2] == keys.left and direction ~= 0 then
  178.       direction = 2
  179.     elseif ev[2] == keys.up and direction ~= 1 then
  180.       direction = 3
  181.     end
  182.   end
  183. end
  184.  
  185. parallel.waitForAny(pauseHandler,lose,display,update,control,scoreCount)
  186. term.setBackgroundColor(colors.black)
  187.  
  188. if score > hscore then
  189.     term.setCursorPos((width/2)-7,height/2)
  190.     term.setTextColor(colors.yellow)
  191.     print('[New highscore!]')
  192.     local file = fs.open(filename,'w')
  193.     file.writeLine(score)
  194.     file.close()
  195.     sleep(0.2)
  196.     os.pullEvent('key')
  197. else
  198.     term.setCursorPos((width/2)-4,height/2)
  199.     term.setTextColor(colors.red)
  200.     print('[You died!]')
  201.     sleep(0.2)
  202.     os.pullEvent('key')
  203. end
  204.  
  205. term.setCursorPos(1,1)
  206. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment