Advertisement
oGoOgO

game_Minesweeper.lua

Jun 3rd, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local event = require("event")
  4. local term = require("term")
  5. local unicode = require("unicode")
  6. local casino = require("casino")
  7.  
  8. local bomb_count = 5 -- 6 мин = 40% на победу, 5 мин 47% на победу
  9. local bets = { 1, 2, 3, 4, 5, 10, 16 }
  10. local symb = string.rep(unicode.char(0xE0BF), 2)
  11. local fields = {}
  12. local game = false
  13. local bet = 1
  14. local attempts = 0
  15.  
  16. local field_types = {
  17. ["clear"] = 0x98df94,
  18. ["close"] = 0xd0d0d0,
  19. ["bomb"] = 0xff0000,
  20. ["close_bomb"] = 0xff0000,
  21. }
  22.  
  23. local function getBombPos(x)
  24. return 5 + ((x - 1) % 6) * 12, 3 + math.floor((x - 1) / 6) * 6
  25. end
  26.  
  27. local function drawField(x, f_type)
  28. gpu.setBackground(field_types[f_type])
  29. local pos_x, pos_y = getBombPos(x)
  30. gpu.fill(pos_x, pos_y, 10, 5, " ")
  31. if (f_type == "bomb") then
  32. gpu.setForeground(0)
  33. gpu.set(pos_x, pos_y + 0, symb .. " " .. symb)
  34. gpu.set(pos_x, pos_y + 1, " \\ / ")
  35. gpu.set(pos_x, pos_y + 2, " " .. symb .. " ")
  36. gpu.set(pos_x, pos_y + 3, " / \\ ")
  37. gpu.set(pos_x, pos_y + 4, symb .. " " .. symb)
  38. end
  39. end
  40.  
  41. local animations = {
  42. ["load"] = function()
  43. for i = 1, 24 do
  44. drawField(i, "clear")
  45. os.sleep()
  46. drawField(i, "close")
  47. end
  48. end,
  49.  
  50. ["reveal"] = function()
  51. for i = 0, 3 do
  52. for j = 1, 6 do
  53. drawField(j + i * 6, "clear")
  54. end
  55. os.sleep(0.1)
  56. for j = 1, 6 do
  57. if (fields[j + i * 6] == "close_bomb") then
  58. drawField(j + i * 6, "bomb")
  59. else
  60. drawField(j + i * 6, "close")
  61. end
  62. end
  63. end
  64. os.sleep(1)
  65. for i = 0, 3 do
  66. for j = 1, 6 do
  67. drawField(j + i * 6, "clear")
  68. end
  69. os.sleep(0.1)
  70. for j = 1, 6 do
  71. drawField(j + i * 6, "close")
  72. end
  73. end
  74. end,
  75. ["error"] = function()
  76. for i = 1, 2 do
  77. gpu.setBackground(0xff0000)
  78. gpu.setForeground(0xffffff)
  79. gpu.fill(58, 29, 17, 5, " ")
  80. gpu.set(61, 31, "Начать игру")
  81. os.sleep(0.1)
  82. gpu.setBackground(0x90ef7e)
  83. gpu.setForeground(0)
  84. gpu.fill(58, 29, 17, 5, " ")
  85. gpu.set(61, 31, "Начать игру")
  86. os.sleep(0.1)
  87. end
  88. end
  89. }
  90.  
  91. local function generateFields()
  92. fields = {}
  93. for i = 1, 24 do
  94. fields[i] = "close"
  95. end
  96. local i = 0
  97. while i < bomb_count
  98. do
  99. local rand = math.random(1, 24)
  100. if (fields[rand] ~= "close_bomb") then
  101. fields[rand] = "close_bomb"
  102. i = i + 1
  103. end
  104. end
  105. end
  106.  
  107. local function getBombId(left, top)
  108. if (((left - 3) % 12) == 0) or (((left - 4) % 12) == 0) or (((top - 2) % 6) == 0) then
  109. return 0
  110. end
  111. return (math.floor((top - 3) / 6) * 6) + math.floor((left + 7) / 12)
  112. end
  113.  
  114. local function endGame()
  115. os.sleep(0.7)
  116. animations.reveal()
  117. gpu.setForeground(0xFFFFFF)
  118. gpu.setBackground(0x990000)
  119. gpu.fill(58, 35, 17, 3, " ")
  120. gpu.set(64, 36, "Выход")
  121. gpu.setBackground(0x90ef7e)
  122. gpu.setForeground(0)
  123. gpu.fill(58, 29, 17, 5, " ")
  124. gpu.set(61, 31, "Начать игру")
  125. game = false
  126. casino.gameIsOver()
  127. end
  128.  
  129. local function drawBets()
  130. gpu.setForeground(0)
  131. for i = 0, #bets - 1 do
  132. gpu.setBackground(i == bet - 1 and 0x90ef7e or 0xd0d0d0)
  133. gpu.fill(5 + i * 7, 37, 5, 1, " ")
  134. gpu.set(7 + i * 7, 37, tostring(bets[i + 1]))
  135. end
  136. end
  137.  
  138. local function handleFieldClick(top, left)
  139. local id = getBombId(left, top)
  140. if (id > 0) then
  141. if (fields[id] == "close") then
  142. drawField(id, "clear")
  143. fields[id] = "clear"
  144. attempts = attempts - 1
  145. end
  146. if (fields[id] == "close_bomb") then
  147. drawField(id, "bomb")
  148. endGame()
  149. return
  150. end
  151. end
  152. if (attempts == 0) then
  153. casino.reward(bets[bet] * 2)
  154. endGame()
  155. return
  156. end
  157. end
  158.  
  159. gpu.setResolution(78, 39)
  160. gpu.setBackground(0xe0e0e0)
  161. term.clear()
  162. gpu.setBackground(0xffffff)
  163. gpu.fill(3, 2, 74, 37, " ")
  164. gpu.setForeground(0x00a000)
  165. gpu.set(4, 29, "Правила игры и награды:")
  166. gpu.set(4, 35, "Ставка:")
  167. gpu.setForeground(0x000000)
  168. gpu.set(4, 30, "Начинайте игру и ищите поля без мин. Если 3 раза")
  169. gpu.set(4, 31, "подряд не наткнулись на поле с миной, то вы")
  170. gpu.set(4, 32, "победили. Всего в игре 24 поля, из которых 5 ")
  171. gpu.set(4, 33, "заминированы. Победа в игре удваивает ставку. ")
  172. gpu.setBackground(0xe0e0e0)
  173. gpu.fill(1, 27, 76, 1, " ")
  174. gpu.fill(54, 27, 2, 12, " ")
  175. gpu.setForeground(0xFFFFFF)
  176. gpu.setBackground(0x990000)
  177. gpu.fill(58, 35, 17, 3, " ")
  178. gpu.set(64, 36, "Выход")
  179. gpu.setBackground(0x90ef7e)
  180. gpu.setForeground(0)
  181. gpu.fill(58, 29, 17, 5, " ")
  182. gpu.set(61, 31, "Начать игру")
  183. drawBets()
  184. animations.load()
  185. while true do
  186. local _, _, left, top = event.pull("touch")
  187.  
  188. -- start game button
  189. if not game and left >= 58 and left <= 75 and top >= 29 and top <= 33 then
  190. local payed, reason = casino.takeMoney(bets[bet])
  191. if payed then
  192. generateFields()
  193. gpu.setBackground(0xffa500)
  194. gpu.fill(58, 29, 17, 5, " ")
  195. gpu.set(62, 31, "Идёт игра")
  196. gpu.setForeground(0xFFFFFF)
  197. gpu.setBackground(0x613C3C)
  198. gpu.fill(58, 35, 17, 3, " ")
  199. gpu.set(64, 36, "Выход")
  200. attempts = 3
  201. game = true
  202. else
  203. animations.error()
  204. end
  205. end
  206.  
  207. -- exit button
  208. if not game and left >= 58 and left <= 75 and top >= 35 and top <= 37 then
  209. error("Exit by request")
  210. end
  211.  
  212. -- game fields
  213. if game and left >= 5 and left <= 74 and top >= 2 and top <= 25 then
  214. handleFieldClick(top, left)
  215. end
  216.  
  217. -- bet buttons
  218. if not game and top == 37 and left >= 5 and left <= 51 then
  219. if (left - 5) % 7 < 5 then
  220. bet = math.floor((left - 5) / 7) + 1
  221. drawBets()
  222. end
  223. end
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement