Advertisement
oGoOgO

game_Labirynth.lua

Jun 3rd, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local gpu = component.gpu
  5. local unicode = require("unicode")
  6. local io = require('io')
  7. local dragging, game, player, lastx, lasty, ending, cooldown, flag = false, false, "", 0, 0, 0, 0, true
  8. local map, start, finish, currentPos = {}, { 1, 17 }, { 35, 17 }, { 1, 17 }
  9.  
  10. local casino = require("casino")
  11.  
  12. local file = io.open('snake-cooldown', 'r')
  13. if (file) then
  14. cooldown = tonumber(file:read("*a"))
  15. file:close();
  16. end
  17.  
  18. function startGenerate()
  19. for i = 1, 35 do
  20. map[i] = {}
  21. for j = 1, 35 do
  22. if (i % 2 ~= 0 and j % 2 ~= 0) then
  23. map[i][j] = 1
  24. else
  25. map[i][j] = 0
  26. end
  27. end
  28. end
  29. end
  30.  
  31. function generateMap()
  32. flag = true
  33. startGenerate()
  34. goToRandPlace(finish[1], finish[2])
  35. end
  36. function isSize(size, x, y)
  37. if (size == 1) then
  38. if (x > 1) and map[x - 2][y] == 1 then
  39. map[x - 1][y] = 2
  40. return true
  41. end
  42. elseif (size == 2) then
  43. if (y > 1) and map[x][y - 2] == 1 then
  44. map[x][y - 1] = 2
  45. return true
  46. end
  47. elseif (size == 3) then
  48. if (x < 35) and map[x + 2][y] == 1 then
  49. map[x + 1][y] = 2
  50. return true
  51. end
  52. elseif (size == 4) then
  53. if (y < 35) and map[x][y + 2] == 1 then
  54. map[x][y + 1] = 2
  55. return true
  56. end
  57. end
  58. return false
  59. end
  60. function nextSize(size, rand)
  61. if (rand == 1) then
  62. if (size < 4) then
  63. return size + 1
  64. else
  65. return 1
  66. end
  67. else
  68. if (size > 1) then
  69. return size - 1
  70. else
  71. return 4
  72. end
  73. end
  74. end
  75. function goToRandPlaceTemp(x, y, count)
  76. map[x][y] = 2
  77. if (count < 1) then
  78. return
  79. end
  80. local size = math.random(1, 4)
  81. local rand = math.random(1, 2)
  82. for i = 1, 4 do
  83. if (isSize(size, x, y)) then
  84. if (size == 1) then
  85. goToRandPlaceTemp(x - 2, y, count - 1)
  86. elseif (size == 2) then
  87. goToRandPlaceTemp(x, y - 2, count - 1)
  88. elseif (size == 3) then
  89. goToRandPlaceTemp(x + 2, y, count - 1)
  90. elseif (size == 4) then
  91. goToRandPlaceTemp(x, y + 2, count - 1)
  92. end
  93. else
  94. size = nextSize(size, rand)
  95. end
  96. end
  97. end
  98. function goToRandPlace(x, y)
  99. if (flag) then
  100. map[x][y] = 3
  101. else
  102. map[x][y] = 2
  103. end
  104. if (x == start[1] and y == start[2]) then
  105. local count = math.random(5, 15)
  106. local times = 5
  107. while times > 0 do
  108. local xRand = math.random(1, 35)
  109. local yRand = math.random(1, 35)
  110.  
  111. if (map[xRand][yRand] == 3) then
  112. times = times - 1
  113. goToRandPlaceTemp(xRand, yRand, count)
  114. end
  115. end
  116. else
  117. local size = math.random(1, 4)
  118. local rand = math.random(1, 2)
  119. for i = 1, 4 do
  120. if (isSize(size, x, y)) then
  121. if (size == 1) then
  122. goToRandPlace(x - 2, y)
  123. elseif (size == 2) then
  124. goToRandPlace(x, y - 2)
  125. elseif (size == 3) then
  126. goToRandPlace(x + 2, y)
  127. elseif (size == 4) then
  128. goToRandPlace(x, y + 2)
  129. end
  130. else
  131. size = nextSize(size, rand)
  132. end
  133. end
  134. end
  135. end
  136. function setGame(status)
  137. game = status
  138. gpu.setForeground(0x000000)
  139. if (game) then
  140. gpu.setBackground(0xd69b8b)
  141. gpu.fill(81, 33, 34, 5, " ")
  142. gpu.set(98 - math.floor((12 + string.len(player)) / 2), 35, "Идёт игра у " .. player)
  143. Log(player .. " начал игру")
  144. else
  145. gpu.setBackground(0x90ee90)
  146. gpu.fill(81, 33, 34, 5, " ")
  147. gpu.set(95, 35, "Играть")
  148. end
  149. gpu.setBackground(0x990000)
  150. gpu.setForeground(0xFFFFFF)
  151. gpu.fill(81, 27, 34, 5, " ")
  152. gpu.set(95, 29, "Выход")
  153. gpu.setForeground(0x000000)
  154. end
  155. local loglist = { "", "", "", "", "", "", "", "", "", "", "" }
  156. function Log(message)
  157. loglist[10] = unicode.sub(message .. " ", 1, 34)
  158. gpu.setBackground(0xdddddd)
  159. for i = 1, 9 do
  160. loglist[i] = loglist[i + 1]
  161. gpu.set(81, 16 + i, loglist[i])
  162. end
  163. end
  164.  
  165. function drag(left, top)
  166. local x, y = math.floor((left - 3) / 2), (top - 2)
  167. if (x < 1) or (x > 35) or (y < 1) or (y > 35) then
  168. lose("ушёл за поле")
  169. return
  170. end
  171. if (map[x][y] == 0) then
  172. lose("задел край")
  173. end
  174. if (x == finish[1]) and (y == finish[2]) then
  175. win()
  176. end
  177. drawPoint(x, y, 0xd1a926)
  178. map[x][y] = 2
  179. end
  180. function drawPoint(x, y, color)
  181. gpu.setBackground(color)
  182. gpu.fill(3 + x * 2, 2 + y, 2, 1, " ")
  183. if (game) then
  184. if math.abs(x - lastx) > 2 then
  185. lose("поторопился")
  186. end
  187. if math.abs(y - lasty) > 2 then
  188. lose("поторопился")
  189. end
  190. end
  191.  
  192. lastx = x
  193. lasty = y
  194. end
  195. function drawField()
  196. generateMap()
  197. gpu.setBackground(0x03511a)
  198. gpu.fill(5, 3, 70, 35, " ")
  199. a = emptyarray()
  200. currentPos = { 1, 17 }
  201. for i = 1, 35 do
  202. for j = 1, 35 do
  203. if map[i][j] == 3 then
  204. map[i][j] = 2
  205. end
  206. end
  207. end
  208. map[start[1]][start[2]] = 3
  209. map[finish[1]][finish[2]] = 4
  210. for x = 1, 35 do
  211. for y = 1, 35 do
  212. if (map[x][y] == 2 or map[x][y] == 1) then
  213. drawPoint(x, y, 0x25d258)
  214. end
  215. if (map[x][y] == 3) then
  216. drawPoint(x, y, 0xd1a926)
  217. end
  218. if (map[x][y] == 4) then
  219. drawPoint(x, y, 0xc2ff7d)
  220. end
  221. end
  222. end
  223. end
  224. function lose(message)
  225. dragging = false
  226. setGame(false)
  227. Log(player .. " " .. message)
  228. end
  229. function win()
  230. dragging = false
  231. setGame(false)
  232. casino.reward(1)
  233. Log(player .. " победил. : ")
  234. end
  235.  
  236. gpu.setResolution(118, 39)
  237. gpu.setBackground(0xe0e0e0)
  238. term.clear()
  239. gpu.setBackground(0xffffff)
  240. gpu.fill(3, 2, 74, 37, " ")
  241. gpu.fill(79, 2, 38, 37, " ")
  242. gpu.setBackground(0xdddddd)
  243. gpu.fill(81, 17, 34, 9, " ")
  244. gpu.setBackground(0xffffff)
  245. gpu.setForeground(0x0000ff)
  246. gpu.set(80, 2, "Правила игры:")
  247. gpu.set(80, 11, "Цена и награда:")
  248. gpu.setForeground(0xaaaaaa)
  249. gpu.set(80, 3, "Необходимо добраться с левой точки")
  250. gpu.set(80, 4, "до правой через лабиринт. Время")
  251. gpu.set(80, 5, "ограничено 60-ю секундами.")
  252. gpu.set(80, 6, " - Нельзя отпускать мышь")
  253. gpu.set(80, 7, ' - Нельзя "телепортироваться"')
  254. gpu.set(80, 8, " - Нельзя задевать стены")
  255. gpu.set(80, 9, " - Нельзя выходить за поле")
  256. gpu.set(80, 12, "Игра абсолютно БЕСПЛАТНАЯ.")
  257. gpu.set(80, 13, "В случае успеха, награда 1 эм.")
  258. gpu.set(80, 14, "Независимо от исхода игры, кулдаун")
  259. gpu.set(80, 15, "игры одна серверная минута.")
  260. function emptyarray()
  261. local arr = {}
  262. for i = 1, 36 do
  263. arr[i] = {}
  264. for j = 1, 36 do
  265. arr[i][j] = 0
  266. end
  267. end
  268. return arr
  269. end
  270. local a = emptyarray()
  271. setGame(false)
  272. drawField(false)
  273. if (cooldown > (os.time() + 4320)) then
  274. cooldown = os.time();
  275. end
  276. while true do
  277. while (cooldown - os.time()) >= 0 do
  278. gpu.setBackground(0xaaaaaa)
  279. gpu.fill(81, 33, 34, 5, " ")
  280. gpu.set(91, 35, "Кулдаун " .. math.floor((cooldown - os.time()) / 72) .. " с.")
  281. local e, _, left, top, _, p = event.pull(1, "touch")
  282. if e and (left > 80) and (left < 115) and (top > 27) and (top < 32) then
  283. error("Exit by request")
  284. end
  285. end
  286. setGame(false)
  287. local e, _, left, top, _, p = event.pull("touch")
  288. if (left > 80) and (left < 115) and (top > 32) and (top < 38) then
  289. ending = os.time() + 4320
  290. player = p
  291. drawField(true)
  292. setGame(true)
  293. end
  294. if (left > 80) and (left < 115) and (top > 27) and (top < 32) then
  295. error("Exit by request")
  296. end
  297. while game do
  298. local e, _, left, top, _, p2 = event.pullMultiple(1, "touch", "drag", "drop")
  299. if (os.time() >= ending) then
  300. lose("не успел")
  301. end
  302. if (player == p2) then
  303. if ((e == "touch") or (e == "drop")) and (dragging) then
  304. lose("отпустил мышь")
  305. end
  306. if (e == "touch") and ((left == 5) or (left == 6)) and (top == 19) then
  307. lastx, lasty = math.floor((left - 3) / 2), (top - 2)
  308. dragging = true
  309. end
  310. if (e == "drag") and (dragging) then
  311. drag(left, top)
  312. end
  313. end
  314.  
  315. if (not game) then
  316. cooldown = os.time() + 4320
  317. p = nil
  318. dragging = false
  319. file = io.open('snake-cooldown', 'w')
  320. file:write(cooldown)
  321. file:close();
  322. end
  323. end
  324. if (p == nil) then
  325. setGame(false)
  326. end
  327. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement