Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Cette ligne permet d'afficher des traces dans la console pendant l'éxécution
- io.stdout:setvbuf('no')
- -- Empèche Love de filtrer les contours des images quand elles sont redimentionnées
- -- Indispensable pour du pixel art
- love.graphics.setDefaultFilter("nearest")
- -- Cette ligne permet de déboguer pas à pas dans ZeroBraneStudio
- if arg[#arg] == "-debug" then require("mobdebug").start() end
- math.randomseed(love.timer.getTime())
- frame = {}
- frame.sprite = {}
- victoire = false
- victoireTimer = 0
- spacebarre = {}
- heros = {}
- arow = {}
- -- usine a sprite dans la liste sprites avec la fonction
- -- liste d'éléments
- liste_sprites = {}
- liste_tirs = {}
- liste_aliens = {}
- tileSheet = {}
- local n
- for n=1,3 do
- tileSheet[n] = love.graphics.newImage("images/tuile_"..n..".png")
- end
- spriteSheet = {}
- local s
- for s=1,5 do
- spriteSheet[s] = love.graphics.newImage("images/explode_"..s..".png")
- end
- --niveau 16*12
- niveau = {}
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,0})
- table.insert(niveau, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})
- table.insert(niveau, {0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0})
- table.insert(niveau, {0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0})
- table.insert(niveau, {0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0})
- table.insert(niveau, {0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
- table.insert(niveau, {0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,0})
- table.insert(niveau, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})
- table.insert(niveau, {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3})
- camera = {}
- camera.y = 0
- camera.vitesse = 1
- sonShoot = love.audio.newSource("sons/shoot.wav","static")
- sonExplode = love.audio.newSource("sons/explode_touch.wav","static")
- imgMenu = love.graphics.newImage("images/menu.jpg")
- imgGameOver = love.graphics.newImage("images/gameover.jpg")
- imgVictory = love.graphics.newImage("images/victory.jpg")
- ecrant_courrant = "menu"
- --fonction squellette de base--
- function updateMenu()
- end
- function updateJeu(dt)
- camera.y = camera.y + camera.vitesse
- local n --pour toutes les boucles
- --boucle pour les aliens
- for n=#liste_aliens,1,-1 do
- alien = liste_aliens[n]
- if alien.y > 0 then
- alien.endormi = false
- end
- if alien.endormi == false then
- alien.y = alien.y + alien.vy
- alien.x = alien.x + alien.vx
- alien.chronotir = alien.chronotir - 1
- if alien.type == 1 then
- if alien.chronotir <= 0 then
- CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,0,500,"enemy")
- alien.chronotir = math.random(50,90)
- end
- end
- if alien.type == 2 then
- if alien.chronotir <= 0 then
- CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,0,500,"enemy")
- alien.chronotir = math.random(40,60)
- end
- end
- if alien.type == 3 then --tourelle
- if alien.chronotir <= 0 then
- local angle
- angle = math.angle(alien.x,alien.y,heros.x,heros.y)
- CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,500*math.cos(angle),
- 500*math.sin(angle),"enemy")
- alien.chronotir = math.random(20,40)
- end
- end
- if alien.type == 4 then
- if alien.chronotir <= 0 then
- alien.angleTir = alien.angleTir + 0.5
- CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,500*math.cos(alien.angleTir),
- 500*math.sin(alien.angleTir),"enemy")
- alien.chronotir = 1
- end
- end
- else
- alien.y = alien.y + camera.vitesse
- end
- if alien.y > hauteur + alien.image:getHeight() or alien.x < 0 or alien.x > largeur then
- alien.supprime = true
- table.remove(liste_aliens,n)
- end
- end
- --lecture des tirs et mise en mouvement
- for n=#liste_tirs,1,-1 do
- local tir = liste_tirs[n]
- tir.y = tir.y + (tir.speedH * dt)
- tir.x = tir.x + (tir.speedW * dt)
- -- suppression du tir si hors ecran--
- if tir.y > hauteur or tir.y < 0 or tir.x > largeur or tir.x < 0 then
- tir.supprime = true
- table.remove(liste_tirs,n)
- end
- --collision tirs et hero--
- if tir.type == "enemy" then
- if collide(tir,heros) then
- tir.supprime = true
- table.remove(liste_tirs,n)
- heros.vie = heros.vie - 1
- if heros.vie <= 0 then
- ecrant_courrant = "gameover"
- end
- end
- end
- if tir.type == "allies" then
- local nAlien
- for nAlien=#liste_aliens,1,-1 do
- local alien = liste_aliens[nAlien]
- if collide(tir,alien) then
- if alien.endormi == false then
- CreeExplosion(tir.x,tir.y)
- print("alien hit")
- tir.supprime = true
- table.remove(liste_tirs,n)
- alien.energie = alien.energie - 1
- if alien.energie <= 0 then
- if alien.type == 4 then
- local nExplosion
- for nExplosion= 1,math.random (120,160) do
- CreeExplosionBoss(alien.x + math.random(-largeur/2,largeur/2), alien.y + math.random(-hauteur/3,hauteur/3*2))
- sonExplode:play()
- end
- end
- if alien.type ~= 4 then
- victoire = true
- victoireTimer = 500
- for nExplosion= 1,math.random (30,80) do
- CreeExplosion(alien.x + math.random(-30,30), alien.y + math.random(-30,30))
- sonExplode:play()
- end
- end
- alien.supprime = true
- table.remove(liste_aliens,nAlien)
- end
- end
- end
- end
- end
- end
- --traitement et suppression des sprites
- for n=#liste_sprites,1,-1 do
- local sprite = liste_sprites[n]
- -- spriteSheet ou pas ?--
- if sprite.maxFrame > 1 then
- sprite.frame = sprite.frame + 0.2
- if math.floor(sprite.frame) > sprite.maxFrame then
- sprite.supprime = true
- else
- sprite.image = sprite.listeFrames[math.floor(sprite.frame)]
- end
- end
- if sprite.supprime == true then
- table.remove(liste_sprites,n)
- end
- end
- --ici debug pour les fleches et mouvement vaiseau
- if love.keyboard.isDown("up") then
- arow.up.bool = true
- if heros.y > 0 + (heros.image:getHeight()) then
- heros.y = heros.y - (heros.speedH * dt)
- else
- heros.y = 0 + (heros.image:getHeight())
- end
- else
- arow.up.bool = false
- end
- if love.keyboard.isDown("right") then
- arow.right.bool = true
- if heros.x < largeur - (heros.image:getWidth()) then
- heros.x = heros.x + (heros.speedW * dt)
- else
- heros.x = largeur - (heros.image:getWidth())
- end
- else
- arow.right.bool = false
- end
- if love.keyboard.isDown("down") then
- arow.down.bool = true
- if heros.y < hauteur - (heros.image:getHeight()) then
- heros.y = heros.y + (heros.speedH * dt)
- else
- heros.y = hauteur - (heros.image:getHeight())
- end
- else
- arow.down.bool = false
- end
- if love.keyboard.isDown("left") then
- arow.left.bool = true
- if heros.x > 0 + (heros.image:getWidth()) then
- heros.x = heros.x - (heros.speedW * dt)
- else
- heros.x = 0 + (heros.image:getWidth())
- end
- else
- arow.left.bool = false
- end
- if love.keyboard.isDown("space") then
- spacebarre.bool = true
- else
- spacebarre.bool = false
- end
- if victoire == true then
- victoireTimer = victoireTimer - 1
- if victoireTimer <= 0 then
- ecrant_courrant = "victory"
- end
- end
- end
- function updateGameOver()
- end
- function updateVictory()
- end
- function drawMenu()
- love.graphics.draw(imgMenu,0,0)
- end
- function drawJeu()
- love.graphics.setColor(255,255,255)
- --dessin du niveau
- local l,c
- local x,y = 0,(0-64) + camera.y
- for l= #niveau,1,-1 do
- for c=1 , 16 do
- local id = niveau[l][c]
- if id ~= 0 then
- love.graphics.draw(tileSheet[id],x,y,0,2,2)
- end
- x = x + 64
- end
- x = 0
- y = y - 64
- end
- local n
- for n=1, #liste_sprites do
- sprite = liste_sprites[n]
- if sprite.bool == true then
- love.graphics.draw(sprite.image,sprite.x,sprite.y,math.rad(sprite.angle),sprite.width,sprite.height,sprite.image:getWidth()/2
- ,sprite.image:getHeight()/2)
- end
- end
- love.graphics.print("nombres de tirs : "..#liste_tirs.." nombres de sprites : "..#liste_sprites.." nombre d'aliens : "..#liste_aliens,0,0)
- --dessin de la barre hp heros--
- heros.initVie = 9
- if heros.vie >= heros.initVie/3 * 2 then
- love.graphics.setColor(51,255,0)
- love.graphics.rectangle("fill",heros.x - heros.image:getWidth(), heros.y + heros.image:getHeight(),heros.vie*6,5)
- elseif heros.vie < heros.initVie/3 * 2 and heros.vie >= heros.initVie/3 then
- love.graphics.setColor(255,102,0)
- love.graphics.rectangle("fill",heros.x - heros.image:getWidth(), heros.y + heros.image:getHeight(),heros.vie*6,5)
- elseif heros.vie < heros.initVie/3 then
- love.graphics.setColor(255,0,0)
- love.graphics.rectangle("fill",heros.x - heros.image:getWidth(), heros.y + heros.image:getHeight(),heros.vie*6,5)
- end
- love.graphics.setColor(255,255,255)
- end
- function drawGameOver()
- love.graphics.draw(imgGameOver,0,0)
- end
- function drawVictory()
- love.graphics.draw(imgVictory,0,0)
- end
- function math.angle(x1,y1, x2,y2) return math.atan2(y2-y1, x2-x1) end
- ----GESTION DES COLLISIONS---
- function collide(a1, a2)
- if (a1==a2) then return false end
- local dx = a1.x - a2.x
- local dy = a1.y - a2.y
- if (math.abs(dx) < a1.image:getWidth()+a2.image:getWidth()) then
- if (math.abs(dy) < a1.image:getHeight()+a2.image:getHeight()) then
- return true
- end
- end
- return false
- end
- --------------------------------------
- --CREATION TIR
- function CreeTir(pNomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeedW,pSpeedH,pType)
- tir = CreeSprite(pNomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeedW,pSpeedH)
- tir.type = pType
- table.insert(liste_tirs,tir)
- end
- --------------------------------------
- --CREATION ALIEN
- function CreeAlien(pType, pX, pY,pRotate,pScaleW,pScaleH,pBool,pSpeed)
- local nomImage = ""
- if pType == 1 then
- nomImage = "enemy1"
- elseif pType == 2 then
- nomImage = "enemy2"
- elseif pType == 3 then
- nomImage = "tourelle"
- elseif pType == 4 then
- nomImage = "enemy3"
- end
- local alien = CreeSprite(nomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeed)
- alien.endormi = true
- alien.chronotir = 0
- alien.type = pType
- if pType == 1 then
- alien.vy = 2
- alien.vx = 0
- alien.energie = 2
- elseif pType == 2 then
- alien.energie = 3
- alien.vy = 2
- local direction = math.random(1,2)
- if direction == 1 then
- alien.vx = 1
- else
- alien.vx = -1
- end
- elseif pType == 3 then -- tourelle
- alien.energie = 4
- alien.vy = camera.vitesse
- alien.vx = 0
- elseif pType == 4 then -- boss
- alien.energie = 8
- alien.vy = camera.vitesse*1.5
- alien.vx = 0
- alien.angleTir = 0
- end
- table.insert(liste_aliens,alien)
- end
- -----------------------------
- ------------CREER ANIMATION SPRITE-------
- --function CreeSpriteAnime(pNomSpriteSheet, pNomImage,pNbSprite)
- -- -- nom de la table du sprite sheet--
- -- local spriteSheet = {}
- -- local n
- -- for n=pNbSprite,1 do
- -- SpriteSheet[n] = CreeSprite("images/"..pNomImage.."_"..n..".png")
- function CreeSprite(pNomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeedW,pSpeedH,pVie)
- sprite = {}
- sprite.x = pX
- sprite.y = pY
- sprite.angle = pRotate
- sprite.width = pScaleW
- sprite.height = pScaleH
- sprite.bool = pBool
- sprite.speedW = pSpeedW
- sprite.speedH = pSpeedH
- sprite.supprime = false
- sprite.vie = pVie
- sprite.initVie = pVie
- if frame.sprite[pNomImage] == nil then
- frame.sprite[pNomImage] = love.graphics.newImage("images/"..pNomImage..".png")
- end
- sprite.image = frame.sprite[pNomImage]
- sprite.frame = 1
- sprite.listeFrames = {}
- sprite.maxFrame = 1
- table.insert(liste_sprites,sprite)
- return sprite
- end
- function CreeExplosion(pX,pY)
- local newExplosion = CreeSprite("explode_1",pX,pY,0,2,2,true)
- newExplosion.listeFrames = spriteSheet
- newExplosion.maxFrame = #spriteSheet
- end
- function CreeExplosionBoss(pX,pY)
- local newExplosion = CreeSprite("explode_1",pX,pY,0,8,8,true)
- newExplosion.listeFrames = spriteSheet
- newExplosion.maxFrame = #spriteSheet
- end
- function love.load()
- love.window.setMode(1024,768)
- largeur = love.graphics.getWidth()
- hauteur = love.graphics.getHeight()
- arow.up = CreeSprite("up",100,400,0,0.25,0.25,false)
- arow.right = CreeSprite("up",125,430,90,0.25,0.25,false)
- arow.down = CreeSprite("up",100,460,180,0.25,0.25,false)
- arow.left = CreeSprite("up",75,430,270,0.25,0.25,false)
- spacebarre = CreeSprite("espacebarre",120,530,0,2,2,false)
- DemarreJeu()
- end
- function DemarreJeu()
- heros = CreeSprite("heros",largeur/2,hauteur/2,0,2,2,true,500,500,9)
- heros.y = hauteur - heros.image:getHeight()*2
- local ligne = 4
- CreeAlien(1,largeur/2,(64/2)-(64*(ligne-1)),0,2,2,true)
- ligne = 9
- CreeAlien(2,largeur/3,(64/2)-(64*(ligne-1)),0,2,2,true)
- ligne = 12
- CreeAlien(3,3*64,(64/2)-(64*(ligne-1)),0,2,2,true)
- ligne = #niveau
- CreeAlien(4,largeur/2,(64/2)-(64*(ligne-1)),0,2,2,true)
- camera.y = 0
- end
- function love.update(dt)
- if ecrant_courrant == "jeu" then
- updateJeu(dt)
- elseif ecrant_courrant == "menu" then
- updateMenu()
- elseif ecrant_courrant == "gameover" then
- updateGameOver()
- elseif ecrant_courrant == "victory" then
- updateVictory()
- end
- end
- function love.draw()
- if ecrant_courrant == "jeu" then
- drawJeu()
- elseif ecrant_courrant == "menu" then
- drawMenu()
- elseif ecrant_courrant == "gameover" then
- drawGameOver()
- elseif ecrant_courrant == "victory" then
- drawVictory()
- end
- -- love.graphics.print(""..tostring(heros.vie),heros.x + heros.image:getWidth(), heros.y + heros.image:getHeight())
- -- love.graphics.print("vie heros = "..tostring(heros.vie))
- end
- function love.keypressed(key)
- if key == "space" then
- if ecrant_courrant == "jeu" then
- CreeTir("laser1",heros.x,heros.y - (heros.image:getHeight()*2)/2,0,2,2,true,0,-500,"allies")
- love.audio.play(sonShoot)
- else if ecrant_courrant == "menu" then
- ecrant_courrant = "jeu"
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement