Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Stocks
- Game Built in 2 hours
- Just Does Games
- ]]--
- term.clear()
- local w,h = term.getSize()
- if not term.isColor() then return printError("Advanced Computer Required.") end
- local run = true
- local seed = os.epoch("utc") -- true randomness
- math.randomseed(seed)
- math.random(1,1) -- to avoid prediction w/ CraftOS-PC
- local menuGlobalSelected = 1
- local player = {}
- player.money = 200
- player.location = 1
- player.stock = 0
- local stock = {}
- stock.level = 0 -- current level
- stock.value = 1 -- inc and dec values
- stock.maxrate = 50 -- rng max
- stock.minrate = 2 -- rng min
- stock.rate = math.random(1, stock.maxrate) -- controls the ups and downs
- stock.caps = 200 -- max up and down
- stock.stability = 5 -- 5 seconds of freedom
- local ms = {}
- ms.main = {{"Buy", "Sell", "Exit"}, {
- --function() table.insert(ms.history, ms.stocks) end,
- function() purchase_stock() end,
- function() sell_stock() end,
- --function() end,
- function() run = false end,
- }}
- ms.stocks = {{"test", "Exit"}, {
- function() end,
- function() table.remove(ms.history, #ms.history) menuGlobalSelected = 1 end,
- }}
- ms.history = {ms.main}
- --[[
- Menu engine
- ]]--
- function m(x,y,x2,y2,menu)
- menu = menu[1] or error("Failed to load Menu.")
- x,y = x or error("Failed to load x"), y or error("Failed to load y")
- x2,y2 = x2 or error("Failed to load x2"), y2 or error("Failed to load y2")
- local sel, scroll, run, w, h = 1,0,true, term.getSize()
- sel = menuGlobalSelected
- while run do
- for i=1, y2-y do
- if menu[i+scroll] ~= nil then
- term.setCursorPos(x,y+i-1)
- term.setTextColor(colors.white)
- if sel == i then write("> ") else write(" ") end write(menu[i+scroll])
- end
- end
- a,b = os.pullEvent("key")
- if b == keys.w or b == keys.up then
- if sel == 1 and scroll ~= 0 then
- scroll = scroll - 1
- elseif sel ~= 1 then
- sel = sel - 1
- end
- elseif b == keys.s or b == keys.down then
- if sel == y2-y and scroll ~= #menu-(y2-y) then
- scroll = scroll + 1
- elseif sel ~= math.min(y2-y,#menu) then
- sel = sel + 1
- end
- elseif b == keys.e or b == keys.enter then
- --paintutils.drawFilledBox(x,y,x2,y2,colors.black)
- menuGlobalSelected = sel
- return sel+scroll
- end
- end
- paintutils.drawFilledBox(x,y,x2,y2,colors.black)
- end
- --[[
- Menu engine
- ]]--
- function updateGUI()
- term.setCursorPos(1,h-2)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- print("Stock: "..player.stock.." ")
- print("Money: "..player.money.." ")
- end
- function purchase_stock()
- local cost = stock.level
- if cost > 0 then cost = cost * 2 end
- if player.money >= math.abs(cost) then
- player.stock = player.stock + 1
- player.money = player.money - math.abs(cost)
- end
- end
- function sell_stock()
- local pay = stock.level
- if pay < 0 then pay = math.ceil(pay/2) end
- if player.stock > 0 then
- player.stock = player.stock - 1
- player.money = player.money + pay
- end
- end
- function runMenu()
- while run do
- ms.history[#ms.history][2][m(3,3,w,h,ms.history[#ms.history])]()
- updateGUI()
- end
- end
- function stock_rate_cycle()
- while run do
- sleep(stock.stability)
- stock.rate = math.random(1, stock.maxrate)
- player.money = player.money + 1
- updateGUI()
- --term.setCursorPos(1,2)
- --print(stock.rate.." | "..math.floor(math.abs(stock.level)/stock.caps*10))
- end
- end
- function runStock()
- while run do
- sleep(.08)
- term.setCursorPos(1,1) term.setTextColor(colors.white) write("Market ")
- term.setBackgroundColor(colors.gray) write(" ")
- term.setBackgroundColor(colors.black)
- if stock.rate < math.floor(stock.maxrate/2) then
- term.setTextColor(colors.lime)
- elseif stock.rate > math.floor(stock.maxrate/2) then
- term.setTextColor(colors.red)
- else
- term.setTextColor(colors.orange)
- end
- write(" $"..stock.level.." ")
- term.setCursorPos(8,1)
- local res = math.random(1,stock.maxrate)
- if res > stock.rate then
- stock.level = math.min(stock.level+stock.value, stock.caps)
- res = "^"
- elseif res < stock.rate then
- stock.level = math.max(stock.level-stock.value, stock.caps*-1)
- res = "!"
- else
- res = "-"
- end
- if stock.level > 0 then term.setBackgroundColor(colors.lime) else term.setBackgroundColor(colors.red) end
- --print("Stock: "..stock.level.." "..res.." ")
- for i=1, math.floor(math.abs(stock.level)/stock.caps*10) do
- write(" ")
- end
- term.setBackgroundColor(colors.black)
- --print(math.abs(stock.level)/stock.caps.." | "..stock.level.."/"..stock.caps)
- end
- end
- function run()
- updateGUI()
- parallel.waitForAny(runStock, stock_rate_cycle, runMenu)
- end
- --[[
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- print("How To Play Stocks:")
- print("")
- print("Buy Stocks while it is in the red.")
- print("Buying Stocks while in the green will make them cost twice as much.")
- print("")
- print("Sell Stocks while it is in the green.")
- print("Selling Stocks while in the red will make them lose twice as much.")
- print("")
- print("Sorry if this seems incomplete, this was a challenge to see what I could make in 2 hours.")
- print("")
- print("Goal: Become a Millionaire!")
- sleep(5)
- print("")
- print("Press any key to start...")
- os.pullEvent()
- term.clear()
- sleep(1)
- --]]
- run()
- term.clear()
- term.setCursorPos(1,1)
- sleep(.2)
Advertisement
Add Comment
Please, Sign In to add comment