Advertisement
bobopopcornboy

snake

May 30th, 2024 (edited)
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.86 KB | Gaming | 0 0
  1. local w, h = term.getSize()
  2. a = 0
  3. timer = os.startTimer(0)
  4. --variables
  5. snakecolor = colors.green
  6. nInterval = 0.3
  7. fruitcolor = colors.red
  8. textcolor = colors.yellow
  9. edgecolor = colors.blue
  10. HIGHSCORE = 0
  11. HIGHSCOREP = "NoOne"
  12.  
  13.  
  14. --functions
  15. function drawpixel(x, y, color)
  16.     term.setCursorPos(x, y)
  17.     term.setBackgroundColor(color)
  18.     term.write(" ")
  19.     term.setBackgroundColor(colors.black)
  20. end
  21.  
  22. local function printCentred(y, s)
  23.     local x = math.floor((w - string.len(s)) / 2)
  24.     term.setCursorPos(x, y)
  25.     --term.clearLine()
  26.     term.write(s)
  27. end
  28.  
  29. local function addwall()
  30.     yes = 0
  31.     while true do
  32.         randomx = math.random(1, w)
  33.         randomy = math.random(2, h)
  34.         for i = 1, #sx do
  35.             if sx[i] == randomx and sy[i] == randomy then
  36.                 yes = yes + 1
  37.             end
  38.         end
  39.         for i = 1, #fruitx do
  40.             if fruitx[i] == randomx and fruity[i] == randomy then
  41.                 yes = yes + 1
  42.             end
  43.         end
  44.         if yes == 0 then
  45.             break
  46.         end
  47.     end
  48.     wallx[#wallx + 1] = randomx
  49.     wally[#wally + 1] = randomy
  50. end
  51.  
  52. local function addfruit(p1)
  53.     yes = 0
  54.     while true do
  55.         randomx = math.random(1, w)
  56.         randomy = math.random(2, h)
  57.         for i = 1, #sx do
  58.             if sx[i] == randomx and sy[i] == randomy then
  59.                 yes = yes + 1
  60.             end
  61.         end
  62.         if yes == 0 then
  63.             break
  64.         end
  65.     end
  66.     fruitx[p1] = randomx
  67.     fruity[p1] = randomy
  68. end
  69.  
  70. function drawline(xc, yc, xv, yv, color)
  71.     if xc == xv then
  72.         if yc < yv then
  73.             for i = 1, yv - yc + 1 do
  74.                 drawpixel(xc, yc + i - 1, color)
  75.             end
  76.         else
  77.             for i = 1, yc - yv + 1 do
  78.                 drawpixel(xc, yc - i + 1, color)
  79.             end
  80.         end
  81.     elseif yc == yv then
  82.         if xc < xv then
  83.             for i = 1, xv - xc + 1 do
  84.                 drawpixel(xc + i - 1, yc, color)
  85.             end
  86.         else
  87.             for i = 1, xc - xv + 1 do
  88.                 drawpixel(xc - i + 1, yc, color)
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. function edge()
  95.     drawline(1, 1, 1, h, edgecolor)
  96.     drawline(1, 1, w, 1, edgecolor)
  97.     drawline(w, 1, w, h, colors.blue)
  98.     drawline(1, h, w, h, edgecolor)
  99. end
  100.  
  101. function drawgame()
  102.         term.clear()
  103.         for i = 1, #sx do
  104.             term.setCursorPos(sx[i], sy[i])
  105.             term.setBackgroundColor(snakecolor)
  106.             term.write(" ")
  107.             term.setBackgroundColor(colors.black)
  108.         end
  109.         for i = 1, #fruitx do
  110.             term.setCursorPos(fruitx[i], fruity[i])
  111.             term.setBackgroundColor(fruitcolor)
  112.             term.write(" ")
  113.         end
  114.         for i = 1, #wallx do
  115.             term.setCursorPos(wallx[i], wally[i])
  116.             term.setBackgroundColor(edgecolor)
  117.             term.write(" ")
  118.         end
  119.         term.setBackgroundColor(colors.black)
  120.         if diffeculty >= 2 then
  121.             edge()
  122.         end
  123.         drawline(1,1,w,1,edgecolor)
  124.         term.setBackgroundColor(edgecolor)
  125.         term.setCursorPos(1, 1)
  126.         term.write("score:" .. score*(diffeculty+1))
  127.         term.write(" ".."HIGHSCORE:"..HIGHSCORE.." Holder:\""..HIGHSCOREP.."\"")
  128.         term.setCursorPos(w-string.len("Difficulty:" ..diffeculty), 1)
  129.         term.write("Difficulty:" ..diffeculty)
  130.         term.setBackgroundColor(colors.black)
  131. end
  132.  
  133. function menu(list,clears,where)
  134. selected = 1
  135. if not clears then term.clear() end
  136. term.setTextColor(textcolor)
  137.   for i=1,#list do
  138.     if selected == i then
  139.       printCentred(h/2 - #list/2 + (i-1), "["..list[i].."]")
  140.     else
  141.       printCentred(h/2 - #list/2 + (i-1), list[i])
  142.     end
  143.   end
  144. if where == 1 then printCentred(1,"press BackSpace (<----) to puase") end
  145. while true do
  146.     local event, o1, o2 = os.pullEvent("key")
  147.     term.clear()
  148.     if clears then drawgame() end
  149.     if where == 1 then printCentred(1,"press BackSpace (<----) to puase") end
  150.     if o1 == keys.up then
  151.         selected = selected + -1
  152.     end
  153.     if o1 == keys.down then
  154.         selected = selected - -1
  155.     end
  156.     if selected > #list then
  157.         selected = 1
  158.     end
  159.     if selected < 1 then
  160.         selected = #list
  161.     end
  162.     if o1 == keys.enter then
  163.         break
  164.     end
  165.     for i=1,#list do
  166.       if selected == i then
  167.         printCentred(h/2 - #list/2 + (i-1), "["..list[i].."]")
  168.       else
  169.         printCentred(h/2 - #list/2 + (i-1), list[i])
  170.     end
  171.   end
  172. end
  173. return selected
  174. end
  175.  
  176. function start()
  177. --menu
  178. diffeculty = menu({"easy","normal","HARD","VERY HARD!","IMPOSIBLE!","Leave"},false,1) - 1
  179. --game
  180. nInterval = 0.3
  181. wallx = {}
  182. wally = {}
  183. sx = {6, 5, 4}
  184. sy = {5, 5, 5}
  185. dx = 1
  186. dy = 0
  187. die = false
  188. fb = 0
  189. die = false
  190. if diffeculty > 0 then
  191.     fb = 0.2
  192. end
  193. if diffeculty >= 3 then
  194.     fb = 0.4
  195. end
  196. fruitx = {math.floor(w / 2)}
  197. fruity = {math.floor(h / 2)}
  198. nInterval = nInterval - fb
  199. local timer = os.startTimer(nInterval)
  200. move = 0
  201. if diffeculty ~= 5 then
  202. while true do
  203.     event, o1, o2 = os.pullEvent()
  204.     if event == "timer" and o1 == timer then
  205.         timer = os.startTimer(nInterval)
  206.     end
  207.     if event == "key" then
  208.         if o1 == keys.right and dx == 0 and move == 0 then
  209.             dx = 1
  210.             dy = 0
  211.             move = 1
  212.         end
  213.         if o1 == keys.left and dx == 0 and move == 0 then
  214.             dx = -1
  215.             dy = 0
  216.             move = 1
  217.         end
  218.         if o1 == keys.up and dy == 0 and move == 0 then
  219.             dx = 0
  220.             dy = -1
  221.             move = 1
  222.         end
  223.         if o1 == keys.down and dy == 0 and move == 0 then
  224.             dx = 0
  225.             dy = 1
  226.             move = 1
  227.         end
  228.         if o1 == keys.backspace then
  229.           if menu({"Resume","Difficulty Select"},true) == 2 then
  230.             break
  231.           end
  232.           timer = os.startTimer(nInterval)
  233.         end
  234.         if o1 == keys.space then
  235.             addfruit(#fruitx + 1)
  236.         end
  237.     end
  238.     if event == "timer" then
  239.         move = 0
  240.         table.insert(sx, 1, sx[1] + dx)
  241.         table.insert(sy, 1, sy[1] + dy)
  242.         for i = 1, #sx - 1 do
  243.             if sx[1] == sx[i + 1] and sy[1] == sy[i + 1] then
  244.                 die = true
  245.             end
  246.         end
  247.         for i = 1, #wallx do
  248.             if sx[1] == wallx[i] and sy[1] == wally[i] then
  249.                 die = true
  250.             end
  251.         end
  252.         for i = 1, #fruitx do
  253.             if fruitx[i] == sx[1] and sy[1] == fruity[i] then
  254.                 addfruit(i)
  255.                 if diffeculty == 4 then addwall() end
  256.                 grow = true
  257.             end
  258.         end
  259.         if #fruitx == 0 then
  260.           addfruit(1)
  261.         end
  262.         if not grow then
  263.             sx[#sx] = nil
  264.             sy[#sy] = nil
  265.         end
  266.         grow = false
  267.         if diffeculty < 2 then
  268.             if sx[1] > w then
  269.                 sx[1] = 1
  270.             end
  271.             if sx[1] < 1 then
  272.                 sx[1] = w
  273.             end
  274.             if sy[1] > h then
  275.                 sy[1] = 2
  276.             end
  277.             if sy[1] < 2 then
  278.                 sy[1] = h
  279.             end
  280.         else
  281.             if sx[1] > w - 1 then
  282.                 die = true
  283.             end
  284.             if sx[1] < 2 then
  285.                 die = true
  286.             end
  287.             if sy[1] > h - 1 then
  288.                 die = true
  289.             end
  290.             if sy[1] < 2 then
  291.                 die = true
  292.             end
  293.         end
  294.         score = #sx -3
  295.         drawgame()
  296.         if score*(diffeculty+1) > HIGHSCORE then
  297.             HIGHSCOREP = "you"
  298.             HIGHSCORE = score*(diffeculty+1)
  299.         end
  300.         if die then
  301.             score = score*(diffeculty+1)
  302.             term.clear()
  303.             printCentred(math.floor(h / 2) - 1, "you died")
  304.             printCentred(math.floor(h / 2), "score:" .. score)
  305.             if HIGHSCOREP == "you" then
  306.             HIGHSCOREP = "__"
  307.             printCentred(math.floor(h / 2) + 1, "NEW HIGH SCORE!")
  308.             printCentred(math.floor(h / 2) + 2, "EnterName:"..HIGHSCOREP)
  309.             shift = 0
  310.             while true do
  311.                 local event, key = os.pullEvent("key")
  312.                 if key == keys.leftShift or key == keys.rightShift then
  313.                     shift = 1 - shift
  314.                 end
  315.                 kes = keys.getName(key)
  316.                 if shift == 1 then kes = string.upper(kes) end
  317.                 if #kes == 1 and string.sub(HIGHSCOREP,1,1) ~= "_" then
  318.                     HIGHSCOREP = string.sub(HIGHSCOREP,1,1)
  319.                     HIGHSCOREP = HIGHSCOREP..kes
  320.                 end
  321.                 if #kes == 1 and string.sub(HIGHSCOREP,1,1) == "_" then
  322.                     HIGHSCOREP = kes.."_"
  323.                 end
  324.                 if key == keys.backspace and string.sub(HIGHSCOREP,2,2) == "_" then
  325.                     HIGHSCOREP = "__"
  326.                 end
  327.                 if key == keys.backspace and string.sub(HIGHSCOREP,2,2) ~= "_" then
  328.                     HIGHSCOREP = string.sub(HIGHSCOREP,1,1)
  329.                     HIGHSCOREP = HIGHSCOREP.."_"
  330.                 end
  331.                 if key == keys.enter and string.sub(HIGHSCOREP,2,2) ~= "_" then
  332.                     break
  333.                 end
  334.                 printCentred(math.floor(h / 2) + 2, "EnterName:"..HIGHSCOREP)
  335.             end
  336.             printCentred(math.floor(h / 2) + 1, "NEW HIGH SCORE!")
  337.             printCentred(math.floor(h / 2) + 2, "press ENTER to play agian")
  338.             else
  339.             printCentred(math.floor(h / 2) + 1, "press ENTER to play agian")
  340.             end
  341.             while true do
  342.                 local event, key = os.pullEvent("key")
  343.                 if key == keys.enter then
  344.                     break
  345.                 end
  346.             end
  347.             break
  348.         end
  349.     end
  350. end
  351. start()
  352. end
  353. term.clear()
  354. term.setCursorPos(1,1)
  355. end
  356. start()
Tags: snake
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement