survivor.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. -- title: survivor
  2. -- author: pixelbath
  3. -- desc: an overhead game where you try not to die
  4. local t = 0
  5. local p = {
  6. x = 120, y = 68, hp = 1000,
  7. }
  8. local cam = {
  9. x = 120, y = 68,
  10. }
  11. local enemies = {}
  12. local drops = {}
  13. function add_drop(x, y, t)
  14. table.insert(enemies, {
  15. x = x, y = y, type = t, dc = 0.25,
  16. })
  17. end
  18. function spawn_enemy(x, y)
  19. table.insert(enemies, {x=x, y=y,})
  20. end
  21. function kill_enemy(en)
  22. -- TODO: check drop chance
  23. if math.random() > en.dc then
  24. add_drop(en.x, en.y, 0)
  25. end
  26. table.remove(enemies, en)
  27. end
  28. function draw_player()
  29. rect(p.x + cam.x, p.y + cam.y, 8, 8, 5)
  30. end
  31. -- also update here to save a loop
  32. function draw_enemies()
  33. for k, en in ipairs(enemies) do
  34. rect(en.x + cam.x, en.y + cam.y, 8, 8, 3)
  35. if en.x > p.x and en.x < p.x + 8 and en.y > p.y and en.y < p.y + 8 then
  36. p.hp = p.hp - 1
  37. else
  38. -- TODO: trig
  39. en.x = en.x
  40. end
  41. end
  42. end
  43. for i=1,30 do
  44. spawn_enemy(i * 20, 80)
  45. end
  46. function TIC()
  47. if btn(0) then p.y = p.y - 1 end
  48. if btn(1) then p.y = p.y + 1 end
  49. if btn(2) then p.x = p.x - 1 end
  50. if btn(3) then p.x = p.x + 1 end
  51. cam.x = 120 - p.x
  52. cam.y = 68 - p.y
  53. cls(0)
  54. draw_player()
  55. draw_enemies()
  56. print("hp: "..p.hp)
  57. end
  58. -- <PALETTE>
  59. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  60. -- </PALETTE>