Advertisement
lyoy

Pico8 Shooter GameJam GameCodeur #11

Dec 9th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.43 KB | None | 0 0
  1. -- alphawar by lyoy
  2. -- created for gamejam gamecodeur 11 --
  3. game={}
  4. game.scene=""
  5. game.statut=""
  6. game.btnxrelease=false
  7. game.score=0
  8.  
  9. grpenemies={}
  10. grpprojectiles={}
  11. grpparticules={}
  12. grpbackground={}
  13. grpgfx={}
  14. player={}
  15. timer=0
  16. timer1s=0
  17. lasttime=0
  18.  
  19. levelbg={
  20. {"<","","","","","","","<",">"},
  21. {"<","","","","","","","<",">"},
  22. {"<","","","","","","","<",">"},
  23. {"<","","","","","","","p",">"},
  24. {"<","","","","","","","p",">"},
  25. {"<","","","","","","","p",">"}
  26.  
  27. }
  28.  
  29.  
  30.  
  31. function _draw()
  32.  
  33.     cls()
  34.    
  35.     if(game.scene=="game")then
  36.         game_draw()
  37.     elseif(game.scene=="start_screen")then
  38.         stscreen_draw()
  39.     end
  40.  
  41. end
  42.  
  43. function game_init()
  44.     game.statut="game"
  45.     player=mkship(50,80)
  46.     mkbackground()
  47.     for i=1,5 do
  48.         local enemy=mkenemy(rnd(100),(rnd(20)+40)*-1,flr(rnd(3)+1) )
  49.     end
  50.    
  51.     for i=1,10 do
  52.         local enemy=mkenemy(rnd(100),(rnd(20)+120)*-1,flr(rnd(3)+1) )
  53.     end
  54.    
  55.     for i=1,15 do
  56.         local enemy=mkenemy(rnd(100),(rnd(20)+200)*-1,flr(rnd(3)+1) )
  57.     end
  58.    
  59.     for i=1,20 do
  60.         local enemy=mkenemy(rnd(100),(rnd(20)+350)*-1,flr(rnd(3)+1) )
  61.     end
  62.    
  63.     for i=1,10 do
  64.         local enemy=mkenemy(rnd(100),(rnd(20)+500)*-1,flr(rnd(3)+1) )
  65.     end
  66.    
  67.  
  68. end
  69.  
  70. function _init()
  71.     game.scene="start_screen"
  72. end
  73.  
  74.  
  75. function game_over()
  76.     game.statut="gameover"
  77.     --restart()
  78. end
  79.  
  80. function restart()
  81.    
  82.     game.scene="start_screen"
  83.     grpenemies={}
  84.     grpprojectiles={}
  85.     grpparticules={}
  86.     grpbackground={}
  87.     grpgfx={}
  88.     player={}
  89.  
  90. end
  91.  
  92. function _update()
  93.    
  94.  
  95.     timer=timer+1
  96.     timer1s=flr(timer/30)
  97.    
  98.    
  99.     if(game.scene=="game") then
  100.         if(btnp(5) and game.statut=="gameover")then
  101.             restart()
  102.         else
  103.             game_update()
  104.         end
  105.        
  106.     elseif(game.scene=="start_screen")then
  107.         stscreen_update()
  108.     end
  109.    
  110.    
  111.    
  112.    
  113.    
  114. end
  115.  
  116.  
  117. ---- functions  make -----
  118. --------------------------
  119.  
  120.  
  121.  
  122.  
  123. function mkenemy(px,py,ptype)
  124.     local enemy={}
  125.     enemy.x=px
  126.     enemy.y=py
  127.     enemy.dx=0
  128.     enemy.speed=0.15
  129.     enemy.ecartx=4
  130.  enemy.ecarty=5
  131.  enemy.shootrate=10
  132.     if (ptype==1) then
  133.         enemy.sprite=
  134.         {
  135.             {"<",0,"e",0,">"},
  136.         {"<","e","a","a",">"},
  137.         {"<",0,"h",0,">"}
  138.         }
  139.         enemy.col=1
  140.         enemy.dx=0
  141.         enemy.dy=1
  142.     elseif (ptype==2) then
  143.         enemy.sprite=
  144.         {
  145.             {"<",0,"e",0,">"},
  146.         {"<","e","a","a",">"},
  147.         {"<","y","y","y",">"}
  148.         }
  149.         enemy.col=2
  150.         enemy.dy=3
  151.     elseif (ptype==3) then
  152.             enemy.sprite=
  153.         {
  154.             {"<","u","e","u",">"},
  155.         {"<","e","a","a",">"},
  156.         {"<","y","y","y",">"},
  157.         {"<",0,"y",0,">"}
  158.         }
  159.         enemy.col=2
  160.         enemy.dy=3
  161.     elseif (ptype==4) then
  162.         enemy.sprite=
  163.         {
  164.             {0,0,"r",0,0},
  165.         {"<","e","a","a",">"},
  166.         {"<","y","y","y",">"},
  167.         {"<","t","0","t",">"},
  168.         {0,"<","\142",">",0}
  169.         }
  170.         enemy.col=1
  171.         enemy.dy=4
  172.     end
  173.     enemy.w=#(enemy.sprite[1])*6
  174.     enemy.h=#enemy.sprite*6
  175.     enemy.life=flr(rnd(60))
  176.     enemy.damagetimer=0
  177.     printh("enemy.h: "..enemy.h)
  178.    
  179.     function enemy:update()
  180.         enemy:collisions()
  181.         enemy:move()
  182.         enemy.shootrate-=1
  183.         if(enemy.shootrate<=0
  184.         and enemy.y>0)then
  185.             enemy.shoot()
  186.             enemy.shootrate=rnd(150)+5
  187.         end
  188.     end
  189.    
  190.     function enemy:move()
  191.         enemy.x=enemy.x+enemy.dx*enemy.speed
  192.         enemy.y=enemy.y+enemy.dy*enemy.speed
  193.        
  194.         --enemy.x=enemy.x+cos(time()/3)*20+50
  195.        
  196.     end--end fn enemu move
  197.    
  198.    
  199.    
  200.     function enemy:shoot()
  201.         mkbullet("benemy",enemy.x+enemy.ecartx,enemy.y+20,1,11,"!!")
  202.     end
  203.    
  204.     function enemy:die()
  205.         local px=enemy.x+enemy.w/2
  206.         local py=enemy.y+enemy.h/2
  207.         sfx(2)
  208.         if(game.score>=0)then
  209.       game.score+=2
  210.      end
  211.        
  212.         for i=1,10 do
  213.             mkparticule(px+rnd(10),py+rnd(4))
  214.         end
  215.        
  216.         del(grpgfx,enemy)
  217.         del(grpenemies,enemy)
  218.     end--end fn enemy die
  219.    
  220.     function enemy:damage(pamount)
  221.         enemy.life=enemy.life-pamount
  222.         enemy.damagetimer=20
  223.         if(enemy.life<=0)then
  224.             enemy.die()
  225.         end
  226.     end-- end fn enemy damage
  227.    
  228.     function enemy:collisions()
  229.         for i=1,#grpprojectiles do
  230.             local proj=grpprojectiles[i]
  231.            
  232.             if(proj~=nil)then
  233.            
  234.                     if proj.x+proj.w/2>enemy.x
  235.                         and proj.x+proj.w/2<enemy.x+enemy.w
  236.                         and proj.y>enemy.y
  237.                         and proj.y<enemy.y+enemy.h
  238.                         and enemy.y>0  then
  239.                     --collision
  240.                        
  241.                         sfx(3)
  242.                         if(proj.tag=="bship")then
  243.                             enemy:damage(proj.damage)
  244.                             proj:die()
  245.                         end
  246.                    
  247.                    
  248.                    
  249.                     end--end check
  250.            
  251.             end--end check proj nil
  252.            
  253.         end--end loop proj
  254.     end--end functin collision
  255.    
  256.     add(grpgfx,enemy)
  257.     add(grpenemies,enemy)
  258.     return enemy
  259.  
  260. end
  261.  
  262.  
  263. function mkparticule(px,py)
  264.     local particule={}
  265.     particule.x=px
  266.     particule.y=py
  267.     particule.life=10
  268.     add(grpparticules,particule)
  269.    
  270.     function particule:update()
  271.         particule.life=particule.life-1
  272.         if(particule.life<=0)then
  273.             del(grpparticules,particule)
  274.         end
  275.     end
  276.    
  277.     return particule
  278.    
  279. end
  280.  
  281. function mkpartbg()
  282.     local bgpart={}
  283.     bgpart.x=rnd(150)
  284.     bgpart.y=rnd(150)
  285.     bgpart.speed=rnd(5)
  286.     function bgpart:update()
  287.             bgpart.y=bgpart.y+1*bgpart.speed
  288.             if(bgpart.y>150)then
  289.                 bgpart.y=rnd(30)*-1
  290.             end
  291.     end
  292.        
  293.     add(grpbackground,bgpart)
  294.     return bgpart
  295.    
  296. end
  297.  
  298. function mkbackground()
  299.     for i=1,20 do
  300.         mkpartbg()
  301.     end
  302. end
  303.  
  304. function mkbullet(ptag,px,py,pdy,pcol,ptext)
  305.     local bullet={}
  306.     bullet.x=px
  307.     bullet.y=py
  308.     bullet.dy=pdy
  309.     bullet.col=pcol
  310.     bullet.tag=ptag
  311.     bullet.sprite=
  312.     {
  313.     {ptext}
  314.     }
  315.     bullet.size=1
  316.     bullet.w=6
  317.     bullet.velocity=3
  318.     bullet.damage=10
  319.     function bullet:move()
  320.         bullet.y=bullet.y+bullet.dy*bullet.velocity
  321.         if (bullet.y<-0) then
  322.             bullet:die()
  323.         end -- end if bullet screen out
  324.     end
  325.     function bullet:die()
  326.         del(grpgfx,bullet)
  327.         del(grpprojectiles,bullet)
  328.     end
  329.    
  330.     add(grpgfx,bullet)
  331.     add(grpprojectiles,bullet)
  332.    
  333.     return bullet
  334. end
  335.  
  336.  
  337.  
  338. function mkship(px,py)
  339.     ship={}
  340.     ship.sprite={
  341.     {"i","","","^","","","i"},
  342.     {"i","","","i","","","i"},
  343.     {"i","","","i","","","i"},
  344.  {"<","i","a","m","-","a",">"},
  345.  {"<","x","w","i","n","g",">"},
  346.  {"<","-","i","i","i","-",">"}
  347.  }
  348.  
  349.  ship.x=px
  350.  ship.y=py
  351.  ship.dx=0
  352.  ship.dy=0
  353.  ship.col=7
  354.  ship.ecartx=4
  355.  ship.ecarty=5
  356.  ship.lifemax=100
  357.  ship.life=ship.lifemax
  358.  ship.speed=2
  359.  ship.w=#(ship.sprite[1])*6
  360.     ship.h=#ship.sprite*6
  361.  ship.firecooldownlimit=5
  362.  ship.firecooldown=ship.firecooldownlimit
  363.  ship.damagetimer=0
  364.  --table.insert(grpgfx,ship)
  365.  
  366.  function ship:update()
  367.     ship:collisions()
  368.         if(ship.firecooldown>0 )then
  369.             ship.firecooldown=ship.firecooldown-1
  370.         else
  371.             ship.firecooldown=ship.firecooldownlimit
  372.         end
  373.  end
  374.  
  375.  function ship:damage(pamount)
  376.     if(ship.life>0)then
  377.         ship.life=ship.life-pamount
  378.     end
  379.        
  380.         ship.damagetimer=20
  381.         if(ship.life<=0)then
  382.             ship.die()
  383.         end
  384.     end-- end fn enemy damage
  385.    
  386.     function ship:die()
  387.      sfx(2)
  388.      if(game.score>0)then
  389.       game.score-=1
  390.      end
  391.    
  392.         game_over()
  393.     end
  394.  
  395.  function ship:collisions()
  396.         for i=1,#grpprojectiles do
  397.             local proj=grpprojectiles[i]
  398.            
  399.             if(proj~=nil)then
  400.                 print("ship",50,50,7)
  401.                     if proj.x+proj.w/2>ship.x
  402.                         and proj.x+proj.w/2<ship.x+ship.w
  403.                         and proj.y>ship.y
  404.                         and proj.y<ship.y+ship.h  then
  405.                     --collision
  406.                     sfx(3)
  407.                     printh("-----collisions ship----")
  408.                     ship:damage(proj.damage)
  409.                     proj:die()
  410.                    
  411.                    
  412.                     end--end check
  413.            
  414.             end--end check proj nil
  415.            
  416.         end--end loop proj
  417.     end--end functin collision
  418.  
  419.  function ship:shoot()
  420.     sfx(1)
  421.     mkbullet("bship",ship.x,ship.y,-1,8,"!")
  422.     mkbullet("bship",ship.x+24,ship.y,-1,8,"!")
  423.    
  424.  end
  425.  add(grpgfx,ship)
  426.  return ship
  427. end
  428.  
  429. ------ draw ------
  430. ------------------
  431.  
  432. function draw_ui()
  433.  
  434.    
  435.     rectfill(0,0,128,10,0)
  436.     rect(0,0,127,10,6)
  437.     print("life",5,3,8)
  438.     --- life bar ----
  439.     local px=24
  440.     local pxmaxinit=50
  441.     local pxmax=50
  442.     if(player~=nil)then
  443.         local lifepercent=player.life/player.lifemax
  444.        
  445.         pxmax = lifepercent*(pxmaxinit-px)+px
  446.        
  447.     end
  448.    
  449.     rectfill(px,3,pxmax,7,8)
  450.     rect(px,3,pxmaxinit,7,5)
  451.    
  452.     print("score",60,3,9)
  453.     print(game.score,90,3,9)
  454. end
  455.  
  456. function draw_cgx()
  457.  
  458.     local posy=0
  459.     local posx=0
  460.     local spacex=6
  461.     local spacey=6
  462.     local col=0
  463.     for i=1,#grpgfx do
  464.         local gfx=grpgfx[i]
  465.        
  466.         if (gfx~=nil)then
  467.            
  468.             if(gfx.ecartx~=nil and gfx.ecarty~=nil)then
  469.                 spacex=gfx.ecartx
  470.                 spacey=gfx.ecarty
  471.             end
  472.             col=gfx.col
  473.            
  474.             for j=1,#grpgfx[i].sprite do
  475.                 posy=j*spacey+grpgfx[i].y
  476.                 local pixl=grpgfx[i].sprite[j]
  477.                     for k=1,#pixl do
  478.                
  479.                         posx=k*spacex+grpgfx[i].x
  480.                        
  481.                            
  482.                        
  483.                         if ( type(pixl[k])=="string") then
  484.                        
  485.                             if (gfx.damagetimer~=nil)then
  486.                                 if (gfx.damagetimer>0) then
  487.                                     gfx.damagetimer=gfx.damagetimer-1
  488.                                     col = flr((time()*20)%3)+7
  489.                                 elseif(gfx.damagetimer<=0)then
  490.                                     --gfx.damagetimer=10
  491.                                 end -- end damage timer
  492.                                
  493.                         end--end check damage timer exist
  494.                        
  495.                             print(pixl[k],posx-1,posy-1,col)
  496.                         elseif (pixl[k]=="") then
  497.                        
  498.                         else
  499.                             if (gfx.damagetimer~=nil)then
  500.                                 if (gfx.damagetimer>0) then
  501.                                     gfx.damagetimer=gfx.damagetimer-1
  502.                                     col = flr((time()*20)%3)+7
  503.                                 elseif(gfx.damagetimer<=0)then
  504.                                     --gfx.damagetimer=10
  505.                                 end -- end damage timer
  506.                                
  507.                             end--end check damage timer exist
  508.                            
  509.                             if(gfx.tag~=nil)then
  510.                                 if(gfx.tag=="benemy")then
  511.                                     circ(posx,posy,2,gfx.col)
  512.                                 elseif(gfx.tag=="bship")then
  513.                                     circ(posx,posy,1,col)
  514.                                 end
  515.                             else
  516.                                 circ(posx,posy,4,col)
  517.                             end
  518.                            
  519.                            
  520.                            
  521.                         end--end if check type
  522.                    
  523.                     end-- end loop sprite col
  524.                
  525.             end-- end loop sprite line
  526.             posx=0
  527.        
  528.         end -- end if gfx not nil
  529.        
  530.        
  531.     end--end loop all gfx
  532.  
  533. end
  534.  
  535. function draw_particules()
  536.  
  537.     for i=1,#grpparticules do
  538.         local part=grpparticules[i]
  539.         if (part~=nil)then
  540.             circ(part.x,part.y,flr((time()*20)%20)+7,flr((time()*10)%3)+7)
  541.         end
  542.     end
  543.  
  544. end
  545.  
  546. function draw_background()
  547.    
  548.     for i=1,#grpbackground do
  549.         local bgp=grpbackground[i]
  550.         if(bgp~=nil)then
  551.             line(bgp.x,bgp.y,bgp.x,bgp.y+bgp.speed,5)
  552.            
  553.         end
  554.     end
  555.    
  556. end
  557.  
  558. function game_draw()
  559.    
  560.     draw_background()
  561.     draw_particules()
  562.     draw_cgx()
  563.     draw_ui()
  564.    
  565.     if(game.statut=="gameover")then
  566.         rectfill(9,40,110,80,0)
  567.         rect(10,40,110,80,1)
  568.         print("game over",39,50,1)
  569.         print("game over",40,50,5)
  570.         print("press \151 to restart",19,65,1)
  571.         print("press \151 to restart",20,65,5)
  572.     end
  573.  
  574. end
  575.  
  576. function stscreen_draw()
  577.  
  578.     line(62,0,128,128,7)
  579.     line(62,0,0,128,7)
  580.     local p=45
  581.     local y=50
  582.     print("alpha war",p-1,y,7)
  583.     print("alpha war",p+1,y,7)
  584.     print("alpha war",p,y+1,7)
  585.     print("alpha war",p,y-1,7)
  586.     print("alpha war",p,y,6)
  587.    
  588.     print("by lyoy",48,60,6)
  589.     print("push \151 to start",32,80,1)
  590.     print("push \151 to start",31,80,8)
  591.    
  592.    
  593.    
  594. end
  595.  
  596. ------- updates -------
  597. -----------------------
  598.  
  599.  
  600. function stscreen_update()
  601.  
  602.     if(btnp(5) and game.scene=="start_screen")then
  603.         --on lance la partie
  604.         game.scene="game"
  605.         game_init()
  606.     end
  607.  
  608. end
  609.  
  610.  
  611.  
  612.  
  613. function game_update()
  614.  
  615.    
  616.  
  617.     player:update()
  618.    
  619.     for i=1,#grpbackground do
  620.         local bgpart=grpbackground[i]
  621.         if(bgpart~=nil)then
  622.             bgpart:update()
  623.         end
  624.     end
  625.    
  626.     for i=1,#grpparticules do
  627.         local part=grpparticules[i]
  628.         if(part~=nil)then
  629.             part:update()
  630.         end
  631.     end
  632.     --[[ debug
  633.     --printh( flr((time()*5)%3)+1 )
  634.     --printh("firecooldown : "..player.firecooldown)
  635.     --printh("firecooldown limit : "..player.firecooldownlimit)
  636.     --printh(#grpprojectiles)
  637.     for i=1,#grpenemies do
  638.         local enemy=grpenemies[i]
  639.         if (enemy~=nil)then
  640.                
  641.                 enemy:update()
  642.                 if (lasttime!=timer1s) then
  643.                     --enemy:move()
  644.                 end--end if timer1s
  645.         end--end check enemy nil
  646.     end--end loop grpenemies
  647.    
  648.     for i=1,#grpprojectiles do
  649.         local proj=grpprojectiles[i]
  650.         if (proj~=nil) then
  651.             proj:move()
  652.         end--end if proj nil
  653.        
  654.     end--end loop projectiles
  655.    
  656.    
  657.     if(game.statut=="game")then
  658.    
  659.         if(btn(0) == true
  660.         and game.scene=="game"
  661.         and player.x>-10) then
  662.             player.x=player.x-1*player.speed
  663.         end
  664.         if(btn(1) == true
  665.         and game.scene=="game"
  666.         and player.x+player.w<130 )then
  667.             player.x=player.x+1*player.speed
  668.             --player.ecartx+=0.03
  669.         end
  670.         if(btn(2) == true
  671.         and game.scene=="game"
  672.         and player.y>0 )then
  673.             player.y=player.y-1*player.speed
  674.         end
  675.         if(btn(3) == true
  676.         and game.scene=="game"
  677.         and player.y+player.h<150 )then
  678.             player.y=player.y+1*player.speed
  679.        
  680.         end
  681.         if(btn(5) == true and game.scene=="game" and player.firecooldown==player.firecooldownlimit) then
  682.             player:shoot()
  683.         elseif(btnp(5) and game.scene=="game" and player.firecooldown>=player.firecooldownlimit-3 )then
  684.             player:shoot()
  685.         end
  686.    
  687.         lasttime=timer1s
  688.    
  689.     elseif(game.statut=="gameover")then
  690.        
  691.         if(btnp(5))then
  692.            
  693.             --game.statut=""
  694.             --game_over()
  695.         end
  696.    
  697.     end
  698.    
  699.    
  700.  
  701.    
  702.  
  703. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement