JustDoesGames

Clicker Game

Jul 29th, 2020 (edited)
2,461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.64 KB | None | 0 0
  1. --[[
  2.  
  3. Clicker
  4. Just Does Games
  5.  
  6. --]]
  7.  
  8. local w,h = term.getSize()
  9. if not term.isColor() then error("Advanced Computer Required") end
  10. local run, inmenu, debug = true, false, false -- debug erases any save data
  11.  
  12. function createClickable()
  13.     if #clicks ~= sv.clicker.game.maxClicks then
  14.         table.insert(clicks, {math.random(1,w), math.random(2,h-1)}) -- Exclude the top and bottom for menu reasons
  15.     end
  16. end
  17.  
  18. function checkClick(x,y)
  19.     for i=1, #clicks do
  20.         if x == clicks[i][1] and y == clicks[i][2] then return {true,i} end
  21.     end
  22.     return false
  23. end
  24.  
  25. function resetClicks()
  26.     clicks = {}
  27.     for i=1, sv.clicker.game.totalClicks do createClickable() end -- reset all clickables just in case
  28.     --error(clicks[1][1].." "..clicks[1][2])
  29. end
  30.  
  31. function display()
  32.     term.setBackgroundColor(sv.clicker.colors.background)
  33.     term.clear() -- usually I do not do this because of the flickering but this is a really small game
  34.     term.setBackgroundColor(sv.clicker.colors.click)
  35.     for i=1, #clicks do
  36.         --error(clicks[i][1].." "..clicks[i][2] )
  37.         term.setCursorPos(clicks[i][1], clicks[i][2])
  38.         write(" ")
  39.     end
  40.     term.setTextColor(sv.clicker.colors.moneyText)
  41.     term.setBackgroundColor(sv.clicker.colors.background)
  42.     term.setCursorPos(1,1)
  43.     write("Money: "..sv.clicker.player.money)
  44.     term.setTextColor(sv.clicker.colors.clickWorthText)
  45.     term.setCursorPos(w-string.len(sv.clicker.game.clickWorth.."/"..sv.clicker.game.totalClicks),1)
  46.     write(sv.clicker.game.clickWorth.."/"..sv.clicker.game.totalClicks)
  47.     term.setTextColor(sv.clicker.colors.text)
  48.     term.setCursorPos(1,h)
  49.     if not inmenu then
  50.     write("Press 'CTRL' for menu")
  51.     end
  52. end
  53.  
  54. function upgrade(t)
  55.     local cost = upgradeCal()
  56.     if sv.clicker.player.money >= cost then
  57.         sv.clicker.player.money = sv.clicker.player.money - cost
  58.         if t == 1 then
  59.             sv.clicker.game.clickWorth = sv.clicker.game.clickWorth + 1
  60.         elseif t == 2 then
  61.             if sv.clicker.game.totalClicks ~= sv.clicker.game.maxClicks then
  62.                 sv.clicker.game.totalClicks = sv.clicker.game.totalClicks + 1
  63.                 resetClicks()
  64.             else
  65.                 sv.clicker.player.money = sv.clicker.player.money + cost
  66.             end
  67.         elseif t == 3 then
  68.             sv.clicker.game.aClickerWorth = sv.clicker.game.aClickerWorth + 1
  69.         end
  70.         sleep(.1) display()
  71.     end
  72. end
  73.  
  74. function runClicker()
  75.     while true do
  76.         sleep(1)
  77.         sv.clicker.player.money = sv.clicker.player.money + (sv.clicker.game.clickWorth*sv.clicker.game.aClickerWorth)
  78.     end
  79. end
  80.  
  81. function runMenu()
  82.     local sel, r = 1, true
  83.     inmenu = true
  84.     while r do -- use r to leave (to avoid rep clicking)
  85.         local menu = {
  86.             {"Upgrade Worth - $"..upgradeCal(), function() upgrade(1) end},
  87.             {"Upgrade Clicks - $"..upgradeCal(), function() upgrade(2) end},
  88.             {"Upgrade Auto Clicker - $"..upgradeCal(), function() upgrade(3) end},
  89.             {"Save and Exit", function() run, r = false, false end}
  90.         }
  91.         while true do -- use break to leave
  92.             term.setCursorPos(1,h)
  93.             term.clearLine()
  94.             term.setCursorPos(1,h)
  95.             term.setTextColor(sv.clicker.colors.menuText)
  96.             write("< "..menu[sel][1].." >")
  97.             a,b = os.pullEvent()
  98.             if a == "key" then
  99.                 if b == keys.leftCtrl or b == keys.rightCtrl or b == keys.q then
  100.                     r = false
  101.                 elseif b == keys.d or b == keys.right then
  102.                     if sel ~= #menu then
  103.                         sel = sel + 1
  104.                     end
  105.                 elseif b == keys.a or b == keys.left then
  106.                     if sel ~= 1 then
  107.                         sel = sel - 1
  108.                     end
  109.                 elseif b == keys.enter or b == keys.e then
  110.                     menu[sel][2]() break
  111.                 end
  112.             elseif a == "mouse_click" then
  113.                 r = false
  114.             end
  115.             if not r then break end
  116.         end
  117.     end
  118.     inmenu = false
  119. end
  120.  
  121. function runGame()
  122.     while run do
  123.         display()
  124.         a,b,x,y = os.pullEvent()
  125.         if a == "key" then -- menu things
  126.             if b == keys.q then
  127.                 run = false
  128.             elseif b == keys.rightCtrl or b == keys.leftCtrl then
  129.                 runMenu()
  130.             end
  131.         elseif a == "mouse_click" or a == "mouse_drag" then -- mouse drag because... ... fuck you
  132.             local tmp = checkClick(x,y)
  133.             if tmp then
  134.                 table.remove(clicks, tmp[2])
  135.                 createClickable()
  136.                 sv.clicker.player.money = sv.clicker.player.money + sv.clicker.game.clickWorth
  137.             end
  138.         end
  139.     end
  140. end
  141.  
  142. local saveDir = "/saves/sv.lua" -- just throwing things around lol
  143.  
  144. function save(dir)
  145.     local f = fs.open(dir, "w")
  146.     if f then f.write(textutils.serialize(sv)) f.close() end
  147. end
  148.  
  149. if debug then save(saveDir) end
  150.  
  151. function load(dir)
  152.     local f = fs.open(dir, "r")
  153.     if f then sv = textutils.unserialize(f.readAll()) f.close() end
  154. end
  155.  
  156. load(saveDir)
  157. sv = sv or {}
  158. sv.clicker = sv.clicker or {}
  159.  
  160. sv.clicker.player, sv.clicker.game, sv.clicker.colors = sv.clicker.player or {}, sv.clicker.game or {}, sv.clicker.colors or {}
  161.  
  162. sv.clicker.player.money = sv.clicker.player.money or 0
  163.  
  164. sv.clicker.game.clickWorth = sv.clicker.game.clickWorth or 1
  165. --sv.clicker.game.clicks = sv.clicker.game.clicks or {}
  166. local clicks = {}
  167. sv.clicker.game.totalClicks = sv.clicker.game.totalClicks or 1
  168. sv.clicker.game.maxClicks = sv.clicker.game.maxClicks or 60
  169. sv.clicker.game.upgradeBase = sv.clicker.game.upgradeBase or 6 -- base
  170.  
  171. sv.clicker.game.aClickerWorth = sv.clicker.game.aClickerWorth or 0
  172.  
  173. function upgradeCal() return math.floor(((sv.clicker.game.clickWorth+sv.clicker.game.totalClicks+sv.clicker.game.aClickerWorth)+(sv.clicker.game.clickWorth*sv.clicker.game.totalClicks*sv.clicker.game.aClickerWorth))*sv.clicker.game.upgradeBase) end
  174.  
  175. sv.clicker.colors.background = sv.clicker.colors.background or colors.black
  176. sv.clicker.colors.text = sv.clicker.colors.text or colors.white
  177. sv.clicker.colors.moneyText = sv.clicker.colors.moneyText or colors.lime
  178. sv.clicker.colors.menuText = sv.clicker.colors.menuText or colors.white
  179. sv.clicker.colors.clickWorthText = sv.clicker.colors.clickWorthText or colors.yellow
  180. sv.clicker.colors.click = sv.clicker.colors.click or colors.green
  181.  
  182. resetClicks()
  183. parallel.waitForAny(runGame, runClicker)
  184. save(saveDir)
  185.  
  186. term.clear()
  187. term.setCursorPos(1,1) sleep(.1) -- to avoid other issues sleep for .1
  188.  
  189. return true
Advertisement
Add Comment
Please, Sign In to add comment