blockbattle.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. -- title: blockbattle
  2. -- author: pixelbath
  3. -- desc: a falling blocks game totally not inspired by anything previous
  4. -- script: lua
  5. t=0
  6. local pieces = {
  7. {
  8. {
  9. {0, 0, 0, 0},
  10. {1, 1, 1, 1},
  11. {0, 0, 0, 0},
  12. {0, 0, 0, 0},
  13. },
  14. {
  15. {0, 1, 0, 0},
  16. {0, 1, 0, 0},
  17. {0, 1, 0, 0},
  18. {0, 1, 0, 0},
  19. },
  20. },
  21. {
  22. {
  23. {0, 0, 0, 0},
  24. {0, 4, 4, 0},
  25. {0, 4, 4, 0},
  26. {0, 0, 0, 0},
  27. },
  28. },
  29. {
  30. {
  31. {0, 0, 0, 0},
  32. {2, 2, 2, 0},
  33. {0, 0, 2, 0},
  34. {0, 0, 0, 0},
  35. },
  36. {
  37. {0, 2, 0, 0},
  38. {0, 2, 0, 0},
  39. {2, 2, 0, 0},
  40. {0, 0, 0, 0},
  41. },
  42. {
  43. {2, 0, 0, 0},
  44. {2, 2, 2, 0},
  45. {0, 0, 0, 0},
  46. {0, 0, 0, 0},
  47. },
  48. {
  49. {0, 2, 2, 0},
  50. {0, 2, 0, 0},
  51. {0, 2, 0, 0},
  52. {0, 0, 0, 0},
  53. },
  54. },
  55. {
  56. {
  57. {0, 0, 0, 0},
  58. {3, 3, 3, 0},
  59. {3, 0, 0, 0},
  60. {0, 0, 0, 0},
  61. },
  62. {
  63. {0, 3, 0, 0},
  64. {0, 3, 0, 0},
  65. {0, 3, 3, 0},
  66. {0, 0, 0, 0},
  67. },
  68. {
  69. {0, 0, 3, 0},
  70. {3, 3, 3, 0},
  71. {0, 0, 0, 0},
  72. {0, 0, 0, 0},
  73. },
  74. {
  75. {3, 3, 0, 0},
  76. {0, 3, 0, 0},
  77. {0, 3, 0, 0},
  78. {0, 0, 0, 0},
  79. },
  80. },
  81. {
  82. {
  83. {0, 0, 0, 0},
  84. {5, 5, 5, 0},
  85. {0, 5, 0, 0},
  86. {0, 0, 0, 0},
  87. },
  88. {
  89. {0, 5, 0, 0},
  90. {0, 5, 5, 0},
  91. {0, 5, 0, 0},
  92. {0, 0, 0, 0},
  93. },
  94. {
  95. {0, 5, 0, 0},
  96. {5, 5, 5, 0},
  97. {0, 0, 0, 0},
  98. {0, 0, 0, 0},
  99. },
  100. {
  101. {0, 5, 0, 0},
  102. {5, 5, 0, 0},
  103. {0, 5, 0, 0},
  104. {0, 0, 0, 0},
  105. },
  106. },
  107. {
  108. {
  109. {0, 0, 0, 0},
  110. {0, 6, 6, 0},
  111. {6, 6, 0, 0},
  112. {0, 0, 0, 0},
  113. },
  114. {
  115. {6, 0, 0, 0},
  116. {6, 6, 0, 0},
  117. {0, 6, 0, 0},
  118. {0, 0, 0, 0},
  119. },
  120. },
  121. {
  122. {
  123. {0, 0, 0, 0},
  124. {7, 7, 0, 0},
  125. {0, 7, 7, 0},
  126. {0, 0, 0, 0},
  127. },
  128. {
  129. {0, 7, 0, 0},
  130. {7, 7, 0, 0},
  131. {7, 0, 0, 0},
  132. {0, 0, 0, 0},
  133. },
  134. },
  135. }
  136. local board = {}
  137. local pieceType = 3
  138. local pieceRotation = 1
  139. local piece_x = 3
  140. local piece_y = 0
  141. function init_board()
  142. for y = 1, 20 do
  143. board[y] = {}
  144. for x = 1, 10 do
  145. board[y][x] = 0
  146. end
  147. end
  148. end
  149. function draw_piece()
  150. for y = 1, 4 do
  151. for x = 1, 4 do
  152. local block = pieces[pieceType][pieceRotation][y][x]
  153. if block ~= 0 then
  154. local blockSize = 8
  155. local blockDrawSize = blockSize - 1
  156. rect(
  157. (piece_x + x - 1) * blockSize,
  158. (piece_y + y - 1) * blockSize,
  159. blockDrawSize,
  160. blockDrawSize,
  161. block
  162. )
  163. end
  164. end
  165. end
  166. end
  167. local factor_are = 4 -- piece spawn delay
  168. local factor_das = 20
  169. local factor_keyrepeat = 5
  170. local t_move = 0
  171. local is_move_pressed = false
  172. local is_das_triggered = false
  173. function hard_drop()
  174. end
  175. function soft_drop()
  176. piece_y = piece_y + 1
  177. end
  178. function try_move_right()
  179. piece_x = piece_x + 1
  180. end
  181. function try_move_left()
  182. piece_x = piece_x - 1
  183. end
  184. function handle_input()
  185. is_move_pressed = false
  186. if btn(0) then
  187. hard_drop()
  188. end
  189. if btn(1) then
  190. is_move_pressed = true
  191. if t_move <= 0 then
  192. if not is_das_triggered then
  193. t_move = factor_das
  194. else
  195. t_move = factor_keyrepeat
  196. end
  197. is_das_triggered = true
  198. soft_drop()
  199. end
  200. end
  201. if btn(2) then
  202. is_move_pressed = true
  203. if t_move <= 0 then
  204. if not is_das_triggered then
  205. t_move = factor_das
  206. else
  207. t_move = factor_keyrepeat
  208. end
  209. is_das_triggered = true
  210. try_move_left()
  211. end
  212. end
  213. if btn(3) then
  214. is_move_pressed = true
  215. if t_move <= 0 then
  216. if not is_das_triggered then
  217. t_move = factor_das
  218. else
  219. t_move = factor_keyrepeat
  220. end
  221. is_das_triggered = true
  222. try_move_right()
  223. end
  224. end
  225. if btnp(4) then
  226. pieceRotation = pieceRotation - 1
  227. if pieceRotation < 1 then
  228. pieceRotation = #pieces[pieceType]
  229. end
  230. end
  231. if btnp(5) then
  232. pieceRotation = pieceRotation + 1
  233. if pieceRotation > #pieces[pieceType] then
  234. pieceRotation = 1
  235. end
  236. end
  237. if is_move_pressed then
  238. t_move = t_move - 1
  239. else
  240. t_move = -1
  241. is_das_triggered = false
  242. end
  243. end
  244. function TIC()
  245. handle_input()
  246. cls(0)
  247. draw_piece()
  248. print("is_move_pressed: " .. tostring(is_move_pressed), 0, 0)
  249. print("is_das_triggered: " .. tostring(is_das_triggered), 0, 7)
  250. print("t_move: " .. tostring(t_move), 0, 14)
  251. end
  252. -- <PALETTE>
  253. -- 000:140c1c44243430346d4e4a4e854c30346524d04648757161597dced27d2c8595a16daa2cd2aa996dc2cadad45edeeed6
  254. -- </PALETTE>