Guest User

Untitled

a guest
Dec 26th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. local t = peripheral.wrap("top") --Monitor
  2. t.setTextScale(0.5)
  3.  
  4. local currentGame = {} --table for current game{name, offset, balance, current bet, base bet}
  5.  
  6. local function setupCurrentGame(name, offset, bal, bet, base)
  7.     currentGame.name = name
  8.     currentGame.offset = offset
  9.     currentGame.bal = bal
  10.     currentGame.bet = bet
  11.     currentGame.base = base
  12. end
  13.  
  14. local function clearCurrentGame()
  15.     count = #currentGame
  16.     for i =1, count do
  17.         currentGame[i] = nil
  18.     end
  19. end
  20.  
  21. local b = {  --table for roulette game buttons {text, x, y, active, bg colour, fg colour, inactive bg colour, inactive fg colour}
  22.             {text = "ODD", x = 3, y = 10, active = false, bg = 32768, fg = 1, inactivebg = 256, inactivefg = 1},
  23.             {text = " 0 ", x = 7, y = 10, active = false, bg = 8192, fg = 1, inactivebg = 256, inactivefg = 1},
  24.             {text = "EVN", x = 11, y = 10, active = false, bg = 16384, fg = 1, inactivebg = 256, inactivefg = 1},
  25.             {text = "SPIN!", x = 6, y = 12, active = false, bg = 32, fg = 1, inactivebg = 256, inactivefg = 1},
  26.             {text = "0", x = 8, y = 18, active = true, bg = 128, fg = 1},
  27.             {text = " +", x = 9, y = 18, active = true, bg = 8192, fg = 1},
  28.             {text = " >", x = 11, y = 18, active = true, bg = 32, fg = 1},
  29.             {text = ' '..string.char(194,59), x = 13, y = 18, active = true, bg = 8192, fg = 1},
  30.             {text = "- ", x = 6, y = 18, active = true, bg = 16384, fg = 1},
  31.             {text = "< ", x = 4, y = 18, active = true, bg = 64, fg = 1},
  32.             {text = string.char(194,43)..' ', x = 2, y = 18, active = true, bg = 16384, fg = 1}
  33.             --,{text = "Quit", x = 8, y = 12, active = true, bg = 16384, fg = 1}
  34. }
  35.  
  36. local function drawButton(buttonTable, i)
  37.     t.setCursorPos(buttonTable[i].x, buttonTable[i].y)
  38.     if buttonTable[i].active then
  39.         t.setBackgroundColor(buttonTable[i].bg)
  40.         t.setTextColor(buttonTable[i].fg)
  41.     else
  42.         t.setBackgroundColor(buttonTable[i].inactivebg)
  43.         t.setTextColor(buttonTable[i].inactivefg)
  44.     end  
  45.     t.write(buttonTable[i].text)
  46. end
  47.  
  48. local function drawButtons(buttonTable)
  49.     for i=1, #buttonTable do
  50.         drawButton(buttonTable, i)
  51.     end
  52. end
  53.  
  54. local function drawBackground() --Set cyan background
  55.     t.setBackgroundColor(512)
  56.     t.clear()
  57. end
  58.  
  59. local function drawStatic() --Draw all static boxes and words
  60.     --Credits and bet boxes
  61.     t.setCursorPos(3, 15)
  62.     t.write("        BET")
  63.     t.setCursorPos(3, 16)
  64.     t.write("           ")
  65. end
  66.  
  67. local function drawBal()
  68.     t.setBackgroundColor(128)
  69.     t.setTextColor(1)
  70.     t.setCursorPos(3, 21)
  71.     t.write("           ")
  72.     text = tostring(currentGame.bal)
  73.     t.setCursorPos(14 - #text, 21)
  74.     t.write(text)
  75. end
  76.  
  77. local function drawBet()
  78.     t.setBackgroundColor(128)
  79.     t.setTextColor(1)
  80.     t.setCursorPos(3, 16)
  81.     t.write("           ")
  82.     text = tostring(currentGame.bet)
  83.     t.setCursorPos(14 - #text, 16)
  84.     t.write(text)
  85. end
  86.  
  87. local function checkSpinButton()
  88.     if currentGame.bet > 0 and (b[1].active or b[2].active or b[3].active) then
  89.         b[4].active = true
  90.         drawButton(b, 4)
  91.     else
  92.         b[4].active = false
  93.         drawButton(b, 4)
  94.     end
  95. end
  96.  
  97. local function btnBetType(bType)
  98.     for i=1, 3 do
  99.         if i == bType then
  100.             b[i].active = true
  101.             drawButton(b, i)
  102.         else
  103.             b[i].active = false
  104.             drawButton(b, i)
  105.         end
  106.     end
  107.     checkSpinButton()
  108. end
  109.  
  110. local function btnBet(amount)
  111.     if amount == 0 then
  112.         currentGame.bet = 0
  113.     end
  114.     currentGame.bet = currentGame.bet + amount
  115.     if currentGame.bet < 0 then
  116.         currentGame.bet = 0
  117.     end
  118.     if currentGame.bet > currentGame.bal then
  119.         currentGame.bet = currentGame.bal
  120.     end
  121.     drawBet()
  122.     checkSpinButton()
  123. end
  124.  
  125. local function buttonControl(x, y)
  126.     for i=1, #b do
  127.         if y == b[i].y and x >= b[i].x and x < b[i].x + #b[i].text then
  128.             if i == 1 then btnBetType(1); return
  129.             elseif i == 2 then btnBetType(2); return
  130.             elseif i == 3 then btnBetType(3); return
  131.             elseif i == 4 and b[4].active and currentGame.bet > 0 then btnBetType(0); return
  132.             elseif i == 5 then btnBet(0); return
  133.             elseif i == 6 then btnBet(currentGame.base); return
  134.             elseif i == 7 then btnBet(currentGame.base * 16); return
  135.             elseif i == 8 then btnBet(currentGame.base * 64); return
  136.             elseif i == 9 then btnBet(0-currentGame.base); return
  137.             elseif i == 10 then btnBet(0-currentGame.base * 16); return
  138.             elseif i == 11 then btnBet(0-currentGame.base * 64); return
  139.             elseif i == 12 then return true
  140.             end
  141.         end
  142.     end
  143. end
  144.  
  145. local function drawXY(x, y)
  146.     t.setBackgroundColor(128)
  147.     t.setTextColor(1)
  148.     t.setCursorPos(1, 1)
  149.     t.write(x)
  150.     t.setCursorPos(1, 2)
  151.     t.write(y)
  152. end
  153.  
  154. local function main()
  155.     while true do
  156.         local e, _, x, y, _ = os.pullEvent()
  157.         if e == "monitor_touch" then
  158.             drawXY(x, y)
  159.             if buttonControl(x, y) then
  160.                 return
  161.             end
  162.         end
  163.        
  164.     end
  165. end
  166.  
  167. local function gameSetup(name)
  168.     setupCurrentGame(name, 5, 10000, 0, 1)
  169.     drawBackground()
  170.     drawStatic()
  171.     drawButtons(b)
  172.     drawBet()
  173.     drawBal()
  174.     t.setCursorPos(1, 1)
  175.     t.write(#b[10].text)
  176.     if main() then
  177.         return
  178.     end
  179.     os.pullEvent("player_off")
  180. end
  181.  
  182. gameSetup('test')
Advertisement
Add Comment
Please, Sign In to add comment