oGoOgO

game_Chests.lua

Jun 3rd, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. local buffer = require("doubleBuffering")
  2. local casino = require("casino")
  3. local event = require("event")
  4.  
  5. local BET_VALUES = {1, 2, 3, 5, 10, 25}
  6.  
  7. local bet = 1
  8. local game = false
  9. local chests = {[0] = 0, 0, 0, 0, 0, 0, 0, 0, 0}
  10.  
  11. local consoleLines = {}
  12. for i = 1, 14 do consoleLines[i] = "" end
  13.  
  14. local function drawRightMenu()
  15. buffer.drawRectangle(67, 2, 21, 15, 0, 0, " ")
  16. buffer.drawText(67, 2, 0xAAAAAA, "Вывод:")
  17. for i = 1, #consoleLines do
  18. buffer.drawText(67, 2 + i, (15 - #consoleLines + i) * 0x111111,
  19. consoleLines[i])
  20. end
  21. buffer.drawRectangle(67, 19, 21, 6, 0xFFFFFF, 0, " ")
  22. for i = 1, 6 do
  23. if bet == 7 - i then
  24. buffer.drawRectangle(67, 18 + i, 21, 1, 0xFF8A00, 0, " ")
  25. end
  26. buffer.drawText(77, 18 + i, 0, tostring(BET_VALUES[7 - i]))
  27. end
  28. buffer.drawChanges()
  29. end
  30.  
  31. local function message(msg)
  32. table.remove(consoleLines, 1)
  33. table.insert(consoleLines, tostring(msg))
  34. drawRightMenu()
  35. end
  36.  
  37. local function drawChest(id, isOpen)
  38. local x, y = id % 3 * 20 + 5, math.floor(id / 3) * 10 + 3
  39. buffer.drawRectangle(x, y, 18, 9, 0x675233, 0, " ")
  40. buffer.drawRectangle(x + 2, y + 1, 14, 1, 0xA8772C, 0, " ")
  41. buffer.drawRectangle(x + 2, y + 3, 14, 5, 0xA8772C, 0, " ")
  42. buffer.drawRectangle(x + 8, y + 2, 2, 2, 0x707070, 0, " ")
  43. if isOpen then buffer.drawRectangle(x, y, 18, 2, 0xFFFFFF, 0, " ") end
  44. end
  45.  
  46. local function drawChestContent(id)
  47. local content = tostring(chests[id])
  48. local x, y = id % 3 * 20 + 14 - math.floor(string.len(content) / 2),
  49. math.floor(id / 3) * 10 + 8
  50. buffer.drawText(x, y, 0xFFFFFF, content)
  51. end
  52.  
  53. local function getChestId(x, y)
  54. local x, y = x - 5, y - 3
  55. if x % 20 ~= 18 and x % 20 ~= 19 and y % 10 ~= 9 then
  56. return math.floor(x / 20) + (math.floor(y / 10) * 3)
  57. end
  58. end
  59.  
  60. local function gameEnd(chest)
  61. drawChest(chest, true)
  62. for i = 0, 8 do drawChestContent(i) end
  63. buffer.drawChanges()
  64. local reward = chests[chest]
  65. message("Вы выиграли " .. reward)
  66. casino.reward(reward)
  67. casino.gameIsOver()
  68. game = false
  69. end
  70.  
  71. local function gameStart()
  72. game = true
  73. message("Началась игра за " .. BET_VALUES[bet])
  74. -- В одном из сундуков может быть малая вероятность на джекпот и большая вероятность на 0 (для баланса)
  75. local jackpotChest = math.random(0, #chests)
  76.  
  77. for i = 0, #chests do
  78. drawChest(i)
  79. if jackpotChest == i then
  80. if math.random(1, 11) == 1 then
  81. chests[i] = BET_VALUES[bet] * 10
  82. else
  83. chests[i] = 0
  84. end
  85. else
  86. -- В обычных сундуках от 0 до Ставка х2
  87. chests[i] = math.random(0, BET_VALUES[bet] * 2)
  88. end
  89. end
  90. buffer.drawChanges()
  91. end
  92.  
  93. local function drawStatic()
  94. buffer.setResolution(89, 33)
  95. buffer.clear(0xBFBFBF)
  96. buffer.drawRectangle(3, 2, 85, 31, 0xFFFFFF, 0, " ")
  97. buffer.drawRectangle(65, 2, 2, 31, 0xBFBFBF, 0, " ")
  98. buffer.drawRectangle(67, 17, 22, 1, 0xBFBFBF, 0, " ")
  99. buffer.drawRectangle(67, 25, 22, 1, 0xBFBFBF, 0, " ")
  100. buffer.drawRectangle(67, 29, 22, 1, 0xBFBFBF, 0, " ")
  101. buffer.drawRectangle(67, 26, 22, 3, 0xc7ffc6, 0, " ")
  102. buffer.drawRectangle(67, 30, 22, 3, 0xffc6c6, 0, " ")
  103. buffer.drawText(74, 18, 0, 'Ставка')
  104. buffer.drawText(75, 27, 0, 'Играть')
  105. buffer.drawText(75, 31, 0, 'Выход')
  106. buffer.drawChanges()
  107. end
  108.  
  109. drawStatic()
  110. drawRightMenu()
  111. buffer.drawChanges()
  112.  
  113. for id = 0, 8 do drawChest(id) end
  114. buffer.drawChanges()
  115.  
  116. while true do
  117. local _, _, x, y = event.pull("touch")
  118.  
  119. if x >= 5 and x <= 62 and y >= 3 and y <= 31 then
  120. if not game then
  121. message("Начните игру")
  122. else
  123. local chest = getChestId(x, y)
  124. if chest ~= nil then gameEnd(chest) end
  125. end
  126. end
  127.  
  128. -- Right menu buttons
  129. if x >= 67 and x <= 87 then
  130. if game then
  131. message("Выберите сундук")
  132. else
  133. -- Bet buttons
  134. if y >= 19 and y <= 24 then
  135. local new_bet = 25 - y
  136. if new_bet ~= bet then
  137. bet = new_bet
  138. drawRightMenu()
  139. end
  140. end
  141. -- Play button
  142. if y >= 26 and y <= 28 then
  143. local payed, reason = casino.takeMoney(BET_VALUES[bet])
  144. if payed then
  145. gameStart()
  146. else
  147. message(reason)
  148. end
  149. end
  150. -- Exit button
  151. if y >= 30 and y <= 32 then error("Exit by request") end
  152. end
  153. end
  154. end
Add Comment
Please, Sign In to add comment