Advertisement
Guest User

Relancer_Jeu

a guest
Jul 22nd, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.21 KB | None | 0 0
  1. -- Empèche Love de filtrer les contours des images quand elles sont redimentionnées
  2. -- Indispensable pour du pixel art
  3. love.graphics.setDefaultFilter("nearest")
  4.  
  5. -- Cette ligne permet de déboguer pas à pas dans ZeroBraneStudio
  6. if arg[#arg] == "-debug" then require("mobdebug").start() end
  7.  
  8. heros = {}
  9.  
  10. math.randomseed(love.timer.getTime())
  11.  
  12. -- Listes d'éléments
  13. liste_sprites = {}
  14. liste_tirs = {}
  15. liste_aliens = {}
  16.  
  17. -- Niveau 16x12
  18. niveau = {}
  19. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  20. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  21. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  22. table.insert(niveau, { 0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0 })
  23. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  24. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  25. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  26. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  27. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  28. table.insert(niveau, { 0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0 })
  29. table.insert(niveau, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 })
  30. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  31. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  32. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  33. table.insert(niveau, { 0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0 })
  34. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  35. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  36. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  37. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  38. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  39. table.insert(niveau, { 0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0 })
  40. table.insert(niveau, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 })
  41. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  42. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  43. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  44. table.insert(niveau, { 0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0 })
  45. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  46. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  47. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  48. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  49. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  50. table.insert(niveau, { 0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0 })
  51. table.insert(niveau, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 })
  52. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  53. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  54. table.insert(niveau, { 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 })
  55. table.insert(niveau, { 0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0 })
  56. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  57. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  58. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  59. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  60. table.insert(niveau, { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0 })
  61. table.insert(niveau, { 0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0 })
  62. table.insert(niveau, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 })
  63. table.insert(niveau, { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 })
  64.  
  65. -- Camera
  66. camera = {}
  67. camera.y = 0
  68. camera.vitesse = 1
  69.  
  70. -- Ecran courant
  71. ecran_courant = "menu"
  72. victoire = false
  73. timerVictoire = 0
  74.  
  75. -- Images des tuiles
  76. imgTuiles = {}
  77. local n
  78. for n=1,3 do
  79.   imgTuiles[n] = love.graphics.newImage("images/tuile_"..n..".png")
  80. end
  81.  
  82. -- Images des explosions
  83. imgExplosion = {}
  84. for n=1,5 do
  85.   imgExplosion[n] = love.graphics.newImage("images/explode_"..n..".png")
  86. end
  87.  
  88.  
  89. imgMenu = love.graphics.newImage("images/menu.jpg")
  90. imgVictoire = love.graphics.newImage("images/victory.jpg")
  91. imgGameOver = love.graphics.newImage("images/gameover.jpg")
  92. sonShoot = love.audio.newSource("sons/shoot.wav","static")
  93. sonExplode = love.audio.newSource("sons/explode_touch.wav","static")
  94.  
  95.  
  96. function math.angle(x1,y1, x2,y2) return math.atan2(y2-y1, x2-x1) end
  97.  
  98.  
  99.  
  100.  
  101. function collide(a1, a2)
  102.  if (a1==a2) then return false end
  103.  local dx = a1.x - a2.x
  104.  local dy = a1.y - a2.y
  105.  if (math.abs(dx) < a1.image:getWidth()+a2.image:getWidth()) then
  106.   if (math.abs(dy) < a1.image:getHeight()+a2.image:getHeight()) then
  107.    return true
  108.   end
  109.  end
  110.  return false
  111. end
  112.  
  113. function CreeTir(pType, pNomImage, pX, pY, pVitesseX, pVitesseY)
  114.   local tir = CreeSprite(pNomImage, pX, pY)
  115.   tir.type = pType
  116.   tir.vx = pVitesseX
  117.   tir.vy = pVitesseY
  118.   table.insert(liste_tirs, tir)
  119.    
  120.   sonShoot:play()
  121. end
  122.  
  123. function CreeAlien(pType, pX, pY)
  124.  
  125.   local nomImage = ""
  126.   if pType == 1 then
  127.     nomImage = "enemy1"
  128.   elseif pType == 2 then
  129.     nomImage = "enemy2"
  130.   elseif pType == 3 then
  131.     nomImage = "tourelle"
  132.   elseif pType == 10 then
  133.     nomImage = "enemy3"
  134.   end
  135.  
  136.   local alien = CreeSprite(nomImage, pX, pY)
  137.  
  138.   alien.type = pType
  139.  
  140.   alien.endormi = true
  141.   alien.chronotir = 0
  142.  
  143.   if pType == 1 then
  144.     alien.vy = 2
  145.     alien.vx = 0
  146.     alien.energie = 1
  147.   elseif pType == 2 then
  148.     alien.vy = 2
  149.     local direction = math.random(1,2)
  150.     if direction == 1 then
  151.       alien.vx = 1
  152.     else
  153.       alien.vx = -1
  154.     end
  155.     alien.energie = 3
  156.   elseif pType == 3 then
  157.     alien.vx = 0
  158.     alien.vy = camera.vitesse
  159.     alien.energie = 5
  160.   elseif pType == 10 then
  161.     alien.vx = 0
  162.     alien.vy = camera.vitesse * 2
  163.     alien.energie = 20
  164.     alien.angle = 0
  165.   end
  166.  
  167.   table.insert(liste_aliens, alien)
  168. end
  169.  
  170. function CreeSprite(pNomImage, pX, pY)
  171.  
  172.   sprite = {}
  173.   sprite.x = pX
  174.   sprite.y = pY
  175.   sprite.supprime = false
  176.   sprite.image = love.graphics.newImage("images/"..pNomImage..".png")
  177.   sprite.l = sprite.image:getWidth()
  178.   sprite.h = sprite.image:getHeight()
  179.   sprite.frame = 1
  180.   sprite.listeFrames = {}
  181.   sprite.maxFrame = 1
  182.  
  183.   table.insert(liste_sprites, sprite)
  184.  
  185.   return sprite
  186.  
  187. end
  188.  
  189. function CreeExplosion(pX, pY)
  190.   local NouvExplosion = CreeSprite("explode_1", pX, pY)
  191.   NouvExplosion.listeFrames = imgExplosion
  192.   NouvExplosion.maxFrame = 5
  193.  
  194. end
  195.  
  196.  
  197. function love.load()
  198.  
  199.   love.window.setMode(1024,768)
  200.   love.window.setTitle("Coucou Marie <3")
  201.  
  202.   largeur = love.graphics.getWidth()
  203.   hauteur = love.graphics.getHeight()
  204.  
  205.   heros = CreeSprite("heros", largeur/2, hauteur/2)
  206.  
  207.   DemarreJeu()
  208.    
  209. end
  210.  
  211. function DemarreJeu()
  212.   heros.x = largeur/2
  213.   heros.y = hauteur - (heros.h*2)
  214.  
  215.   -- Création des aliens
  216.   local ligne=4
  217.   CreeAlien(1, largeur/2, -(64/2)-(64*(ligne-1)))
  218.   ligne = 10
  219.   CreeAlien(2, largeur/2, -(64/2)-(64*(ligne-1)))
  220.   ligne = 11
  221.   CreeAlien(3, 3*64, -(64/2)-(64*(ligne-1)))
  222.   ligne = 20
  223.   CreeAlien(10, largeur/2, -(64/2)-(64*(ligne-1)))
  224.  
  225.   -- RAZ de la Caméra
  226.   camera.y = 0
  227.  
  228. end
  229.  
  230. function updateJeu()
  231. camera.y = camera.y + camera.vitesse
  232.    
  233.     local n
  234.  
  235.     -- traitement des tirs
  236.     for n=#liste_tirs,1,-1 do
  237.       local tir = liste_tirs[n]
  238.       tir.x = tir.x + tir.vx
  239.       tir.y = tir.y + tir.vy
  240.      
  241.       -- Vérifie si on touche le heros
  242.       if tir.type == "alien" then
  243.         if collide(heros,tir) then
  244.           print("Boom je suis touché !!")
  245.           tir.supprime = true
  246.           table.remove(liste_tirs, n)
  247.           ecran_courant = "gameover"
  248.         end
  249.       end
  250.      
  251.       if tir.type == "heros" then
  252.         local nAlien
  253.         for nAlien=#liste_aliens,1,-1 do
  254.           local alien = liste_aliens[nAlien]
  255.           if alien.endormi == false then
  256.             if collide(tir,alien) then
  257.               CreeExplosion(tir.x, tir.y)
  258.               tir.supprime = true
  259.               table.remove(liste_tirs, n)
  260.               alien.energie = alien.energie - 1
  261.               sonExplode:play()
  262.               if alien.energie <= 0 then
  263.                 local nExplosion
  264.                 for nExplosion =1,5 do
  265.                   CreeExplosion(alien.x + math.random(-10, 10), alien.y + math.random(-10, 10))
  266.                 end
  267.                 if alien.type == 10 then
  268.                   victoire = true
  269.                   timerVictoire = 200
  270.                   for nExplosion=1,20 do
  271.                     CreeExplosion(alien.x + math.random(-100,100), alien.y + math.random(-100,100))
  272.                   end
  273.                  
  274.                 end
  275.                 alien.supprime = true
  276.                 table.remove(liste_aliens, nAlien)
  277.               end
  278.             end
  279.           end
  280.         end
  281.       end
  282.      
  283.       -- Vérifier si le tir n'est pas sorti de l'écran
  284.       if (tir.y < 0 or tir.y > hauteur) and tir.supprime == false then
  285.        
  286.         -- Marque le sprite pour le supprimer plus tard
  287.         tir.supprime = true
  288.         table.remove(liste_tirs, n)
  289.        
  290.       end
  291.      
  292.     end
  293.    
  294.     -- traitement des aliens
  295.     for n=#liste_aliens,1,-1 do
  296.       local alien = liste_aliens[n]
  297.      
  298.       if alien.y > 0 then
  299.         alien.endormi = false
  300.       end
  301.      
  302.       if alien.endormi == false then
  303.         alien.x = alien.x + alien.vx
  304.         alien.y = alien.y + alien.vy
  305.        
  306.         if alien.type == 1 or alien.type == 2 then
  307.           alien.chronotir = alien.chronotir - 1
  308.           if alien.chronotir <= 0 then
  309.             alien.chronotir = math.random(60,100)
  310.             CreeTir("alien","laser2",alien.x,alien.y,0,10)
  311.           end
  312.         elseif alien.type == 3 then
  313.           alien.chronotir = alien.chronotir - 1
  314.           if alien.chronotir <= 0 then
  315.             alien.chronotir = 25
  316.             local vx,vy
  317.             local angle
  318.             angle = math.angle(alien.x,alien.y,heros.x,heros.y)
  319.             vx = 10 * math.cos(angle)
  320.             vy = 10 * math.sin(angle)
  321.             CreeTir("alien","laser2",alien.x,alien.y,vx,vy)
  322.           end
  323.         elseif alien.type == 10 then
  324.           if alien.y >= hauteur/3 then
  325.             alien.y = hauteur/3
  326.           end
  327.           alien.chronotir = alien.chronotir - 1
  328.           if alien.chronotir <= 0 then
  329.             alien.chronotir = 15
  330.             local vx,vy
  331.             alien.angle = alien.angle + 0.5
  332.             vx = 10 * math.cos(alien.angle)
  333.             vy = 10 * math.sin(alien.angle)
  334.             CreeTir("alien","laser2",alien.x,alien.y,vx,vy)
  335.           end
  336.          
  337.         end
  338.        
  339.        
  340.       else
  341.         alien.y = alien.y + camera.vitesse
  342.       end
  343.      
  344.       if alien.y > hauteur then
  345.         alien.supprime = true
  346.         table.remove(liste_aliens, n)
  347.       end
  348.     end
  349.    
  350.     -- Traitement et purge des sprites à supprimer
  351.     for n=#liste_sprites,1,-1 do
  352.       local sprite = liste_sprites[n]
  353.       -- Le sprite est il animé ?
  354.       if sprite.maxFrame > 1 then
  355.         sprite.frame = sprite.frame + 0.2
  356.         if math.floor(sprite.frame) > sprite.maxFrame then
  357.           sprite.supprime = true
  358.         else
  359.           sprite.image = sprite.listeFrames[math.floor(sprite.frame)]
  360.         end
  361.       end
  362.       if sprite.supprime == true then
  363.         table.remove(liste_sprites,n)
  364.       end
  365.     end
  366.  
  367.     if love.keyboard.isDown("right") and heros.x < largeur then
  368.       heros.x = heros.x + 4
  369.     end
  370.  
  371.     if love.keyboard.isDown("left") and heros.x > 0 then
  372.       heros.x = heros.x - 4
  373.     end
  374.    
  375.     if love.keyboard.isDown("up") and heros.y > 0 then
  376.       heros.y = heros.y - 4
  377.     end
  378.  
  379.     if love.keyboard.isDown("down") and heros.y < hauteur then
  380.       heros.y = heros.y + 4
  381.     end
  382.     if victoire == true then
  383.       timerVictoire = timerVictoire - 1
  384.       if timerVictoire == 0 then
  385.         ecran_courant = "victoire"
  386.       end
  387.     end
  388. end
  389.  
  390. function updateMenu()
  391.  
  392. end
  393.  
  394. function drawJeu()
  395.   local nbLignes = #niveau
  396.   local ligne,colonne
  397.   local x,y
  398.  
  399.   x=0
  400.   y=(0 - 64) + camera.y
  401.   for ligne=nbLignes,1,-1 do
  402.     for colonne=1,16 do
  403.       -- Dessine la tuile
  404.       local tuile = niveau[ligne][colonne]
  405.       if tuile > 0 then
  406.         love.graphics.draw(imgTuiles[tuile],x,y,0,2,2)
  407.       end
  408.       x = x + 64
  409.     end
  410.     x = 0
  411.     y = y - 64
  412.   end
  413.  
  414.   local n
  415.   for n=1,#liste_sprites do
  416.     local s = liste_sprites[n]
  417.     love.graphics.draw(s.image, s.x, s.y, 0, 2, 2, s.l/2, s.h/2)
  418.   end
  419.  
  420.   love.graphics.print("Nombre de tirs = "..#liste_tirs.." Nombre de sprites = "..#liste_sprites.." Nombre d'aliens = "..#liste_aliens,0,0)  
  421. end
  422.  
  423. function love.update(dt)
  424.   if ecran_courant == "jeu" then
  425.     updateJeu()
  426.   elseif ecran_courant == "menu" then
  427.     updateMenu()
  428.   elseif ecran_courant == "gameover" then
  429.     DemarreJeu()
  430.    
  431.    
  432.   end
  433.  
  434. end
  435.  
  436. function drawMenu()
  437.   love.graphics.draw(imgMenu,0,0)
  438.  
  439. end
  440.  
  441. function drawGameOver()
  442.   love.graphics.draw(imgGameOver,0,0)
  443. end
  444.  
  445. function drawVictoire()
  446.   love.graphics.draw(imgVictoire,0,0)
  447. end
  448.  
  449. function love.draw()
  450.   if ecran_courant == "jeu" then
  451.     drawJeu()
  452.   elseif ecran_courant == "menu" then
  453.     drawMenu()
  454.   elseif ecran_courant == "gameover" then
  455.     drawGameOver()
  456.     DemarreJeu()
  457.    
  458.    
  459.    
  460.    
  461.   elseif ecran_courant == "victoire" then
  462.     drawVictoire()
  463.    
  464.   end
  465.  
  466.  
  467.  
  468.    
  469. end
  470.  
  471. function love.keypressed(key)
  472.   if ecran_courant == "jeu" then
  473.     if key == "space" then
  474.       CreeTir("heros", "laser1", heros.x, heros.y - (heros.h*2)/2, 0, -10)
  475.     end
  476.  
  477.   elseif ecran_courant == "menu" then
  478.     if key == "space" then
  479.       ecran_courant = "jeu"
  480.     end
  481.   elseif ecran_courant == "gameover" then
  482.     if key == "space" then
  483.       DemarreJeu()
  484.     end  
  485.  
  486.      
  487.  
  488.      
  489.      
  490.        
  491.    
  492.   end
  493.  
  494. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement