Advertisement
Agent_Silence

love2d game updated 1.5

Apr 11th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.68 KB | None | 0 0
  1.  
  2. createEnemyTimerMax = 0.6
  3. createEnemyTimer = createEnemyTimerMax
  4.  
  5. pauseTimerMax = 0.1
  6. pauseTimer = pauseTimerMax
  7.  
  8. saved = false
  9. isAlive = false
  10. isPaused = false
  11. score = 0
  12. scores = {}
  13.  
  14. cheat1ac = false
  15. cheat2ac = false
  16.  
  17. canShoot = true
  18. canShootTimerMax = 0.2
  19. canShootTimer = canShootTimerMax
  20.  
  21. bulletImg = nil
  22. lrDir = 0
  23. udDir = 0
  24.  
  25. bullets = {}
  26. enemies = {}
  27.  
  28. themes = {
  29. love.audio.newSource("assets/sounds/repost.mp3","stream"),
  30. love.audio.newSource("assets/sounds/sbq.mp3","stream"),
  31. love.audio.newSource("assets/sounds/stars.mp3","stream"),
  32. love.audio.newSource("assets/sounds/swing.mp3","stream"),
  33. love.audio.newSource("assets/sounds/wii.mp3","stream")}
  34.  
  35. theme = math.random(1,#themes)
  36.  
  37. particle = {}
  38.  
  39. function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  40.   return x1 < x2+w2 and
  41.          x2 < x1+w1 and
  42.          y1 < y2+h2 and
  43.          y2 < y1+h1
  44. end
  45.  
  46. function formatTable()
  47.     table.sort(scores,function(a,b) return a>b end)
  48.     return "scores = {"..table.concat(scores, ", ").."}"
  49. end
  50.  
  51. function love.load(arg)
  52.     if love.filesystem.exists("save.nibba") then
  53.         local run = love.filesystem.load("save.nibba")
  54.         run()
  55.     end
  56.  
  57.     player = { x = 200, y = 200, speed = 450, img = nil}
  58.     main_img = love.graphics.newImage('assets/sprites/Player_John.png')
  59.     player.img = main_img
  60.     player.x = (love.graphics.getWidth()/2)-(player.img:getWidth()/2)
  61.     player.y = (love.graphics.getHeight()/2)-(player.img:getWidth()/2)
  62.     bltrecover = love.graphics.newImage("assets/sprites/L.png")
  63.     bulletImg = love.graphics.newImage("assets/sprites/L.png")
  64.     jooshrecover = love.graphics.newImage("assets/sprites/Joosh.jpg")
  65.     scndImg = love.graphics.newImage("assets/sprites/Player_JohnSmall.png")
  66.     enemyImg = {
  67.         love.graphics.newImage("assets/sprites/Danny.jpg"),
  68.         love.graphics.newImage("assets/sprites/Jerrod.jpg"),
  69.         love.graphics.newImage("assets/sprites/Joosh.jpg"),
  70.         love.graphics.newImage("assets/sprites/Agent.jpg"),}
  71.  
  72.     hit = {
  73.         love.audio.newSource("assets/sounds/funny.wav","static"),
  74.         love.audio.newSource("assets/sounds/funny2.wav","static"),
  75.         love.audio.newSource("assets/sounds/funny3.wav","static"),
  76.         love.audio.newSource("assets/sounds/funny4.wav","static"),
  77.         love.audio.newSource("assets/sounds/funny5.wav","static"),
  78.         love.audio.newSource("assets/sounds/funny6.wav","static"),
  79.         love.audio.newSource("assets/sounds/funny7.wav","static"),
  80.         love.audio.newSource("assets/sounds/funny8.wav","static"),
  81.         love.audio.newSource("assets/sounds/funny9.wav","static")}
  82.  
  83.     local img = bulletImg
  84.  
  85.  
  86.  
  87.     love.audio.setVolume(0.7)
  88. end
  89.  
  90.  
  91. function love.update(dt)
  92.  
  93.     if love.keyboard.isDown('escape') then
  94.         if saved == false then
  95.             table.insert(scores,score)
  96.             saved = true
  97.         end
  98.         if love.filesystem.exists("save.nibba") then --sorry i've never worked with filesystems before. just starting out.
  99.             love.filesystem.write("save.nibba",formatTable())
  100.         else
  101.             local file = love.filesystem.newFile("save.nibba")
  102.             file:open("w")
  103.             file:write(formatTable())
  104.             file:close()
  105.         end
  106.         love.event.push('quit')
  107.     end
  108.     pauseTimer = pauseTimer - (1*dt)
  109.     if love.keyboard.isDown("space") and isPaused == false and pauseTimer < 0 and isAlive then
  110.         isPaused = true
  111.         love.audio.pause(themes[theme])
  112.         pauseTimer = pauseTimerMax
  113.     elseif love.keyboard.isDown("space") and isPaused == true and pauseTimer < 0 and isAlive then
  114.         isPaused = false
  115.         love.audio.resume(themes[theme])
  116.         pauseTimer = pauseTimerMax
  117.     end
  118.  
  119.     if love.keyboard.isDown("j") and love.keyboard.isDown("o") and love.keyboard.isDown("h") and love.keyboard.isDown("n") and cheat1ac == false then
  120.         bulletImg = scndImg
  121.         player.speed = 550
  122.         cheat1ac = true
  123.     end
  124.  
  125.     if love.keyboard.isDown("d") and love.keyboard.isDown("a") and love.keyboard.isDown("n") and love.keyboard.isDown("k") and cheat2ac == false then
  126.         cheat2ac = true
  127.         player.img = enemyImg[3]
  128.         enemyImg[3] = main_img
  129.     end
  130.  
  131.     if isPaused == false then
  132.  
  133.     canShootTimer = canShootTimer - (1*dt)
  134.     if canShootTimer < 0 then
  135.         canShoot = true
  136.     end
  137.  
  138.     if love.mouse.isDown(1) then
  139.     if isAlive and canShoot then
  140.         local x,y = love.mouse.getPosition()
  141.         local startX = player.x + ((player.img:getWidth() / 2) - bulletImg:getWidth()/2)
  142.         local startY = player.y + ((player.img:getHeight() / 2) - bulletImg:getHeight()/2)
  143.         local mouseX = x - bulletImg:getWidth()/2
  144.         local mouseY = y - bulletImg:getHeight()/2
  145.  
  146.         local angle = math.atan2((mouseY - startY), (mouseX - startX))
  147.  
  148.         local bulletDx = score > 60 and 900 * math.cos(angle) or 600 * math.cos(angle)
  149.         local bulletDy = score > 60 and 900 * math.sin(angle) or 600 * math.sin(angle)
  150.         local bulletDz = nil
  151.         local bulletDw = nil
  152.         if cheat1ac then
  153.             bulletDz = score > 60 and 900 * math.cos(angle+1.57079633) or 600 * math.cos(angle+1.57079633 )
  154.             bulletDw = score > 60 and 900 * math.sin(angle+1.57079633) or 600 * math.sin(angle+1.57079633 )
  155.         end
  156.         table.insert(bullets, {x = startX, y = startY, img = bulletImg, dx = bulletDx, dy = bulletDy})
  157.         if cheat1ac == true then
  158.             table.insert(bullets, {x = startX, y = startY, img = bulletImg, dx = 0-bulletDx, dy = 0-bulletDy})
  159.             table.insert(bullets, {x = startX, y = startY, img = bulletImg, dx = bulletDz, dy = bulletDw})
  160.             table.insert(bullets, {x = startX, y = startY, img = bulletImg, dx = 0-bulletDz, dy = 0-bulletDw})
  161.         end
  162.         canShoot = false
  163.         canShootTimer = canShootTimerMax
  164.     end
  165.     end
  166.  
  167.  
  168.     if love.keyboard.isDown('left','a') then
  169.         if player.x > 0 then -- binds us to the map
  170.             player.x = player.x - (player.speed*dt)
  171.         end
  172.         lrDir = -1
  173.     elseif love.keyboard.isDown('right','d') then
  174.         if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
  175.             player.x = player.x + (player.speed*dt)
  176.         end
  177.         lrDir = 1
  178.     end
  179.  
  180.     if love.keyboard.isDown('up','w') then
  181.         if player.y > 0 then
  182.             player.y = player.y - (player.speed*dt)
  183.         end
  184.         udDir = -1
  185.     elseif love.keyboard.isDown('down','s') then
  186.         if player.y < (love.graphics.getHeight() - player.img:getHeight()) then
  187.             player.y = player.y + (player.speed*dt)
  188.         end
  189.         udDir = 1
  190.     end
  191.    
  192.     for i, bullet in ipairs(bullets) do
  193.         if cheat2ac and #enemies > 0  then
  194.             local closest = {dist = 100000,index = 1}
  195.             for i,v in ipairs(enemies) do
  196.                 if ((v.x-bullet.x)^2+(v.y-bullet.y)^2)^0.5 < closest.dist then
  197.                     closest = {dist = ((v.x-bullet.x)^2+(v.y-bullet.y)^2)^0.5, index = i}
  198.                 end
  199.             end
  200.             local angle = math.atan2((enemies[closest.index].y - bullet.y), (enemies[closest.index].x - bullet.x))
  201.             local bulletDx = score > 60 and 900 * math.cos(angle) or 600 * math.cos(angle)
  202.             local bulletDy = score > 60 and 900 * math.sin(angle) or 600 * math.sin(angle)
  203.             bullet.x = bullet.x + (bulletDx * dt)
  204.             bullet.y = bullet.y + (bulletDy * dt)
  205.         else
  206.             bullet.x = bullet.x + (bullet.dx * dt)
  207.             bullet.y = bullet.y + (bullet.dy * dt)
  208.         end
  209.  
  210.         if bullet.y+ bullet.img:getHeight() < 0 or bullet.y > (love.graphics.getHeight() + bullet.img:getHeight()) or bullet.x+ bullet.img:getWidth() < 0 or bullet.x > (love.graphics.getWidth() + bullet.img:getWidth()) then -- remove bullets when they pass off the screen
  211.             table.remove(bullets, i)
  212.         end
  213.     end
  214.  
  215.    
  216.     for i, enemy in ipairs(enemies) do
  217.         for j, bullet in ipairs(bullets) do
  218.             if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), bullet.x, bullet.y, bullet.img:getWidth(), bullet.img:getHeight()) then
  219.                 if score < 29 or cheat2ac then
  220.                     table.remove(bullets, j)
  221.                 end
  222.                 local tPartic = {
  223.                 p = love.graphics.newParticleSystem(bulletImg, 8),
  224.                 x= enemy.x,
  225.                 y = enemy.y}
  226.  
  227.                 tPartic.p:setSizes(0.8,0.7,0.6,0.5,0.4)
  228.                 tPartic.p:setSizeVariation(0.2)
  229.                 tPartic.p:setParticleLifetime(1, 3) -- Particles live at least 2s and at most 5s.
  230.                 tPartic.p:setLinearAcceleration(-50, 100, 50, 600) -- Randomized movement towards the bottom of the screen.
  231.                 tPartic.p:setSpeed(100,400)
  232.                 tPartic.p:setColors(255, 255, 255, 255, 255, 255, 255, 0) -- Fade to black.
  233.                 tPartic.p:setAreaSpread("normal", 15, 15 )
  234.                 tPartic.p:emit(8)
  235.                 table.insert(particle,tPartic)
  236.                 table.remove(enemies, i)
  237.                 if #particle >  7 then
  238.                     table.remove(particle,1)
  239.                 end
  240.                 score = score + 1
  241.                 if math.random(1,4)==0 then
  242.                 love.audio.play(hit[math.random(1,#hit)])
  243.                 end
  244.             end
  245.         end
  246.     end
  247.        
  248.     for i, enemy in ipairs(enemies) do
  249.         if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.img:getWidth(), player.img:getHeight()) and isAlive then
  250.             table.remove(enemies, i)
  251.             isAlive = false
  252.             saved = true
  253.             table.insert(scores, score)
  254.             table.sort(scores,function(a,b) return a>b end)
  255.         end
  256.  
  257.         local angle = math.atan2((player.y - enemy.y), (player.x - enemy.x))
  258.         local bulletDx = score > 29 and 350 * math.cos(angle) or 250 * math.cos(angle)
  259.         local bulletDy = score > 29 and 350 * math.sin(angle) or 250 * math.sin(angle)
  260.  
  261.         enemy.x = enemy.x + (bulletDx * dt)
  262.         enemy.y = enemy.y + (bulletDy * dt)
  263.         if isAlive == false then
  264.             enemies = {}
  265.         end
  266.     end
  267.    
  268.     createEnemyTimer = createEnemyTimer - (1 * dt)
  269.     if createEnemyTimer < 0 and isAlive then
  270.         createEnemyTimer = createEnemyTimerMax
  271.  
  272.         -- Create an enemy
  273.         yopt = {math.random(love.graphics.getHeight(),love.graphics.getHeight()+70),math.random(-70,0)}
  274.         xRand = math.random(-70,love.graphics.getWidth()+70)
  275.         yRand = (xRand < 0 or xRand > love.graphics.getWidth()) and math.random(0,love.graphics.getHeight()) or yopt[math.random(1,2)]
  276.         newEnemy = { x = xRand, y = yRand, img = enemyImg[math.random(1,#enemyImg)] }
  277.         table.insert(enemies, newEnemy)
  278.     end
  279.  
  280.        
  281.    
  282.    
  283.     if score == 60 then
  284.         createEnemyTimerMax = 0.45
  285.     elseif score == 90 then
  286.         canShootTimerMax = 0.1
  287.         createEnemyTimerMax = 0.3
  288.     end
  289.     for i,v in ipairs(particle) do
  290.         v.p:update(dt)
  291.     end
  292.  
  293.     if not isAlive and love.keyboard.isDown('r') then
  294.     -- remove all our bullets and enemies from screen
  295.     bullets = {}
  296.     enemies = {}
  297.  
  298.     -- reset timers
  299.     score = 0
  300.     canShootTimerMax = 0.2
  301.     canShootTimer = canShootTimerMax
  302.     createEnemyTimer = 0.6
  303.     createEnemyTimer = createEnemyTimerMax
  304.     cheat1ac = false
  305.     cheat2ac = false
  306.  
  307.     player.img = main_img
  308.     bulletImg = bltrecover
  309.     enemyImg[3] = jooshrecover
  310.  
  311.     -- move player back to default position
  312.     player.x = (love.graphics.getWidth()/2)-(player.img:getWidth()/2)
  313.     player.y = (love.graphics.getHeight()/2)-(player.img:getWidth()/2)
  314.  
  315.     -- reset our game state
  316.     saved = false
  317.     isAlive = true
  318.     isPaused = false
  319.     love.audio.rewind(themes[theme])
  320.     love.audio.pause()
  321.     love.audio.setVolume(0.7)
  322.     theme = math.random(1,#themes)
  323.     love.audio.play(themes[theme])
  324.     end
  325. end
  326. end
  327.  
  328.  
  329. function love.draw(dt)
  330.     love.graphics.setColor(86,12,81)
  331.     love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),love.graphics.getHeight())
  332.     love.graphics.setColor(255,255,255)
  333.     love.graphics.print("Score "..score, love.graphics:getWidth()/2-30, 5)
  334.  
  335.     if isAlive then
  336.         love.graphics.draw(player.img, player.x, player.y)
  337.     else
  338.         love.graphics.print("Press 'R' to Start", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-100)
  339.         love.graphics.print("Top Scores", love.graphics:getWidth()/2-30, love.graphics:getHeight()/2-85)
  340.         for i=1,#scores > 10 and 10 or #scores do
  341.             love.graphics.print(i.." : "..scores[i],love.graphics:getWidth()/2-20, love.graphics:getHeight()/2-70+15*i)
  342.         end
  343.     end
  344.  
  345.     for i, bullet in ipairs(bullets) do
  346.         love.graphics.draw(bullet.img, bullet.x, bullet.y)
  347.     end
  348.  
  349.     --love.graphics.draw(psystem, love.graphics.getWidth()/2, love.graphics.getHeight()/2)
  350.     for i,v in ipairs(particle) do
  351.         love.graphics.draw(v.p,v.x,v.y)
  352.     end
  353.  
  354.     for i, enemy in ipairs(enemies) do
  355.         love.graphics.draw(enemy.img, enemy.x, enemy.y)
  356.     end
  357.     if isPaused == true then
  358.     love.graphics.print("Press Space to Unpause", love.graphics:getWidth()/2-75, love.graphics:getHeight()/2-10)
  359.     end
  360. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement