FERROUSSAGE

guess the number

Sep 10th, 2021 (edited)
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.69 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local chest = component.chest
  4. local interface = component.me_interface
  5. local unicode = require("unicode")
  6. local gpu = component.gpu
  7. local computer = require('computer')
  8. local terminal = require("term")
  9. local cb = component.chat_box
  10.  
  11. local setTextColor = gpu.setForeground
  12.  
  13. -- local timeRelog = 10
  14. local nickPlayer = ""
  15. -- local eventTimer
  16. local digit = ""
  17. local stateMoney = false
  18.  
  19. cb.setName("§cУгадай§7Число§7")
  20.  
  21. local colors = {
  22.     main = 0xFF5555,
  23.     white = 0xFFFFFF
  24. }
  25.  
  26. local digitsOfStr = {
  27.     "один",
  28.     "два",
  29.     "три",
  30.     "четыре",
  31.     "пять"
  32.     -- "шесть",
  33.     -- "семь",
  34.     -- "восемь",
  35.     -- "девять",
  36.     -- "десять"
  37. }
  38.  
  39. local maxW, maxH = 90, 26
  40. gpu.setResolution(maxW, maxH)
  41.  
  42. function drawBox(x, y, width, height, colorBorder, text, colorText, align)
  43.     setTextColor(colorBorder)
  44.  
  45.     gpu.fill(x, y, 1, height, "│")
  46.     gpu.fill(x + width - 1 , y, 1, height, "│")
  47.     gpu.set(x, y, "┌" .. string.rep('─', width - 2) .. "┐")
  48.     gpu.set(x, y + height - 1,"└" .. string.rep("─", width - 2) .. "┘")
  49.  
  50.     if text then
  51.         setTextColor(colorText)
  52.         gpu.set(x + align, y + 1, text)
  53.     end
  54. end
  55.  
  56.  
  57. function drawScreenMain()
  58.     terminal.clear()
  59.  
  60.     drawBox(1, 1, maxW, maxH, colors.main, _, _, _)
  61.  
  62.     setTextColor(colors.white)
  63.    
  64.     drawBox(25, 1, 40, 3, colors.white, "Привет, удачи и приятной игры!", colors.white, 5)
  65.  
  66.     drawBox(3, 5, 40, 18, colors.white, _, _, _)
  67.  
  68.     gpu.set(17, 5, "| Пояснения |")
  69.     gpu.set(5, 8, "Суть игры проста, тебе нужно")
  70.     gpu.set(5, 10, "угадать число от 1 до 5")
  71.     gpu.set(5, 12, "Ставка: деньги х1")
  72.     gpu.set(5, 14, "Приз: деньги х5")
  73.     gpu.set(5, 17, "Сейчас в банке: " .. interface.getStackInSlot(1).qty .. " банкнот")
  74.     gpu.set(5, 18, "Тыкай по кнопке и вперед!")
  75.     gpu.set(5, 20, "В случае проблем: /tell FERROUS")
  76.  
  77.     drawBox(47, 5, 41, 18, colors.white, _, _, _)
  78.  
  79.     gpu.set(62, 5, "| Правила |")
  80.     gpu.set(49, 8, "1. Формат ответа: один, два, три и тд")
  81.     gpu.set(49, 10, "2. Писать ответ в локал. чат")
  82.     -- gpu.set(49, 12, "3. Время на игру - 40 секунд")
  83.     gpu.set(49, 12, "3. Приходить еще и звать друзей:)")
  84.  
  85.     drawBox(35, 24, 21, 3, colors.main, "Начать игру", colors.white, 4)
  86. end
  87.  
  88. function drawScreenGame()
  89.     drawBox(1, 1, maxW, maxH, colors.main, _, _, _)
  90.     drawBox(30, 1, 32, 3, colors.white, "Игрок: " .. nickPlayer, colors.white, 3)
  91.     drawBox(30, maxH / 2, 30, 3, colors.main, "Бот загадал число", colors.white, 7)
  92. end
  93.  
  94. function randomDigit()
  95.     return digitsOfStr[math.random(1, 5)]
  96. end
  97.  
  98. function resetUsers()
  99.     local users = { computer.users() }
  100.     for i = 1, #users do
  101.         computer.removeUser(users[i])
  102.     end
  103. end
  104.  
  105. function gameOver(digit)
  106.     -- event.cancel(eventTimer)
  107.     resetUsers()
  108.     gpu.fill(30, maxH / 2, 35, 3, " ")
  109.  
  110.     drawBox(30, maxH / 2, 35, 3, colors.main, "Бот загадал число - " .. digit, colors.white, 5)
  111.     os.sleep(2)
  112.     -- timeRelog = 10
  113.     stateMoney = false
  114.     drawScreenMain()
  115. end
  116.  
  117. -- function printTime(digit)
  118. --     timeRelog = timeRelog - 1
  119. --     if timeRelog <= 0 then
  120. --         event.cancel(eventTimer)
  121. --         resetUsers()
  122. --         gpu.fill(30, maxH / 2, 35, 3, " ")
  123. --         drawBox(30, maxH / 2, 35, 3, colors.main, "Время вышло!", colors.white, 12)
  124. --         os.sleep(2)
  125. --         timeRelog = 10
  126. --         stateMoney = false
  127. --         drawScreenMain()
  128. --     end
  129. --     if nickPlayer ~= nil then
  130. --         gpu.set(50, 2, "Время: " .. timeRelog .. "c.")
  131. --     end
  132. -- end
  133.  
  134. function findElement(array, str)
  135.     local state = false
  136.     for k, v in pairs(array) do
  137.         if v == str then
  138.             state = true
  139.         end
  140.     end
  141.     return state
  142. end
  143.  
  144. drawScreenMain()
  145.  
  146. while true do
  147.     local e, _, w, h, _, name = event.pull(1, "touch")
  148.     if e == "touch" then
  149.         nickPlayer = name
  150.         computer.addUser(nickPlayer)
  151.         if w >= 35 and w <= 55 and h >= 24 and h <= 26 then  
  152.             for i = 1, chest.getInventorySize() do
  153.                 local item = chest.getStackInSlot(i)
  154.                 if item and item.name == "npcMoney" and item.qty >= 1 then
  155.                     if interface.getStackInSlot(1) and interface.getStackInSlot(1).qty >= 5 then
  156.                         chest.pushItemIntoSlot("DOWN", i, 1, 1)
  157.  
  158.                         terminal.clear()
  159.  
  160.                         drawScreenGame()
  161.                         -- eventTimer = event.timer(1, printTime, math.huge)
  162.                         digit = randomDigit()
  163.  
  164.                         while true do
  165.                             local _, _, nick, msg = event.pull(1, "chat_message")
  166.                             if msg ~= nil and nick ~= nil then
  167.                                 if nick == nickPlayer then
  168.                                     if findElement(digitsOfStr, msg) then
  169.                                         if msg == digit then
  170.                                             gpu.fill(30, maxH / 2, 30, 3,' ')
  171.                                             drawBox(30, maxH / 2, 30, 3, colors.main, "Ты выиграл!", colors.white, 9)
  172.                                             cb.say("§a" .. nickPlayer .. " выиграл 5 эм")
  173.                                             interface.pushItemIntoSlot("UP", 1, 5, 1)
  174.                                             os.sleep(1)
  175.                                         else
  176.                                             gpu.fill(30, maxH / 2, 30, 3,' ')
  177.                                             drawBox(30, maxH / 2, 30, 3, colors.main, "Ты проиграл!", colors.white, 9)
  178.                                             os.sleep(1)
  179.                                         end
  180.                                         gameOver(digit)
  181.                                         break
  182.                                     end
  183.                                 end
  184.                             end
  185.                         end
  186.                     end
  187.                 else
  188.                     stateMoney = true
  189.                 end
  190.             end
  191.         end
  192.         if stateMoney then
  193.             stateMoney = false
  194.             cb.say("§a" .. nickPlayer .. ", для начала надо внести ставку (деньги х1)")
  195.             resetUsers()
  196.         end
  197.     end
  198. end
Add Comment
Please, Sign In to add comment