Advertisement
GameCodeurStudent

Untitled

Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.20 KB | None | 0 0
  1. -- Cette ligne permet d'afficher des traces dans la console pendant l'éxécution
  2. io.stdout:setvbuf('no')
  3.  
  4. -- Empèche Love de filtrer les contours des images quand elles sont redimentionnées
  5. -- Indispensable pour du pixel art
  6. love.graphics.setDefaultFilter("nearest")
  7.  
  8. -- Cette ligne permet de déboguer pas à pas dans ZeroBraneStudio
  9. if arg[#arg] == "-debug" then require("mobdebug").start() end
  10.  
  11. math.randomseed(love.timer.getTime())
  12. frame = {}
  13. frame.sprite = {}
  14. victoire = false
  15. victoireTimer = 0
  16. spacebarre = {}
  17. heros = {}
  18. arow = {}
  19. -- usine a sprite dans la liste sprites avec la fonction
  20. -- liste d'éléments
  21. liste_sprites = {}
  22. liste_tirs = {}
  23. liste_aliens = {}
  24.  
  25. tileSheet = {}
  26. local n
  27. for n=1,3 do
  28. tileSheet[n] = love.graphics.newImage("images/tuile_"..n..".png")
  29. end
  30.  
  31. spriteSheet = {}
  32. local s
  33. for s=1,5 do
  34. spriteSheet[s] = love.graphics.newImage("images/explode_"..s..".png")
  35. end
  36.  
  37.  
  38. --niveau 16*12
  39. niveau = {}
  40. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  41. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  42. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  43. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  44. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  45. table.insert(niveau, {0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,0})
  46. table.insert(niveau, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})
  47. table.insert(niveau, {0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0})
  48. table.insert(niveau, {0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0})
  49. table.insert(niveau, {0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0})
  50. table.insert(niveau, {0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0})
  51. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  52. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  53. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  54. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  55. table.insert(niveau, {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0})
  56. table.insert(niveau, {0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,0})
  57. table.insert(niveau, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})
  58. table.insert(niveau, {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3})
  59.  
  60.  
  61. camera = {}
  62. camera.y = 0
  63. camera.vitesse = 1
  64.  
  65. sonShoot = love.audio.newSource("sons/shoot.wav","static")
  66. sonExplode = love.audio.newSource("sons/explode_touch.wav","static")
  67. imgMenu = love.graphics.newImage("images/menu.jpg")
  68. imgGameOver = love.graphics.newImage("images/gameover.jpg")
  69. imgVictory = love.graphics.newImage("images/victory.jpg")
  70. ecrant_courrant = "menu"
  71.  
  72. --fonction squellette de base--
  73.  
  74. function updateMenu()
  75. end
  76.  
  77. function updateJeu(dt)
  78. camera.y = camera.y + camera.vitesse
  79. local n --pour toutes les boucles
  80.  
  81. --boucle pour les aliens
  82. for n=#liste_aliens,1,-1 do
  83. alien = liste_aliens[n]
  84. if alien.y > 0 then
  85. alien.endormi = false
  86. end
  87. if alien.endormi == false then
  88. alien.y = alien.y + alien.vy
  89. alien.x = alien.x + alien.vx
  90. alien.chronotir = alien.chronotir - 1
  91. if alien.type == 1 then
  92. if alien.chronotir <= 0 then
  93. CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,0,500,"enemy")
  94. alien.chronotir = math.random(50,90)
  95. end
  96. end
  97. if alien.type == 2 then
  98. if alien.chronotir <= 0 then
  99. CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,0,500,"enemy")
  100. alien.chronotir = math.random(40,60)
  101. end
  102. end
  103. if alien.type == 3 then --tourelle
  104. if alien.chronotir <= 0 then
  105. local angle
  106. angle = math.angle(alien.x,alien.y,heros.x,heros.y)
  107. CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,500*math.cos(angle),
  108. 500*math.sin(angle),"enemy")
  109. alien.chronotir = math.random(20,40)
  110. end
  111. end
  112. if alien.type == 4 then
  113. if alien.chronotir <= 0 then
  114. alien.angleTir = alien.angleTir + 0.5
  115. CreeTir("laser2",alien.x,alien.y + alien.image:getHeight(),0,2,2,true,500*math.cos(alien.angleTir),
  116. 500*math.sin(alien.angleTir),"enemy")
  117. alien.chronotir = 1
  118. end
  119. end
  120. else
  121. alien.y = alien.y + camera.vitesse
  122. end
  123. if alien.y > hauteur + alien.image:getHeight() or alien.x < 0 or alien.x > largeur then
  124. alien.supprime = true
  125. table.remove(liste_aliens,n)
  126. end
  127. end
  128.  
  129.  
  130. --lecture des tirs et mise en mouvement
  131. for n=#liste_tirs,1,-1 do
  132. local tir = liste_tirs[n]
  133. tir.y = tir.y + (tir.speedH * dt)
  134. tir.x = tir.x + (tir.speedW * dt)
  135. -- suppression du tir si hors ecran--
  136. if tir.y > hauteur or tir.y < 0 or tir.x > largeur or tir.x < 0 then
  137. tir.supprime = true
  138. table.remove(liste_tirs,n)
  139. end
  140.  
  141. --collision tirs et hero--
  142.  
  143. if tir.type == "enemy" then
  144. if collide(tir,heros) then
  145. tir.supprime = true
  146. table.remove(liste_tirs,n)
  147. heros.vie = heros.vie - 1
  148. if heros.vie <= 0 then
  149. ecrant_courrant = "gameover"
  150. end
  151. end
  152. end
  153.  
  154. if tir.type == "allies" then
  155. local nAlien
  156. for nAlien=#liste_aliens,1,-1 do
  157. local alien = liste_aliens[nAlien]
  158. if collide(tir,alien) then
  159. if alien.endormi == false then
  160. CreeExplosion(tir.x,tir.y)
  161. print("alien hit")
  162. tir.supprime = true
  163. table.remove(liste_tirs,n)
  164. alien.energie = alien.energie - 1
  165. if alien.energie <= 0 then
  166. if alien.type == 4 then
  167. local nExplosion
  168. for nExplosion= 1,math.random (120,160) do
  169. CreeExplosionBoss(alien.x + math.random(-largeur/2,largeur/2), alien.y + math.random(-hauteur/3,hauteur/3*2))
  170. sonExplode:play()
  171. end
  172. end
  173. if alien.type ~= 4 then
  174. victoire = true
  175. victoireTimer = 500
  176. for nExplosion= 1,math.random (30,80) do
  177. CreeExplosion(alien.x + math.random(-30,30), alien.y + math.random(-30,30))
  178. sonExplode:play()
  179. end
  180. end
  181. alien.supprime = true
  182. table.remove(liste_aliens,nAlien)
  183. end
  184. end
  185. end
  186. end
  187. end
  188. end
  189.  
  190.  
  191. --traitement et suppression des sprites
  192. for n=#liste_sprites,1,-1 do
  193. local sprite = liste_sprites[n]
  194. -- spriteSheet ou pas ?--
  195. if sprite.maxFrame > 1 then
  196. sprite.frame = sprite.frame + 0.2
  197. if math.floor(sprite.frame) > sprite.maxFrame then
  198. sprite.supprime = true
  199. else
  200. sprite.image = sprite.listeFrames[math.floor(sprite.frame)]
  201. end
  202. end
  203. if sprite.supprime == true then
  204. table.remove(liste_sprites,n)
  205. end
  206. end
  207.  
  208. --ici debug pour les fleches et mouvement vaiseau
  209. if love.keyboard.isDown("up") then
  210. arow.up.bool = true
  211. if heros.y > 0 + (heros.image:getHeight()) then
  212. heros.y = heros.y - (heros.speedH * dt)
  213. else
  214. heros.y = 0 + (heros.image:getHeight())
  215. end
  216. else
  217. arow.up.bool = false
  218. end
  219. if love.keyboard.isDown("right") then
  220. arow.right.bool = true
  221. if heros.x < largeur - (heros.image:getWidth()) then
  222. heros.x = heros.x + (heros.speedW * dt)
  223. else
  224. heros.x = largeur - (heros.image:getWidth())
  225. end
  226. else
  227. arow.right.bool = false
  228. end
  229. if love.keyboard.isDown("down") then
  230. arow.down.bool = true
  231. if heros.y < hauteur - (heros.image:getHeight()) then
  232. heros.y = heros.y + (heros.speedH * dt)
  233. else
  234. heros.y = hauteur - (heros.image:getHeight())
  235. end
  236. else
  237. arow.down.bool = false
  238. end
  239. if love.keyboard.isDown("left") then
  240. arow.left.bool = true
  241. if heros.x > 0 + (heros.image:getWidth()) then
  242. heros.x = heros.x - (heros.speedW * dt)
  243. else
  244. heros.x = 0 + (heros.image:getWidth())
  245. end
  246. else
  247. arow.left.bool = false
  248. end
  249.  
  250. if love.keyboard.isDown("space") then
  251. spacebarre.bool = true
  252. else
  253. spacebarre.bool = false
  254. end
  255. if victoire == true then
  256. victoireTimer = victoireTimer - 1
  257. if victoireTimer <= 0 then
  258. ecrant_courrant = "victory"
  259. end
  260. end
  261. end
  262.  
  263. function updateGameOver()
  264. end
  265.  
  266. function updateVictory()
  267. end
  268.  
  269. function drawMenu()
  270. love.graphics.draw(imgMenu,0,0)
  271. end
  272.  
  273. function drawJeu()
  274. love.graphics.setColor(255,255,255)
  275. --dessin du niveau
  276. local l,c
  277. local x,y = 0,(0-64) + camera.y
  278. for l= #niveau,1,-1 do
  279. for c=1 , 16 do
  280. local id = niveau[l][c]
  281. if id ~= 0 then
  282. love.graphics.draw(tileSheet[id],x,y,0,2,2)
  283. end
  284. x = x + 64
  285. end
  286. x = 0
  287. y = y - 64
  288. end
  289.  
  290. local n
  291. for n=1, #liste_sprites do
  292. sprite = liste_sprites[n]
  293. if sprite.bool == true then
  294. love.graphics.draw(sprite.image,sprite.x,sprite.y,math.rad(sprite.angle),sprite.width,sprite.height,sprite.image:getWidth()/2
  295. ,sprite.image:getHeight()/2)
  296. end
  297. end
  298. love.graphics.print("nombres de tirs : "..#liste_tirs.." nombres de sprites : "..#liste_sprites.." nombre d'aliens : "..#liste_aliens,0,0)
  299.  
  300. --dessin de la barre hp heros--
  301. heros.initVie = 9
  302. if heros.vie >= heros.initVie/3 * 2 then
  303. love.graphics.setColor(51,255,0)
  304. love.graphics.rectangle("fill",heros.x - heros.image:getWidth(), heros.y + heros.image:getHeight(),heros.vie*6,5)
  305. elseif heros.vie < heros.initVie/3 * 2 and heros.vie >= heros.initVie/3 then
  306. love.graphics.setColor(255,102,0)
  307. love.graphics.rectangle("fill",heros.x - heros.image:getWidth(), heros.y + heros.image:getHeight(),heros.vie*6,5)
  308. elseif heros.vie < heros.initVie/3 then
  309. love.graphics.setColor(255,0,0)
  310. love.graphics.rectangle("fill",heros.x - heros.image:getWidth(), heros.y + heros.image:getHeight(),heros.vie*6,5)
  311. end
  312. love.graphics.setColor(255,255,255)
  313.  
  314. end
  315.  
  316. function drawGameOver()
  317. love.graphics.draw(imgGameOver,0,0)
  318. end
  319.  
  320. function drawVictory()
  321. love.graphics.draw(imgVictory,0,0)
  322. end
  323.  
  324. function math.angle(x1,y1, x2,y2) return math.atan2(y2-y1, x2-x1) end
  325. ----GESTION DES COLLISIONS---
  326. function collide(a1, a2)
  327. if (a1==a2) then return false end
  328. local dx = a1.x - a2.x
  329. local dy = a1.y - a2.y
  330. if (math.abs(dx) < a1.image:getWidth()+a2.image:getWidth()) then
  331. if (math.abs(dy) < a1.image:getHeight()+a2.image:getHeight()) then
  332. return true
  333. end
  334. end
  335. return false
  336. end
  337. --------------------------------------
  338. --CREATION TIR
  339. function CreeTir(pNomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeedW,pSpeedH,pType)
  340. tir = CreeSprite(pNomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeedW,pSpeedH)
  341. tir.type = pType
  342. table.insert(liste_tirs,tir)
  343. end
  344.  
  345. --------------------------------------
  346. --CREATION ALIEN
  347.  
  348. function CreeAlien(pType, pX, pY,pRotate,pScaleW,pScaleH,pBool,pSpeed)
  349. local nomImage = ""
  350. if pType == 1 then
  351. nomImage = "enemy1"
  352. elseif pType == 2 then
  353. nomImage = "enemy2"
  354. elseif pType == 3 then
  355. nomImage = "tourelle"
  356. elseif pType == 4 then
  357. nomImage = "enemy3"
  358. end
  359.  
  360. local alien = CreeSprite(nomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeed)
  361.  
  362. alien.endormi = true
  363. alien.chronotir = 0
  364. alien.type = pType
  365.  
  366. if pType == 1 then
  367. alien.vy = 2
  368. alien.vx = 0
  369. alien.energie = 2
  370. elseif pType == 2 then
  371. alien.energie = 3
  372. alien.vy = 2
  373. local direction = math.random(1,2)
  374. if direction == 1 then
  375. alien.vx = 1
  376. else
  377. alien.vx = -1
  378. end
  379. elseif pType == 3 then -- tourelle
  380. alien.energie = 4
  381. alien.vy = camera.vitesse
  382. alien.vx = 0
  383. elseif pType == 4 then -- boss
  384. alien.energie = 8
  385. alien.vy = camera.vitesse*1.5
  386. alien.vx = 0
  387. alien.angleTir = 0
  388. end
  389. table.insert(liste_aliens,alien)
  390. end
  391.  
  392. -----------------------------
  393. ------------CREER ANIMATION SPRITE-------
  394.  
  395. --function CreeSpriteAnime(pNomSpriteSheet, pNomImage,pNbSprite)
  396. -- -- nom de la table du sprite sheet--
  397. -- local spriteSheet = {}
  398. -- local n
  399. -- for n=pNbSprite,1 do
  400. -- SpriteSheet[n] = CreeSprite("images/"..pNomImage.."_"..n..".png")
  401.  
  402.  
  403. function CreeSprite(pNomImage,pX,pY,pRotate,pScaleW,pScaleH,pBool,pSpeedW,pSpeedH,pVie)
  404.  
  405. sprite = {}
  406. sprite.x = pX
  407. sprite.y = pY
  408. sprite.angle = pRotate
  409. sprite.width = pScaleW
  410. sprite.height = pScaleH
  411. sprite.bool = pBool
  412. sprite.speedW = pSpeedW
  413. sprite.speedH = pSpeedH
  414. sprite.supprime = false
  415. sprite.vie = pVie
  416. sprite.initVie = pVie
  417.  
  418. if frame.sprite[pNomImage] == nil then
  419. frame.sprite[pNomImage] = love.graphics.newImage("images/"..pNomImage..".png")
  420. end
  421. sprite.image = frame.sprite[pNomImage]
  422.  
  423.  
  424. sprite.frame = 1
  425. sprite.listeFrames = {}
  426. sprite.maxFrame = 1
  427.  
  428. table.insert(liste_sprites,sprite)
  429.  
  430. return sprite
  431.  
  432. end
  433.  
  434. function CreeExplosion(pX,pY)
  435.  
  436. local newExplosion = CreeSprite("explode_1",pX,pY,0,2,2,true)
  437. newExplosion.listeFrames = spriteSheet
  438. newExplosion.maxFrame = #spriteSheet
  439. end
  440.  
  441. function CreeExplosionBoss(pX,pY)
  442.  
  443. local newExplosion = CreeSprite("explode_1",pX,pY,0,8,8,true)
  444. newExplosion.listeFrames = spriteSheet
  445. newExplosion.maxFrame = #spriteSheet
  446. end
  447.  
  448. function love.load()
  449.  
  450. love.window.setMode(1024,768)
  451.  
  452. largeur = love.graphics.getWidth()
  453. hauteur = love.graphics.getHeight()
  454.  
  455. arow.up = CreeSprite("up",100,400,0,0.25,0.25,false)
  456. arow.right = CreeSprite("up",125,430,90,0.25,0.25,false)
  457. arow.down = CreeSprite("up",100,460,180,0.25,0.25,false)
  458. arow.left = CreeSprite("up",75,430,270,0.25,0.25,false)
  459. spacebarre = CreeSprite("espacebarre",120,530,0,2,2,false)
  460. DemarreJeu()
  461.  
  462. end
  463.  
  464. function DemarreJeu()
  465.  
  466. heros = CreeSprite("heros",largeur/2,hauteur/2,0,2,2,true,500,500,9)
  467. heros.y = hauteur - heros.image:getHeight()*2
  468. local ligne = 4
  469. CreeAlien(1,largeur/2,(64/2)-(64*(ligne-1)),0,2,2,true)
  470. ligne = 9
  471. CreeAlien(2,largeur/3,(64/2)-(64*(ligne-1)),0,2,2,true)
  472. ligne = 12
  473. CreeAlien(3,3*64,(64/2)-(64*(ligne-1)),0,2,2,true)
  474. ligne = #niveau
  475. CreeAlien(4,largeur/2,(64/2)-(64*(ligne-1)),0,2,2,true)
  476.  
  477. camera.y = 0
  478.  
  479. end
  480.  
  481.  
  482. function love.update(dt)
  483.  
  484. if ecrant_courrant == "jeu" then
  485. updateJeu(dt)
  486. elseif ecrant_courrant == "menu" then
  487. updateMenu()
  488. elseif ecrant_courrant == "gameover" then
  489. updateGameOver()
  490. elseif ecrant_courrant == "victory" then
  491. updateVictory()
  492. end
  493. end
  494.  
  495. function love.draw()
  496.  
  497. if ecrant_courrant == "jeu" then
  498. drawJeu()
  499. elseif ecrant_courrant == "menu" then
  500. drawMenu()
  501. elseif ecrant_courrant == "gameover" then
  502. drawGameOver()
  503. elseif ecrant_courrant == "victory" then
  504. drawVictory()
  505. end
  506.  
  507.  
  508.  
  509. -- love.graphics.print(""..tostring(heros.vie),heros.x + heros.image:getWidth(), heros.y + heros.image:getHeight())
  510. -- love.graphics.print("vie heros = "..tostring(heros.vie))
  511. end
  512.  
  513. function love.keypressed(key)
  514.  
  515. if key == "space" then
  516. if ecrant_courrant == "jeu" then
  517. CreeTir("laser1",heros.x,heros.y - (heros.image:getHeight()*2)/2,0,2,2,true,0,-500,"allies")
  518. love.audio.play(sonShoot)
  519. else if ecrant_courrant == "menu" then
  520. ecrant_courrant = "jeu"
  521. end
  522. end
  523. end
  524.  
  525. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement