LUAdev

Button clicker game

Nov 30th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. --[[
  2.     This program is made by Engineer on these forums: http://www.computercraft.info/forums2/
  3.     When this program is being used please credit to the author; no legal actions will be taken in any way. Even when this is not done, but it would be nice.
  4. ]]--
  5.  
  6.  
  7. if not os.loadAPI("redtracker") then
  8.     local urlHandle = http.get("http://pastebin.com/raw.php?i=ANDVZV8V")
  9.     local fileHandle = fs.open("redtracker", "w")
  10.     fileHandle.write(urlHandle.readAll())
  11.     urlHandle.close()
  12.     fileHandle.close()
  13.     os.loadAPI("redtracker")
  14. end
  15.  
  16. local tArgs = { ... }
  17. if #tArgs == 0 then
  18.     print("No arguments given")
  19.     return
  20. end
  21.  
  22. local shouldCount = true
  23. local order = {}
  24. local tracker = {}
  25. local runUntilKey = false
  26. local count = 100
  27.  
  28. local function checkSide( sSide )
  29.     for _, side in pairs(rs.getSides()) do
  30.         if sSide == side or string.find(side, sSide) then
  31.             return side
  32.         end
  33.     end
  34.     return false
  35. end
  36.  
  37. local function updateScreen()
  38.     term.setBackgroundColour(colours.white)
  39.     term.setTextColour(colours.black)
  40.     term.clear()
  41.     term.setCursorPos(1, 1)
  42.     local w, h = term.getSize()
  43.     for index, side in pairs(order) do
  44.         print(string.format("Player %d", index))
  45.         term.setBackgroundColour(math.pow(2, index + 1))
  46.        
  47.         local clicks = tostring(tracker[side])
  48.         local width = math.floor(tracker[side] / count * w)
  49.         write(clicks)
  50.         if width > #clicks then
  51.             write(string.rep(" ", width - #clicks))
  52.         end
  53.         print()
  54.         term.setBackgroundColour(colours.white)
  55.     end
  56. end
  57.  
  58. local function callback(side, active)
  59.     if active and shouldCount then
  60.         tracker[side] = 1 + tracker[side]
  61.         if tracker[side] == count then
  62.             shouldCount = false
  63.         end
  64.         updateScreen()
  65.     end
  66. end
  67.  
  68. for _, option in pairs( tArgs ) do
  69.     if option:sub(1, 2) == "s:" then
  70.         local side = checkSide(option:sub(3, #option))
  71.         if side then
  72.             order[#order + 1] = side
  73.             tracker[side] = 0
  74.             redtracker.addCallback(callback, side)
  75.         end
  76.     elseif option:sub(1, 2) == "m:" then
  77.         local side = option:sub(3, #option)
  78.         if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
  79.             term.redirect(peripheral.wrap(side))
  80.         end
  81.     elseif option:sub(1, 2) == "c:" then
  82.         local countInput = tonumber(option:sub(3, #option))
  83.         if countInput > 0 then
  84.             count = countInput
  85.         end
  86.     elseif option == "rununtilkey" then
  87.         runUntilKey = not runUntilKey
  88.     end
  89. end
  90.  
  91. repeat
  92.     updateScreen()
  93.     while shouldCount do
  94.         local event = os.pullEvent()
  95.         if event == "redstone" then
  96.             redtracker.update()
  97.         elseif event == "key" and runUntilKey then
  98.             runUntilKey = false
  99.         elseif event == "monitor_resize" or event == "term_resize" then
  100.             updateScreen()
  101.         end
  102.     end
  103.  
  104.     term.clear()
  105.     term.setCursorPos(1, 1)
  106.     for index, side in pairs(order) do
  107.         if tracker[side] == count then
  108.             print(string.format("Player %d wins!", index))
  109.             print("Touch the screen to continue")
  110.             os.pullEvent("monitor_touch")
  111.             break
  112.         end
  113.     end
  114.     for side, clicks in pairs(tracker) do
  115.         tracker[side] = 0
  116.     end
  117.     shouldCount = true
  118. until not runUntilKey
  119.  
  120. term.setBackgroundColour(colours.black)
  121. term.clear()
  122. if term.restore then
  123.     term.restore()
  124. else
  125.     term.redirect(term.native())
  126. end
Advertisement
Add Comment
Please, Sign In to add comment