Advertisement
nonogamer9

e

Oct 29th, 2024 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. local board_width, board_height = 6, 13
  2. local block_width, block_height = 2, 1
  3. local board_offset_x = 19
  4. local board_offset_y = 3
  5.  
  6. local colors_list = {
  7. colors.red,
  8. colors.blue,
  9. colors.green,
  10. colors.yellow,
  11. colors.magenta,
  12. colors.cyan
  13. }
  14.  
  15. local board = {}
  16. local current_piece = {}
  17. local game_over = false
  18. local score = 0
  19. local level = 1
  20. local fall_interval = 0.5 -- Initial fall speed (in seconds)
  21.  
  22. -- Initialize the board
  23. for y = 1, board_height do
  24. board[y] = {}
  25. for x = 1, board_width do
  26. board[y][x] = 0
  27. end
  28. end
  29.  
  30. -- Create buffers for double buffering
  31. local main_buffer = window.create(term.current(), 1, 1, 51, 19)
  32. local back_buffer = window.create(term.current(), 1, 1, 51, 19, false)
  33.  
  34. -- Function to create a new piece
  35. local function new_piece()
  36. current_piece = {
  37. x = math.floor(board_width / 2),
  38. y = 1,
  39. colors = {
  40. colors_list[math.random(#colors_list)],
  41. colors_list[math.random(#colors_list)],
  42. colors_list[math.random(#colors_list)]
  43. }
  44. }
  45. end
  46.  
  47. -- Function to draw the adaptive plasma sinusoid background
  48. local function draw_plasma_background()
  49. local time = os.clock() * 2
  50. local board_pixel_width = board_width * block_width
  51. local board_pixel_height = board_height * block_height
  52.  
  53. for y = 1, board_pixel_height do
  54. for x = 1, board_pixel_width do
  55. local nx = x / board_pixel_width
  56. local ny = y / board_pixel_height
  57. local v = math.sin(nx * 10 + time) + math.sin(ny * 10 + time)
  58. v = (v + 2) / 4 -- Normalize to 0-1 range
  59. local color = math.floor(v * 7) + 8 -- Map to color range 8-15 (lighter colors)
  60. back_buffer.setCursorPos(board_offset_x + x - 1, board_offset_y + y - 1)
  61. back_buffer.setBackgroundColor(color)
  62. back_buffer.write(" ")
  63. end
  64. end
  65. end
  66.  
  67. -- Function to draw a block
  68. local function draw_block(x, y, color)
  69. back_buffer.setBackgroundColor(color)
  70. for by = 0, block_height - 1 do
  71. for bx = 0, block_width - 1 do
  72. back_buffer.setCursorPos(board_offset_x + (x-1)*block_width + bx, board_offset_y + (y-1)*block_height + by)
  73. back_buffer.write(" ")
  74. end
  75. end
  76. end
  77.  
  78. -- Function to draw the game board
  79. local function draw_board()
  80. back_buffer.setBackgroundColor(colors.gray)
  81. back_buffer.clear()
  82.  
  83. draw_plasma_background()
  84.  
  85. -- Draw the board contents
  86. for y = 1, board_height do
  87. for x = 1, board_width do
  88. if board[y][x] ~= 0 then
  89. draw_block(x, y, board[y][x])
  90. end
  91. end
  92. end
  93.  
  94. -- Draw the current piece
  95. for i = 1, 3 do
  96. draw_block(current_piece.x, current_piece.y + i - 1, current_piece.colors[i])
  97. end
  98.  
  99. -- Draw score and level
  100. back_buffer.setTextColor(colors.white)
  101. back_buffer.setBackgroundColor(colors.gray)
  102. back_buffer.setCursorPos(1, 1)
  103. back_buffer.write("Score: " .. score)
  104. back_buffer.setCursorPos(1, 2)
  105. back_buffer.write("Level: " .. level)
  106.  
  107. -- Swap buffers
  108. back_buffer.setVisible(true)
  109. main_buffer.setVisible(false)
  110. main_buffer, back_buffer = back_buffer, main_buffer
  111. end
  112.  
  113. -- Function to check for matches
  114. local function check_matches()
  115. local matches = {}
  116.  
  117. -- Check vertical matches
  118. for x = 1, board_width do
  119. for y = 1, board_height - 2 do
  120. if board[y][x] ~= 0 and board[y][x] == board[y+1][x] and board[y][x] == board[y+2][x] then
  121. matches[#matches+1] = {y, x}
  122. matches[#matches+1] = {y+1, x}
  123. matches[#matches+1] = {y+2, x}
  124. end
  125. end
  126. end
  127.  
  128. -- Check horizontal matches
  129. for y = 1, board_height do
  130. for x = 1, board_width - 2 do
  131. if board[y][x] ~= 0 and board[y][x] == board[y][x+1] and board[y][x] == board[y][x+2] then
  132. matches[#matches+1] = {y, x}
  133. matches[#matches+1] = {y, x+1}
  134. matches[#matches+1] = {y, x+2}
  135. end
  136. end
  137. end
  138.  
  139. -- Check diagonal matches (top-left to bottom-right)
  140. for y = 1, board_height - 2 do
  141. for x = 1, board_width - 2 do
  142. if board[y][x] ~= 0 and board[y][x] == board[y+1][x+1] and board[y][x] == board[y+2][x+2] then
  143. matches[#matches+1] = {y, x}
  144. matches[#matches+1] = {y+1, x+1}
  145. matches[#matches+1] = {y+2, x+2}
  146. end
  147. end
  148. end
  149.  
  150. -- Check diagonal matches (top-right to bottom-left)
  151. for y = 1, board_height - 2 do
  152. for x = 3, board_width do
  153. if board[y][x] ~= 0 and board[y][x] == board[y+1][x-1] and board[y][x] == board[y+2][x-2] then
  154. matches[#matches+1] = {y, x}
  155. matches[#matches+1] = {y+1, x-1}
  156. matches[#matches+1] = {y+2, x-2}
  157. end
  158. end
  159. end
  160.  
  161. -- Remove matches and update score
  162. for _, match in ipairs(matches) do
  163. board[match[1]][match[2]] = 0
  164. score = score + 10
  165. end
  166.  
  167. -- Move blocks down
  168. for x = 1, board_width do
  169. local write_y = board_height
  170. for y = board_height, 1, -1 do
  171. if board[y][x] ~= 0 then
  172. board[write_y][x] = board[y][x]
  173. if write_y ~= y then
  174. board[y][x] = 0
  175. end
  176. write_y = write_y - 1
  177. end
  178. end
  179. end
  180.  
  181. -- Update level and fall speed
  182. level = math.floor(score / 100) + 1
  183. fall_interval = math.max(0.1, 0.5 - (level - 1) * 0.05)
  184.  
  185. return #matches > 0
  186. end
  187.  
  188. -- Function to move the piece down
  189. local function move_piece_down()
  190. if current_piece.y + 2 >= board_height or board[current_piece.y+3][current_piece.x] ~= 0 then
  191. -- Place piece on board
  192. for i = 1, 3 do
  193. if current_piece.y + i - 1 <= board_height then
  194. board[current_piece.y+i-1][current_piece.x] = current_piece.colors[i]
  195. end
  196. end
  197.  
  198. -- Check for matches
  199. while check_matches() do
  200. draw_board()
  201. os.sleep(0.2)
  202. end
  203.  
  204. -- Create new piece
  205. new_piece()
  206.  
  207. -- Check for game over
  208. if board[1][current_piece.x] ~= 0 then
  209. game_over = true
  210. end
  211.  
  212. return true -- Piece has landed
  213. else
  214. current_piece.y = current_piece.y + 1
  215. return false -- Piece is still falling
  216. end
  217. end
  218.  
  219. -- Function to handle user input and game updates
  220. local function game_loop()
  221. local last_fall_time = os.clock()
  222. local last_draw_time = os.clock()
  223.  
  224. while not game_over do
  225. -- Handle input
  226. local timer = os.startTimer(0.05)
  227. local event, param = os.pullEvent()
  228. if event == "key" then
  229. local key = param
  230. if key == keys.left and current_piece.x > 1 and
  231. board[current_piece.y][current_piece.x-1] == 0 and
  232. board[current_piece.y+1][current_piece.x-1] == 0 and
  233. board[current_piece.y+2][current_piece.x-1] == 0 then
  234. current_piece.x = current_piece.x - 1
  235. elseif key == keys.right and current_piece.x < board_width and
  236. board[current_piece.y][current_piece.x+1] == 0 and
  237. board[current_piece.y+1][current_piece.x+1] == 0 and
  238. board[current_piece.y+2][current_piece.x+1] == 0 then
  239. current_piece.x = current_piece.x + 1
  240. elseif key == keys.down then
  241. move_piece_down()
  242. elseif key == keys.up then
  243. table.insert(current_piece.colors, table.remove(current_piece.colors, 1))
  244. end
  245. end
  246.  
  247. -- Update game state
  248. local current_time = os.clock()
  249. if current_time - last_fall_time >= fall_interval then
  250. move_piece_down()
  251. last_fall_time = current_time
  252. end
  253.  
  254. -- Redraw the board (limit to 30 FPS)
  255. if current_time - last_draw_time >= 1/30 then
  256. draw_board()
  257. last_draw_time = current_time
  258. end
  259. end
  260. end
  261.  
  262. -- Main game function
  263. local function play_game()
  264. new_piece()
  265. draw_board()
  266.  
  267. game_loop()
  268.  
  269. -- Game over screen
  270. main_buffer.setBackgroundColor(colors.black)
  271. main_buffer.clear()
  272. main_buffer.setCursorPos(1, 8)
  273. main_buffer.setTextColor(colors.white)
  274. main_buffer.write("Game Over!")
  275. main_buffer.setCursorPos(1, 10)
  276. main_buffer.write("Final Score: " .. score)
  277. main_buffer.setCursorPos(1, 11)
  278. main_buffer.write("Final Level: " .. level)
  279. os.sleep(3)
  280. end
  281.  
  282. -- Start the game
  283. play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement