Advertisement
Guest User

code love2d

a guest
Mar 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. enemy = {}
  2. enemies_controller = {}
  3. enemies_controller.enemies = {}
  4.  
  5. function love.load()
  6. player = {}
  7. player.x = 0
  8. player.y = 450
  9. player.bullets = {}
  10. player.cooldown = 20
  11. player.speed = 1
  12. player.fire = function()
  13. if player.cooldown <= 0 then
  14. player.cooldown = 20
  15. bullet = {}
  16. bullet.x = player.x + 5
  17. bullet.y = player.y
  18. table.insert(player.bullets, bullet)
  19. end
  20. end
  21. enemies_controller:spawnEnemy(0,0)
  22. enemies_controller:spawnEnemy(100, 0)
  23. end
  24.  
  25. function enemies_controller:spawnEnemy(x, y)
  26. enemies = {}
  27. enemy = {}
  28. enemy.x = x
  29. enemy.y = y
  30. enemy.bullets = {}
  31. enemy.cooldown = 20
  32. enemy.speed = 1
  33. table.insert(self.enemies, enemy)
  34. end
  35.  
  36. function enemy:fire()
  37. if self.cooldown <= 0 then
  38. self.cooldown = 20
  39. bullet = {}
  40. bullet.x = self.x + 5
  41. bullet.y = self.y
  42. table.insert(self.bullets, bullet)
  43. end
  44. end
  45.  
  46. function love.update(dt)
  47. player.cooldown = player.cooldown - 0.1
  48.  
  49. if love.keyboard.isDown('a') then
  50. player.x = player.x - player.speed
  51. end
  52. if love.keyboard.isDown('d') then
  53. player.x = player.x + player.speed
  54. end
  55.  
  56. if love.keyboard.isDown("space") then
  57. player.fire()
  58. end
  59.  
  60. for _,e in pairs(enemies_controller.enemies) do
  61. e.y = e.y + 0.2
  62. end
  63.  
  64. for i,b in ipairs(player.bullets) do
  65. if b.y < -10 then
  66. table.remove(player.bullets, i)
  67. end
  68. b.y = b.y - 0.7
  69. end
  70. end
  71.  
  72. function love.draw()
  73. ---Draw the Player, kthxbye
  74. love.graphics.setColor(0, 0, 255)
  75. love.graphics.rectangle("fill", player.x, player.y, 20, 20)
  76.  
  77. ---Draw the Enemy, kthxbye
  78. love.graphics.setColor(255,0,0)
  79. for _,e in pairs(enemies_controller.enemies) do
  80. love.graphics.rectangle("fill", e.x, e.y, 20, 20)
  81. end
  82.  
  83. ---Draw bullets, kthxbye
  84. love.graphics.setColor(255,255,255)
  85. for _,b in pairs(player.bullets) do
  86. love.graphics.rectangle("fill", b.x, b.y, 10, 10)
  87. end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement