Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. local image
  2.  
  3. function love.load()
  4.  
  5. anim8 = require('anim8-master/anim8')
  6. cameraFile = require("hump-master/camera")
  7. cam = cameraFile()
  8.  
  9. image = love.graphics.newImage('sprites/portal2.png')
  10. local portalgrid = anim8.newGrid(48, 48, 432, 48)
  11. animation = anim8.newAnimation(portalgrid('1-8', 1), .09)
  12.  
  13. sprites = {}
  14. sprites.player = love.graphics.newImage('sprites/testingplayer.png')
  15. sprites.bullet = love.graphics.newImage('sprites/testingbullet.png')
  16. sprites.zombie = love.graphics.newImage('sprites/testingzombie.png')
  17. sprites.background = love.graphics.newImage('sprites/background.png')
  18.  
  19. player = {}
  20. player.x = love.graphics.getWidth()/2
  21. player.y = love.graphics.getHeight()/2
  22. player.walk_speed = 100
  23. player.sprint_speed = player.walk_speed * 1.5
  24. player.speed = 180
  25.  
  26. zombies = {}
  27. bullets = {}
  28.  
  29. gameState = 1
  30. maxTime = 2
  31. timer = maxTime
  32. score = 0
  33.  
  34. myFont = love.graphics.newFont(40)
  35. end
  36.  
  37. function love.update(dt)
  38.  
  39. animation:update(dt)
  40.  
  41. -- cam:lookAt(player.x, player.y) -- Optional Camera Attach to the player
  42.  
  43. if gameState == 2 then
  44.  
  45. if love.keyboard.isDown("lshift")then
  46. player.speed = player.sprint_speed
  47. else
  48. player.speed = player.walk_speed
  49. end
  50.  
  51. if love.keyboard.isDown("s") and player.y < love.graphics.getHeight() then
  52. player.y = player.y + player.speed * dt
  53. end
  54.  
  55. if love.keyboard.isDown("w") and player.y > 0 then
  56. player.y = player.y - player.speed * dt
  57. end
  58.  
  59. if love.keyboard.isDown("a") and player.x > 0 then
  60. player.x = player.x - player.speed * dt
  61. end
  62.  
  63. if love.keyboard.isDown("d") and player.x < love.graphics.getWidth() then
  64. player.x = player.x + player.speed * dt
  65. end
  66. end
  67.  
  68. for i,z in ipairs(zombies) do
  69. z.x = z.x + math.cos(zombie_player_angle(z)) * z.speed * dt
  70. z.y = z.y + math.sin(zombie_player_angle(z)) * z.speed * dt
  71. z.animation:update(dt)
  72.  
  73. if distanceBetween(z.x, z.y, player.x, player.y) < 30 then
  74. for i,z in ipairs(zombies) do
  75. zombies[i] = nil
  76. gameState = 1
  77. player.x = love.graphics.getWidth()/2
  78. player.y = love.graphics.getHeight()/2
  79. end
  80. end
  81. end
  82.  
  83. for i,b in ipairs(bullets) do
  84. b.x = b.x + math.cos(b.direction) * b.speed * dt
  85. b.y = b.y + math.sin(b.direction) * b.speed * dt
  86. end
  87.  
  88. for i=#bullets, 1, -1 do
  89. local b = bullets[i]
  90. if b.x < 0 or b.y < 0 or b.x > love.graphics.getWidth() or b.y > love.graphics.getHeight() then
  91. table.remove(bullets, i)
  92. end
  93. end
  94.  
  95. for i,z in ipairs(zombies) do
  96. for j, b in ipairs(bullets) do
  97. if distanceBetween(z.x, z.y, b.x, b.y) < 20 then
  98. z.dead = true
  99. b.dead = true
  100. score = score + 1
  101. end
  102. end
  103. end
  104.  
  105. for i=#zombies,1,-1 do
  106. local z = zombies[i]
  107. if z.dead == true then
  108. table.remove(zombies, i)
  109. end
  110. end
  111.  
  112. for i=#bullets, 1, -1 do
  113. local b = bullets[i]
  114. if b.dead == true then
  115. table.remove(bullets, i)
  116. end
  117. end
  118.  
  119. if gameState == 2 then
  120. timer = timer - dt
  121. if timer <= 0 then
  122. spawnZombie()
  123. maxTime = maxTime * 0.95
  124. timer = maxTime
  125. end
  126. end
  127. end
  128.  
  129. function love.draw()
  130. cam:attach()
  131.  
  132. love.graphics.draw(sprites.background, 0, 0)
  133.  
  134. if gameState == 1 then
  135. love.graphics.setFont(myFont)
  136. love.graphics.printf("Click anywhere to begin", 0, 50, love.graphics.getWidth(), "center")
  137. end
  138.  
  139. love.graphics.printf("Score: " .. score, 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), "center")
  140.  
  141. love.graphics.draw(sprites.player, player.x, player.y, nil, nil, nil, sprites.player:getWidth()/2, sprites.player:getHeight()/2)
  142.  
  143. for i,z in ipairs(zombies) do
  144. love.graphics.draw(sprites.zombie, z.x, z.y, nil, nil, nil, sprites.zombie:getWidth()/2, sprites.zombie:getHeight()/2)
  145. z.animation:draw(image,z.x-25, z.y-24)
  146. end
  147.  
  148. for i,b in ipairs(bullets) do
  149. love.graphics.draw(sprites.bullet, b.x, b.y, nil, nil, nil, sprites.bullet:getWidth()/2, sprites.bullet:getHeight()/2)
  150. end
  151. cam:detach()
  152. end
  153.  
  154. function player_mouse_angle()
  155. return math.atan2(player.y - love.mouse.getY(), player.x - love.mouse.getX()) + math.pi
  156. end
  157.  
  158. function zombie_player_angle(enemy)
  159. return math.atan2(player.y - enemy.y,player.x - enemy.x)
  160. end
  161.  
  162. function spawnZombie()
  163. zombie = {}
  164. zombie.x = 0
  165. zombie.y = 0
  166. zombie.speed = 140
  167. zombie.dead = false
  168. zombie.portalgrid = anim8.newGrid(48, 48, 432, 48)
  169. zombie.animation = anim8.newAnimation(zombie.portalgrid('1-8', 1), .09)
  170.  
  171. local side = math.random(1, 4)
  172.  
  173. if side == 1 then
  174. zombie.x = 200
  175. zombie.y = math.random(300, love.graphics.getHeight())
  176. elseif side == 2 then
  177. zombie.x = math.random(30, love.graphics.getWidth())
  178. zombie.y = 200
  179. elseif side == 3 then
  180. zombie.x = love.graphics.getWidth() - 200
  181. zombie.y = math.random(-30, love.graphics.getHeight())
  182. else
  183. zombie.x = math.random(-30, love.graphics.getWidth())
  184. zombie.y = love.graphics.getHeight() - 200
  185. end
  186.  
  187. table.insert(zombies, zombie)
  188. end
  189.  
  190. function spawnBullet()
  191. bullet = {}
  192. bullet.x = player.x
  193. bullet.y = player.y
  194. bullet.speed = 500
  195. bullet.direction = player_mouse_angle()
  196. bullet.dead = false
  197.  
  198. table.insert(bullets, bullet)
  199. end
  200.  
  201. function love.mousepressed(x, y, b, istouch)
  202. if b == 1 and gameState == 2 then
  203. spawnBullet()
  204. end
  205.  
  206. if gameState == 1 then
  207. gameState = 2
  208. maxTime = 2
  209. time = maxTime
  210. score = 0
  211. end
  212. end
  213.  
  214. function distanceBetween(x1, y1, x2, y2)
  215. return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement