blockbattle.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 active_piece = {
  138. type = 3,
  139. rotation = 1,
  140. x = 3, y = 0,
  141. }
  142. local rules = {
  143. keyrepeat = 5,
  144. das = 20,
  145. are = 4,
  146. }
  147. local spriteIndexStart = 256
  148. local blockSize = 8
  149. local t_move = 0
  150. local is_move_pressed = false
  151. local is_das_triggered = false
  152. function init_board()
  153. for y = 1, 20 do
  154. board[y] = {}
  155. for x = 1, 10 do
  156. board[y][x] = 0
  157. end
  158. end
  159. end
  160. function spawn_piece()
  161. end
  162. function draw_piece()
  163. for y = 1, 4 do
  164. for x = 1, 4 do
  165. local block = pieces[active_piece.type][active_piece.rotation][y][x]
  166. if block ~= 0 then
  167. spr(
  168. spriteIndexStart + block,
  169. (active_piece.x + x - 1) * blockSize,
  170. (active_piece.y + y - 1) * blockSize
  171. )
  172. end
  173. end
  174. end
  175. end
  176. function hard_drop()
  177. active_piece.type = active_piece.type + 1
  178. if active_piece.type > 7 then active_piece.type = 1 end
  179. end
  180. function soft_drop()
  181. active_piece.y = active_piece.y + 1
  182. end
  183. function try_move_right()
  184. active_piece.x = active_piece.x + 1
  185. end
  186. function try_move_left()
  187. active_piece.x = active_piece.x - 1
  188. end
  189. function handle_input()
  190. is_move_pressed = false
  191. if btnp(0) then
  192. hard_drop()
  193. end
  194. if btn(1) then
  195. is_move_pressed = true
  196. if t_move <= 0 then
  197. t_move = rules.das
  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 = rules.das
  206. else
  207. t_move = rules.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 = rules.das
  218. else
  219. t_move = rules.keyrepeat
  220. end
  221. is_das_triggered = true
  222. try_move_right()
  223. end
  224. end
  225. if btnp(4) then
  226. active_piece.rotation = active_piece.rotation - 1
  227. if active_piece.rotation < 1 then
  228. active_piece.rotation = #pieces[active_piece.type]
  229. end
  230. end
  231. if btnp(5) then
  232. active_piece.rotation = active_piece.rotation + 1
  233. if active_piece.rotation > #pieces[active_piece.type] then
  234. active_piece.rotation = 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. end
  249. -- <SPRITES>
  250. -- 001:bbbbbbbb9aaaaaab9aaaaaab9aaaaaab9aaaaaab9aaaaaab9aaaaaab89999999
  251. -- 002:8aaaaaaa8999999a8999999a8999999a8999999a8999999a8999999a88888888
  252. -- 003:4444444423333334233333342333333423333334233333342333333412222222
  253. -- 004:cccccccc2444444c2444444c2444444c2444444c2444444c2444444c12222222
  254. -- 005:55555555766666657666666576666665766666657666666576666665f7777777
  255. -- 006:ddddddddfeeeeeedfeeeeeedfeeeeeedfeeeeeedfeeeeeedfeeeeeedffffffff
  256. -- 007:3333333312222223122222231222222312222223122222231222222381111111
  257. -- 016:8bbbbbbb8abbbbba8aabbbaa8aaabaaa8aaa9aaa8aa999aa8a99999a88888888
  258. -- 017:f5555555f6555556f6655566f6665666f6667666f6677766f6777776ffffffff
  259. -- 018:0000000000044000004cc4000444cc4004444440004444000004400000000000
  260. -- 019:0aaaaaa0a000000aa000000aa000000aa000000aa000000aa000000a0aaaaaa0
  261. -- 020:0666666060000006600000066000000660000006600000066000000606666660
  262. -- </SPRITES>
  263. -- <PALETTE>
  264. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  265. -- </PALETTE>