blockbattle.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. -- title: blockbattle
  2. -- author: pixelbath
  3. -- desc: a falling blocks game totally not inspired by anything previous
  4. -- script: lua
  5. local spriteIndexStart = 256
  6. local blockSize = 5
  7. local t = 0
  8. local t_move = 0
  9. local is_move_pressed = false
  10. local is_das_triggered = false
  11. -- i, o, j, l, t, s, z
  12. local pieces = {
  13. {
  14. {
  15. {0, 0, 0, 0},
  16. {1, 1, 1, 1},
  17. {0, 0, 0, 0},
  18. {0, 0, 0, 0},
  19. },
  20. {
  21. {0, 1, 0, 0},
  22. {0, 1, 0, 0},
  23. {0, 1, 0, 0},
  24. {0, 1, 0, 0},
  25. },
  26. },
  27. {
  28. {
  29. {0, 0, 0, 0},
  30. {0, 4, 4, 0},
  31. {0, 4, 4, 0},
  32. {0, 0, 0, 0},
  33. },
  34. },
  35. {
  36. {
  37. {0, 0, 0, 0},
  38. {2, 2, 2, 0},
  39. {0, 0, 2, 0},
  40. {0, 0, 0, 0},
  41. },
  42. {
  43. {0, 2, 0, 0},
  44. {0, 2, 0, 0},
  45. {2, 2, 0, 0},
  46. {0, 0, 0, 0},
  47. },
  48. {
  49. {2, 0, 0, 0},
  50. {2, 2, 2, 0},
  51. {0, 0, 0, 0},
  52. {0, 0, 0, 0},
  53. },
  54. {
  55. {0, 2, 2, 0},
  56. {0, 2, 0, 0},
  57. {0, 2, 0, 0},
  58. {0, 0, 0, 0},
  59. },
  60. },
  61. {
  62. {
  63. {0, 0, 0, 0},
  64. {3, 3, 3, 0},
  65. {3, 0, 0, 0},
  66. {0, 0, 0, 0},
  67. },
  68. {
  69. {0, 3, 0, 0},
  70. {0, 3, 0, 0},
  71. {0, 3, 3, 0},
  72. {0, 0, 0, 0},
  73. },
  74. {
  75. {0, 0, 3, 0},
  76. {3, 3, 3, 0},
  77. {0, 0, 0, 0},
  78. {0, 0, 0, 0},
  79. },
  80. {
  81. {3, 3, 0, 0},
  82. {0, 3, 0, 0},
  83. {0, 3, 0, 0},
  84. {0, 0, 0, 0},
  85. },
  86. },
  87. {
  88. {
  89. {0, 0, 0, 0},
  90. {5, 5, 5, 0},
  91. {0, 5, 0, 0},
  92. {0, 0, 0, 0},
  93. },
  94. {
  95. {0, 5, 0, 0},
  96. {0, 5, 5, 0},
  97. {0, 5, 0, 0},
  98. {0, 0, 0, 0},
  99. },
  100. {
  101. {0, 5, 0, 0},
  102. {5, 5, 5, 0},
  103. {0, 0, 0, 0},
  104. {0, 0, 0, 0},
  105. },
  106. {
  107. {0, 5, 0, 0},
  108. {5, 5, 0, 0},
  109. {0, 5, 0, 0},
  110. {0, 0, 0, 0},
  111. },
  112. },
  113. {
  114. {
  115. {0, 0, 0, 0},
  116. {0, 6, 6, 0},
  117. {6, 6, 0, 0},
  118. {0, 0, 0, 0},
  119. },
  120. {
  121. {6, 0, 0, 0},
  122. {6, 6, 0, 0},
  123. {0, 6, 0, 0},
  124. {0, 0, 0, 0},
  125. },
  126. },
  127. {
  128. {
  129. {0, 0, 0, 0},
  130. {7, 7, 0, 0},
  131. {0, 7, 7, 0},
  132. {0, 0, 0, 0},
  133. },
  134. {
  135. {0, 7, 0, 0},
  136. {7, 7, 0, 0},
  137. {7, 0, 0, 0},
  138. {0, 0, 0, 0},
  139. },
  140. },
  141. }
  142. local board = {}
  143. local game_state = {
  144. piece_queue = {},
  145. piece_bag = {},
  146. update = nil,
  147. }
  148. local active_piece = {
  149. type = nil,
  150. rotation = 1,
  151. x = 3, y = 0,
  152. }
  153. local rules = {
  154. keyrepeat = 5,
  155. das = 20,
  156. are = 4,
  157. }
  158. -- gaiden, vs, single
  159. local options = {
  160. gametype = "gaiden",
  161. shared_queue = true,
  162. }
  163. function shuffle(x)
  164. for i = #x, 2, -1 do
  165. local j = math.random(i)
  166. x[i], x[j] = x[j], x[i]
  167. end
  168. end
  169. function fill_bag()
  170. -- just throw a bag of randomized pieces in it
  171. local new_bag = { 1, 2, 3, 4, 5, 6, 7 }
  172. shuffle(new_bag)
  173. for i = 1,7 do
  174. table.insert(game_state.piece_bag, new_bag[i])
  175. end
  176. end
  177. function init_board()
  178. for y = 1, 20 do
  179. board[y] = {}
  180. for x = 1, 10 do
  181. board[y][x] = 0
  182. end
  183. end
  184. end
  185. function spawn_piece()
  186. if #game_state.piece_bag <= 4 then
  187. fill_bag()
  188. end
  189. active_piece = {
  190. type = game_state.piece_bag[1],
  191. rotation = 1,
  192. x = 3, y = 0,
  193. }
  194. table.remove(game_state.piece_bag, 1)
  195. end
  196. function hard_drop()
  197. -- active_piece.rotation = 1
  198. active_piece.type = active_piece.type + 1
  199. if active_piece.type > 7 then active_piece.type = 1 end
  200. trace("type: "..tostring(active_piece.type)..", rot="..tostring(active_piece.rotation))
  201. end
  202. function soft_drop()
  203. active_piece.y = active_piece.y + 1
  204. end
  205. function try_move_right()
  206. active_piece.x = active_piece.x + 1
  207. end
  208. function try_move_left()
  209. active_piece.x = active_piece.x - 1
  210. end
  211. function handle_input()
  212. is_move_pressed = false
  213. if btnp(0) then
  214. spawn_piece()
  215. end
  216. if btn(1) then
  217. is_move_pressed = true
  218. if t_move <= 0 then
  219. t_move = rules.das
  220. soft_drop()
  221. end
  222. end
  223. if btn(2) then
  224. is_move_pressed = true
  225. if t_move <= 0 then
  226. if not is_das_triggered then
  227. t_move = rules.das
  228. else
  229. t_move = rules.keyrepeat
  230. end
  231. is_das_triggered = true
  232. try_move_left()
  233. end
  234. end
  235. if btn(3) then
  236. is_move_pressed = true
  237. if t_move <= 0 then
  238. if not is_das_triggered then
  239. t_move = rules.das
  240. else
  241. t_move = rules.keyrepeat
  242. end
  243. is_das_triggered = true
  244. try_move_right()
  245. end
  246. end
  247. if btnp(4) then
  248. active_piece.rotation = active_piece.rotation - 1
  249. if active_piece.rotation < 1 then
  250. active_piece.rotation = #pieces[active_piece.type]
  251. end
  252. end
  253. if btnp(5) then
  254. active_piece.rotation = active_piece.rotation + 1
  255. if active_piece.rotation > #pieces[active_piece.type] then
  256. active_piece.rotation = 1
  257. end
  258. end
  259. if is_move_pressed then
  260. t_move = t_move - 1
  261. else
  262. t_move = -1
  263. is_das_triggered = false
  264. end
  265. end
  266. function draw_board(xpos, ypos)
  267. -- draw next pieces
  268. for i=1,3 do
  269. local ptype = game_state.piece_bag[i]
  270. draw_piece(200, 20 + (i * 20), ptype, 1)
  271. end
  272. end
  273. function draw_piece(xpos, ypos, ptype, rotation)
  274. for y = 1, 4 do
  275. for x = 1, 4 do
  276. local block = pieces[ptype][rotation][y][x]
  277. if block ~= 0 then
  278. spr(
  279. spriteIndexStart + block,
  280. (x - 1) * blockSize + xpos,
  281. (y - 1) * blockSize + ypos
  282. )
  283. end
  284. end
  285. end
  286. end
  287. -- BEGIN state updates
  288. function update_intro()
  289. if t == 30 then
  290. game_state.update = update_menu
  291. else
  292. print("intro " .. tostring(30-t))
  293. end
  294. end
  295. function update_menu()
  296. print("Press X to start")
  297. if btnp(5) then
  298. game_state.update = update_game
  299. init_board()
  300. fill_bag()
  301. spawn_piece()
  302. end
  303. end
  304. function update_game()
  305. handle_input()
  306. draw_board()
  307. draw_piece(active_piece.x * blockSize, active_piece.y * blockSize, active_piece.type, active_piece.rotation)
  308. end
  309. -- END state updates
  310. game_state.update = update_intro
  311. function TIC()
  312. cls(0)
  313. -- call our current state's update
  314. game_state.update()
  315. t = t + 1
  316. end
  317. -- <SPRITES>
  318. -- 001:bbbbc0009aaab0009aaab0009aaab00089999000000000000000000000000000
  319. -- 002:8aaab0008999a0008999a0008999a00088888000000000000000000000000000
  320. -- 003:4444c00023334000233340002333400012222000000000000000000000000000
  321. -- 004:3cccc0002444c0002444c0002444c00012222000000000000000000000000000
  322. -- 005:55554000766650007666500076665000f7777000000000000000000000000000
  323. -- 006:ddddc000feeed000feeed000feeed000fffff000000000000000000000000000
  324. -- 007:3333400012223000122230001222300081111000000000000000000000000000
  325. -- 016:8bbbb0008abba0008a99a0008999900088888000000000000000000000000000
  326. -- 017:f5555000f6556000f6776000f7777000fffff000000000000000000000000000
  327. -- 018:0aaa0000a000a000a000a000a000a0000aaa0000000000000000000000000000
  328. -- 019:0666000060006000600060006000600006660000000000000000000000000000
  329. -- 020:000000000bc000000abbc0000aaabbc00aaaaff00aaff0000ff0000000000000
  330. -- 021:0000000005400000065540000666554006666ff0066ff0000ff0000000000000
  331. -- 022:0000000000044000004cc4000444cc4004444440004444000004400000000000
  332. -- </SPRITES>
  333. -- <PALETTE>
  334. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  335. -- </PALETTE>