paramus

SlotMachine

Aug 20th, 2025 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | Gaming | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local slotPos = "bottom"
  3. local prizePos = "left"
  4. local bigPrizeCount = 10
  5. local prizeCount = 5
  6.  
  7. function FixValue(val, color, color7)
  8.     term.setTextColour(color)
  9.     if (val == 0) then
  10.         return "\7"
  11.     elseif (val == 8) then
  12.         term.setTextColour(color7)
  13.         return 7
  14.     end
  15.    
  16.     if (val == 1) then
  17.         term.setTextColour(color7)
  18.         return 7
  19.     elseif (val == 2) then
  20.         return "\2"
  21.     elseif (val == 3) then
  22.         return "\3"
  23.     elseif (val == 4) then
  24.         return "\4"
  25.     elseif (val == 5) then
  26.         return "\5"
  27.     elseif (val == 6) then
  28.         return "\6"
  29.     elseif (val == 7) then
  30.         return "\7"
  31.     end
  32. end
  33.  
  34. function Draw(a,b,c)
  35.     term.clear()
  36.     term.setTextColour(colors.yellow)
  37.     term.setCursorPos(1,1)
  38.     write("\127" .. "\127" .. "\127" .. "\127" .. "\127" .. "\127" .. "\127")
  39.    
  40.     for i=2,4 do
  41.         term.setCursorPos(1,i)
  42.         write("\127")
  43.         term.setCursorPos(7,i)
  44.         write("\127")
  45.     end
  46.    
  47.     term.setTextColour(colors.lightGray)
  48.     term.setCursorPos(2,2)
  49.     write(FixValue(a-1, colors.lightGray, colors.gray))
  50.     term.setCursorPos(4,2)
  51.     write(FixValue(b-1, colors.lightGray, colors.gray))
  52.     term.setCursorPos(6,2)
  53.     write(FixValue(c-1, colors.lightGray, colors.gray))
  54.    
  55.     -- Actual values
  56.     term.setTextColour(colors.white)
  57.     term.setCursorPos(2,3)
  58.     write(FixValue(a, colors.white, colors.red))
  59.     term.setCursorPos(4,3)
  60.     write(FixValue(b, colors.white, colors.red))
  61.     term.setCursorPos(6,3)
  62.     write(FixValue(c, colors.white, colors.red))
  63.    
  64.     term.setTextColour(colors.lightGray)
  65.     term.setCursorPos(2,4)
  66.     write(FixValue(a+1, colors.lightGray, colors.gray))
  67.     term.setCursorPos(4,4)
  68.     write(FixValue(b+1, colors.lightGray, colors.gray))
  69.     term.setCursorPos(6,4)
  70.     write(FixValue(c+1, colors.lightGray, colors.gray))
  71.    
  72.     term.setTextColour(colors.yellow)
  73.     term.setCursorPos(1,5)
  74.     write("\127" .. "\127" .. "\127" .. "\127" .. "\127" .. "\127" .. "\127")
  75. end
  76.  
  77. function DispensePrize(qty)
  78.     --TODO: Test
  79.     for i=1,qty do
  80.         rs.setAnalogOutput(prizePos, 1)
  81.         sleep(0.2)
  82.         rs.setAnalogOutput(prizePos, 0)
  83.         sleep(0.2)
  84.     end
  85. end
  86.  
  87. function Game()
  88.     local a = math.random(1,3)
  89.     local b = math.random(1,3)
  90.     local c = math.random(1,3)
  91.  
  92.     --Visual effect
  93.     for i=1,35 do
  94.         sleep(0.2)
  95.         Draw(math.random(1,7),math.random(1,7),math.random(1,7))
  96.     end
  97.    
  98.     Draw(a,b,c)
  99.    
  100.     --Big prize
  101.     if (a == 1 and b == 1 and c == 1) then
  102.         sleep(1)
  103.         --term.clear()
  104.         term.setTextColour(colors.green)
  105.         term.setCursorPos(3,2)
  106.         write("BIG")
  107.         term.setCursorPos(1,3)
  108.         write("WINNER!")
  109.         DispensePrize(bigPrizeCount)
  110.         return
  111.     end
  112.    
  113.     -- Normal prize
  114.     if (a ==  b and b == c) then
  115.         sleep(1)
  116.         --term.clear()
  117.         term.setTextColour(colors.green)
  118.         term.setCursorPos(1,3)
  119.         write("WINNER!")
  120.         DispensePrize(prizeCount)
  121.     else
  122.         sleep(1)
  123.         --term.clear()
  124.         term.setTextColour(colors.red)
  125.         term.setCursorPos(3,2)
  126.         write("TOO")
  127.         term.setCursorPos(3,3)
  128.         write("BAD")
  129.     end
  130. end
  131.  
  132. function ScreenSaver()
  133.     Draw(math.random(1,3),math.random(1,3),math.random(1,3))
  134. end
  135.  
  136. rs.setAnalogOutput(slotPos, 0)
  137. term.redirect(monitor)
  138. while(true) do
  139.     sleep(1)
  140.     ScreenSaver()
  141.     os.pullEvent("redstone")
  142.     rs.setAnalogOutput(slotPos, 15)
  143.     Game()
  144.     sleep(0.1)
  145.     rs.setAnalogOutput(slotPos, 0)
  146.     sleep(0.1)
  147. end
Advertisement
Add Comment
Please, Sign In to add comment