Advertisement
RemiCraft

Cookie Clicker

Nov 19th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.80 KB | None | 0 0
  1. local w, h = term.getSize() -- First getting the size of the computer screen display
  2. menustate = "cookie"
  3. terminate = false
  4. cookies = 0
  5. cps = 0
  6.  
  7. function round(n)
  8.   return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
  9. end
  10.  
  11. function printCentered(sText)
  12.     local x, y = term.getCursorPos()
  13.     x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
  14.     term.setCursorPos(x, y)
  15.     print(sText)
  16. end
  17.  
  18. local function drawMenuColour() -- creates the colours of the menu
  19.     paintutils.drawFilledBox(0, 0, w, h, colours.grey) -- Bottom Grey
  20.     paintutils.drawFilledBox(0, 0, w, 2, colours.orange) -- top 2 Cyan
  21. end
  22.  
  23. local function drawTitle()
  24.     term.setTextColour(colours.black)
  25.     term.setBackgroundColour(colours.orange)
  26.     term.setCursorPos(12,1)
  27.     print("- CCLICKERS")     --Title
  28.     print(string.rep("=", w))
  29.     term.setBackgroundColour(colours.lightGrey)
  30. end
  31.  
  32. local function drawHeadings() -- creates the game statistic displays
  33.   if menustate == "cookie" then
  34.     term.setBackgroundColour(colours.grey)
  35.     term.setTextColour(colours.lightGrey)
  36.     paintutils.drawLine(2,4,27,4, colours.grey)
  37.     term.setCursorPos(2, 4)
  38.     print("Cookies:", cookies)
  39.     paintutils.drawLine(2,5,27,5, colours.grey)
  40.     term.setCursorPos(2,5)
  41.     print("CPS:", cps)
  42.   end
  43. end
  44.  
  45. local function drawCookie()
  46.   if menustate == "cookie" then
  47.     paintutils.drawFilledBox(4,7,13,13, colours.orange)
  48.     paintutils.drawPixel(4,7, colours.grey)
  49.     paintutils.drawPixel(13,7, colours.grey)
  50.     paintutils.drawPixel(4,13, colours.grey)
  51.     paintutils.drawPixel(13,13, colours.grey)
  52.     paintutils.drawPixel(5,9, colours.black)
  53.     paintutils.drawPixel(7,12, colours.black) -- W,H,Colour
  54.     paintutils.drawPixel(10,10, colours.black)
  55.     paintutils.drawPixel(8,8, colours.black)
  56.     paintutils.drawPixel(11,12, colours.black)
  57.     paintutils.drawPixel(12,9, colours.black)
  58.   end
  59. end
  60.  
  61. local function drawExit()
  62.   term.setBackgroundColour(colours.red)
  63.   term.setTextColour(colours.white)
  64.   term.setCursorPos(2, h-1)
  65.   print("EXIT")
  66. end
  67.  
  68. local function drawMenuSwap()
  69.   term.setBackgroundColour(colours.blue)
  70.   term.setTextColor(colours.white)
  71.   term.setCursorPos(w-4, h-1)
  72.   if menustate == "cookie" then
  73.     print("SHOP")
  74.   else
  75.     print("BACK")
  76.   end
  77. end
  78.  
  79. local function exitPrompt()
  80.   --making fancy boxes
  81.   paintutils.drawFilledBox(2,6,25,14, colours.black)
  82.   paintutils.drawFilledBox(2,6,25,7, colours.orange)
  83.   --Making a fancy title
  84.   term.setBackgroundColour(colours.orange)
  85.   term.setTextColour(colours.black)
  86.   term.setCursorPos(9,6)
  87.   print("EXIT MENU")
  88.   term.setCursorPos(2,7)
  89.   print(string.rep("=", 24))
  90.   --making fancy text
  91.   term.setBackgroundColour(colours.black)
  92.   term.setTextColour(colours.white)
  93.   term.setCursorPos(3, 9)
  94.   print("Are you sure you want")
  95.   term.setCursorPos(9,10)
  96.   print("to exit?")
  97.   --Making fancy Yes/No boxes
  98.   term.setBackgroundColour(colours.green)
  99.   term.setCursorPos(9, 12)
  100.   print("YES")
  101.   term.setBackgroundColour(colours.red)
  102.   term.setCursorPos(15, 12)
  103.   print("NO ")
  104.   -- using the exit GUI
  105.   while true do
  106.     local event, button, x, y = os.pullEventRaw("mouse_click") -- It enables you to select with the mouse
  107.     if (event == "mouse_click" and x >= 1  and 25 >= x and y >= 6  and 14 >= y) then -- if you click in the window zone
  108.       if (event == "mouse_click" and x >= 9 and 11 >= x and y == 12) then -- and if you also click on the 'YES'
  109.         return true
  110.       elseif (x >= 15 and 18 >= x and y == 12) then -- and if you also click on the 'NO'
  111.         return false
  112.       end
  113.     elseif button == 28 then -- If you hit enter
  114.       return true
  115.     else
  116.       return false
  117.     end
  118.   end
  119. end
  120.  
  121. pickaxePrice = 25
  122. villagerPrice = 75
  123. farmPrice = 300
  124. minePrice = 1000
  125. factoryPrice = 7500
  126. portalPrice = 25000
  127.  
  128. --loading the save file
  129. local lines = {}
  130. local cfg = fs.open( "programs/cclicker.save", "r" )
  131. for line in cfg.readLine do
  132.     lines[#lines+1] = line
  133. end
  134. cfg.close()
  135.  
  136. --[[
  137. The save file is now saved as a table, and each line of the save data may be called using:
  138. For line x = lines[x]
  139. To write to the save file or change one specific line, use:
  140. lines[y] = foo
  141. ]]
  142.  
  143. --Load save file
  144. cookies = round(lines[1])
  145. pickaxeAmount = round(lines[2])
  146. villagerAmount = round(lines[3])
  147. farmAmount = round(lines[4])
  148. mineAmount = round(lines[5])
  149. factoryAmount = round(lines[6])
  150. portalAmount = round(lines[7])
  151.  
  152. local function drawUpgrades()
  153.   drawMenuSwap()
  154.   if menustate == "upgrades" then
  155.     --PICKAXE Upgrade menu
  156.     if cookies >= pickaxePrice then -- If you have enough money, it's green
  157.       paintutils.drawFilledBox(w-24, 4, w-14, 7, colours.green)
  158.       term.setTextColour(colours.black)
  159.       term.setBackgroundColour(colours.green)
  160.     else -- If you don't have enough money, it's red
  161.       paintutils.drawFilledBox(w-24, 4, w-14, 7, colours.red)
  162.       term.setTextColour(colours.white)
  163.       term.setBackgroundColour(colours.red)
  164.     end
  165.     -- Writing the text
  166.     term.setCursorPos(w-22,4)
  167.     print("Pickaxe")
  168.     term.setCursorPos(w-22,5)
  169.     print("0.3 CPS")
  170.     term.setCursorPos(w-22,6)
  171.     print("$",pickaxePrice)
  172.     term.setCursorPos(w-22,7)
  173.     print("Own: ",pickaxeAmount)
  174.  
  175.     --VILLAGER upgrade menu
  176.     if cookies >= villagerPrice then -- If you have enough money, it's green
  177.       paintutils.drawFilledBox(w-11, 4, w-1, 7, colours.green)
  178.       term.setTextColour(colours.black)
  179.       term.setBackgroundColour(colours.green)
  180.     else -- If you don't have enough money, it's red
  181.       paintutils.drawFilledBox(w-11, 4, w-1, 7, colours.red)
  182.       term.setTextColour(colours.white)
  183.       term.setBackgroundColour(colours.red)
  184.     end
  185.     --Writing the text
  186.     term.setCursorPos(w-10,4)
  187.     print("Villager")
  188.     term.setCursorPos(w-10,5)
  189.     print("1 CPS")
  190.     term.setCursorPos(w-10,6)
  191.     print("$",villagerPrice)
  192.     term.setCursorPos(w-10,7)
  193.     print("Own: ",villagerAmount)
  194.  
  195.     -- FARM upgrade menu
  196.     if cookies >= farmPrice then -- If you have enough money, it's green
  197.       paintutils.drawFilledBox(w-24, 9, w-14, 12, colours.green)
  198.       term.setTextColour(colours.black)
  199.       term.setBackgroundColour(colours.green)
  200.     else -- If you don't have enough money, it's red
  201.       paintutils.drawFilledBox(w-24, 9, w-14, 12, colours.red)
  202.       term.setTextColour(colours.white)
  203.       term.setBackgroundColour(colours.red)
  204.     end
  205.     --Writing the text
  206.     term.setCursorPos(w-22,9)
  207.     print("Farm")
  208.     term.setCursorPos(w-22,10)
  209.     print("3 CPS")
  210.     term.setCursorPos(w-22,11)
  211.     print("$",farmPrice)
  212.     term.setCursorPos(w-22,12)
  213.     print("Own: ",farmAmount)
  214.  
  215.     --Mine Upgrade Menu
  216.     if cookies >= minePrice then -- If you have enough money, it's green
  217.       paintutils.drawFilledBox(w-11, 9, w-1, 12, colours.green)
  218.       term.setTextColour(colours.black)
  219.       term.setBackgroundColour(colours.green)
  220.     else -- If you don't have enough money, it's red
  221.       paintutils.drawFilledBox(w-11, 9, w-1, 12, colours.red)
  222.       term.setTextColour(colours.white)
  223.       term.setBackgroundColour(colours.red)
  224.     end
  225.     --Writing the text
  226.     term.setCursorPos(w-10,9)
  227.     print("Mine")
  228.     term.setCursorPos(w-10,10)
  229.     print("5 CPS")
  230.     term.setCursorPos(w-10,11)
  231.     print("$",minePrice)
  232.     term.setCursorPos(w-10,12)
  233.     print("Own: ",mineAmount)
  234.  
  235.     --Factory Upgrade Menu
  236.     if cookies >= factoryPrice then -- If you have enough money, it's green
  237.       paintutils.drawFilledBox(w-24, 14, w-14, 17, colours.green)
  238.       term.setTextColour(colours.black)
  239.       term.setBackgroundColour(colours.green)
  240.     else -- If you don't have enough money, it's red
  241.       paintutils.drawFilledBox(w-24, 14, w-14, 17, colours.red)
  242.       term.setTextColour(colours.white)
  243.       term.setBackgroundColour(colours.red)
  244.     end
  245.     --Writing the text
  246.     term.setCursorPos(w-22,14)
  247.     print("Factory")
  248.     term.setCursorPos(w-22,15)
  249.     print("15 CPS")
  250.     term.setCursorPos(w-22,16)
  251.     print("$",factoryPrice)
  252.     term.setCursorPos(w-22,17)
  253.     print("Own: ",factoryAmount)
  254.  
  255.     --Portal Upgrade Menu
  256.     if cookies >= portalPrice then -- If you have enough money, it's green
  257.       paintutils.drawFilledBox(w-11, 14, w-1, 17, colours.green)
  258.       term.setTextColour(colours.black)
  259.       term.setBackgroundColour(colours.green)
  260.     else -- If you don't have enough money, it's red
  261.       paintutils.drawFilledBox(w-11, 14, w-1, 17, colours.red)
  262.       term.setTextColour(colours.white)
  263.       term.setBackgroundColour(colours.red)
  264.     end
  265.     --Writing the text
  266.     term.setCursorPos(w-10,14)
  267.     print("Portal")
  268.     term.setCursorPos(w-10,15)
  269.     print("50 CPS")
  270.     term.setCursorPos(w-10,16)
  271.     print("$",portalPrice)
  272.     term.setCursorPos(w-10,17)
  273.     print("Own: ",portalAmount)
  274.   end
  275. end
  276.  
  277. -- Functions to run the game
  278.  
  279. local function runGame()
  280.   while true do -- The continuous loop of the game
  281.     drawTitle()
  282.     drawExit()
  283.     drawHeadings() -- draws in all the data
  284.     local event, button, x, y = os.pullEventRaw() -- if there is a mouse click
  285.     if menustate == "cookie" then -- if you're on the cookie page
  286.       if (event == "mouse_click" and x >= 4  and 13 >= x and y >= 7  and 13 >= y) then -- and if that mouse click is on the cookie
  287.         cookies = cookies + 1 -- you get more cookies
  288.       end
  289.       if (event == "mouse_click" and w-1 >= x and x >= w-4 and y == h-1) then -- If you hit SHOP / BACK
  290.         menustate = "upgrades"
  291.         drawMenuColour()
  292.         drawUpgrades()
  293.       end
  294.     elseif menustate == "upgrades" then -- if you're on the menu page
  295.       if (event == "mouse_click" and x >= w-24 and w-14 >= x and y >= 4 and 7 >= y) then -- PICKAXE CLICK
  296.         if cookies >= pickaxePrice then
  297.           cookies = cookies - pickaxePrice
  298.           pickaxeAmount = pickaxeAmount + 1
  299.         end
  300.       end
  301.       if (event == "mouse_click" and x >= w-11 and w-1 >= x and y >= 4 and 7 >= y) then -- VILLAGER CLICK
  302.         if cookies >= villagerPrice then
  303.           cookies = cookies - villagerPrice
  304.           villagerAmount = villagerAmount + 1
  305.         end
  306.       end
  307.       if (event == "mouse_click" and x >= w-23 and w-13 >= x and y >= 9 and 12 >= y) then -- FARM CLICK
  308.         if cookies >= villagerPrice then
  309.           cookies = cookies - farmPrice
  310.           farmAmount = farmAmount + 1
  311.         end
  312.       end
  313.       if (event == "mouse_click" and x >= w-11 and w-1 >= x and y >= 9 and 12 >= y) then -- MINE CLICK
  314.         if cookies >= minePrice then
  315.           cookies = cookies - minePrice
  316.           mineAmount = mineAmount + 1
  317.         end
  318.       end
  319.       if (event == "mouse_click" and x >= w-23 and w-13 >= x and y >= 14 and 17 >= y) then -- FACTORY CLICK
  320.         if cookies >= factoryPrice then
  321.           cookies = cookies - factoryPrice
  322.           factoryAmount = factoryAmount + 1
  323.         end
  324.       end
  325.       if (event == "mouse_click" and x >= w-11 and w-1 >= x and y >= 14  and 17 >= y) then -- PORTAL CLICK
  326.         if cookies >= portalPrice then
  327.           cookies = cookies - portalPrice
  328.           portalAmount = portalAmount + 1
  329.         end
  330.       end
  331.       if (event == "mouse_click" and w-1 >= x and x >= w-4 and y == h-1) then -- If you hit SHOP / BACK
  332.         menustate = "cookie"
  333.         drawMenuColour()
  334.         drawCookie()
  335.       end
  336.     end
  337.     if (event == "mouse_click" and 5 >= x and x >= 2 and y == h-1) then -- If you hit EXIT
  338.       return
  339.     end
  340.     if event == "terminate" then
  341.       break
  342.     end
  343.     cps = (pickaxeAmount*0.3) + (villagerAmount*1) + (farmAmount*3) + (mineAmount*5) + (factoryAmount*15) + (portalAmount*50)
  344.     pickaxePrice = 25 + (pickaxeAmount*5)
  345.     villagerPrice = 75 + (villagerAmount*20)
  346.     farmPrice = 300 + (farmAmount*75)
  347.     minePrice = 1000 + (mineAmount*350)
  348.     factoryPrice = 7500 + (factoryAmount*1200)
  349.     portalPrice = 25000 + (portalAmount*10000)
  350.     drawUpgrades()
  351.   end
  352. end
  353.  
  354. local function runUpgrades()
  355.   while true do -- continuously adding cookies every amount of seconds for the different runUpgrades
  356.     sleep(1)
  357.     cps = (pickaxeAmount*0.3) + (villagerAmount*1) + (farmAmount*3) + (mineAmount*5) + (factoryAmount*15) + (portalAmount*50)
  358.     cookies = cookies + cps
  359.     drawHeadings() -- draws in all the data
  360.     drawUpgrades()
  361.   end
  362. end
  363.  
  364. -- Running the game
  365.  
  366. drawMenuColour()
  367. drawTitle()
  368. drawExit()
  369. drawCookie()
  370. drawUpgrades()
  371.  
  372. while true do
  373.   parallel.waitForAny(runGame, runUpgrades)
  374.   if exitPrompt() then
  375.     break
  376.   else
  377.     drawMenuColour()
  378.     drawTitle()
  379.     drawExit()
  380.     drawCookie()
  381.   end
  382. end
  383.  
  384. lines[1] = round(cookies)
  385. lines[2] = round(pickaxeAmount)
  386. lines[3] = round(villagerAmount)
  387. lines[4] = round(farmAmount)
  388. lines[5] = round(mineAmount)
  389. lines[6] = round(factoryAmount)
  390. lines[7] = round(portalAmount)
  391.  
  392. local h = fs.open( "programs/cclicker.save", "w" )
  393. for i = 1, #lines do
  394.   h.writeLine( lines[i] )
  395. end
  396. h.close()
  397.  
  398. shell.run("main.lua")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement