shmup.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. -- title: untitled shmup
  2. -- author: game developer
  3. -- desc: something like Gradius/Nemesis/Salamander/Life Force
  4. -- script: lua
  5. t=0
  6. local bullet_speed = 6
  7. local bullet_timer = 0
  8. local bullet_timer_start = 10
  9. local player_bullets = {}
  10. local player = {
  11. x=20,y=60,
  12. vx=0,vy=0,
  13. speed=1.5,
  14. sp=256,
  15. fire_t=0,
  16. fire_held=false,
  17. gun_type=1,
  18. gun_level=1,
  19. gunpods={},
  20. }
  21. local grad_palettes = {
  22. { 1, 2, 3, 4, 12 },
  23. { 7, 6, 5, 4, 12 },
  24. { 8, 9, 10, 11, 12 },
  25. }
  26. function mod(a, b)
  27. return math.floor(a-(math.floor(a/b)*b))
  28. end
  29. function draw_gradient(xpos, ypos, width, height, startindex, gradient)
  30. local idx = startindex or 1
  31. local grad = gradient or 1
  32. local band_w = width / 5 --# of colors in each gradient
  33. local cur_band = 0
  34. for i=0,width-1 do
  35. if cur_band >= band_w then
  36. idx = idx + 1
  37. cur_band = 0
  38. end
  39. cur_band = cur_band + 1
  40. if idx > 5 then idx = 1 end
  41. line(xpos + i, ypos, xpos + i - height, ypos + height, grad_palettes[grad][idx])
  42. end
  43. end
  44. -- TODO: various types
  45. function add_bullet()
  46. sfx(0, 'D-6', 10, 0, 11, 4)
  47. table.insert(player_bullets, {
  48. x=player.x + 10,
  49. y=player.y,
  50. vx = 0,
  51. vy = 0,
  52. })
  53. end
  54. function add_gunpod()
  55. if #player.gunpods < 5 then
  56. table.insert(player.gunpods, {
  57. x=0,y=0,vx=0,vy=0,
  58. sprs={ 259,260,261,262,263 },
  59. fr=1,t=0,frametime=5,
  60. })
  61. end
  62. end
  63. function draw_player()
  64. local engine_spr = 274
  65. if player.vx > 0 then
  66. engine_spr = 306
  67. elseif player.vx < 0 then
  68. engine_spr = 290
  69. end
  70. if player.vy > 0 then
  71. player.sp = 272
  72. elseif player.vy < 0 then
  73. player.sp = 288
  74. else
  75. player.sp = 256
  76. end
  77. -- draw engine fire then player
  78. spr(engine_spr, player.x-8, player.y+1)
  79. spr(player.sp, player.x, player.y, 0, 1, 0, 0, 2, 1)
  80. -- draw gunpods
  81. if #player.gunpods > 0 then
  82. for key,pod in ipairs(player.gunpods) do
  83. pod.t = pod.t + 1
  84. if mod(pod.t, pod.frametime) == 0 then
  85. pod.fr = pod.fr + 1
  86. if pod.fr > #pod.sprs then pod.fr = 1 end
  87. end
  88. spr(pod.sprs[pod.fr], pod.x, pod.y)
  89. -- TODO: move this into an update
  90. local targx = player.x + 4 - pod.x
  91. local targy = player.y - 10 - pod.y
  92. pod.vx = targx / 3
  93. pod.vy = targy / 3
  94. pod.x = pod.x + pod.vx
  95. pod.y = pod.y + pod.vy
  96. end
  97. end
  98. end
  99. add_gunpod()
  100. function draw_bullets()
  101. for key,val in ipairs(player_bullets) do
  102. spr(258, val.x, val.y, 0)
  103. end
  104. end
  105. function handle_input()
  106. player.vx = 0
  107. player.vy = 0
  108. if btn(0) then
  109. player.vy=-player.speed
  110. end
  111. if btn(1) then
  112. player.vy=player.speed
  113. end
  114. if btn(2) then
  115. player.vx=-player.speed
  116. end
  117. if btn(3) then
  118. player.vx=player.speed
  119. end
  120. -- slow down diagonal movements
  121. if player.vx * player.vy ~= 0 then
  122. player.vx = player.vx * 0.707
  123. player.vy = player.vy * 0.707
  124. end
  125. player.x = player.x + player.vx
  126. player.y = player.y + player.vy
  127. if player.x < 5 then player.x = 5 end
  128. if player.x > 220 then player.x = 220 end
  129. if player.y < 5 then player.y = 5 end
  130. if player.y > 128 then player.y = 128 end
  131. player.fire_held = false
  132. if btn(4) then
  133. if player.fire_t <= 0 then
  134. player.fire_t = bullet_timer_start
  135. add_bullet()
  136. else
  137. player.fire_t = player.fire_t - 1
  138. end
  139. player.fire_held = true
  140. else
  141. player.fire_t = 0
  142. end
  143. -- debug
  144. if btnp(5) then
  145. player.gun_type = mod(player.gun_type + 1, 3) + 1
  146. end
  147. end
  148. function TIC()
  149. handle_input()
  150. for key,val in ipairs(player_bullets) do
  151. val.x = val.x + bullet_speed
  152. if val.x > 240 then
  153. table.remove(player_bullets, key)
  154. end
  155. end
  156. cls(0)
  157. draw_player()
  158. draw_bullets()
  159. draw_gradient(10, 40, 10, 5, mod(t/3, 5) + 1, player.gun_type)
  160. dbg("type:"..tostring(player.gun_type), 0)
  161. dbg(tostring(player.fire_held), 1)
  162. dbg(tostring(mod(t/3, 5) + 1), 2)
  163. -- dbg(tostring(player.vy), 3)
  164. t=t+1
  165. end
  166. function dbg(msg, line_no)
  167. print(msg, 5, line_no * 7, 5, true, 1, true)
  168. end
  169. -- <SPRITES>
  170. -- 000:00000000eccef0000edddefa0fcccc8bcf9deededfb8cddd00db8cdd00008cce
  171. -- 001:000000000000000090000000aa9deecc99eedcddddcccdf0ccf0000000000000
  172. -- 002:0000000000000000000000004040440030303300000000000000000000000000
  173. -- 003:00dd00000eeedddd023eeee02344320023443200023eddd00feefeee00ff0000
  174. -- 004:000000000f1eeddd02331ee023443200234eddd00feefeee00f1000000000000
  175. -- 005:000000000022000002332eef23dd31f0231ddddd0fe1eee00022000000000000
  176. -- 006:000000000022000002dd2000231ddddd23e1eeee02ee20000022000000000000
  177. -- 007:0000000000de00000f1ddddd2341eee02344320002331ee00f1eeddd00000000
  178. -- 016:dccefeed0ddddefa0fcccc8acf9deededfbbcddd0000bcdd00008ccc00000dd0
  179. -- 017:d00000009eedd000aaaaeecc9aa9dccdddcccdf0ccd00000d000000000000000
  180. -- 018:0000000000000880000889ab08899abc000889ab000008800000000000000000
  181. -- 032:ecdf00000edddf0000edddc00bbcfeeddcc99feeee8888880bc99f99000bfeee
  182. -- 033:000000000000000009aa0000cdc99e00eeedddcc88888eef9eeeedd0df000000
  183. -- 034:00000000000000000000089a000089ab0000089a000000000000000000000000
  184. -- 050:00000000000099a000889abb8889accc00889abb000099a00000000000000000
  185. -- </SPRITES>
  186. -- <WAVES>
  187. -- 000:123345667889abbcdeff1fedcba86642
  188. -- </WAVES>
  189. -- <SFX>
  190. -- 000:00d050808060c040d020e020f010f010f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000502000000000
  191. -- </SFX>
  192. -- <PALETTE>
  193. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  194. -- </PALETTE>