Log In  

Cart #21242 | 2016-05-24 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2

I was playing with the sprite editor, and found my creatures nice enough to make a game.
I'm still focused on my lode runner project, so I wanted to keep it short and simple.
this won't be the game of the year, and to give it a use, I did my best to document the code so eventually it can help beginners.

the game is simple (boring ?) - fire and hit a max. of alien before you die
but there's room to improve it :

  • increase the difficulty by adding new aliens every 10 points
  • move the aliens in direction of the player when it is at a certain range
    ...
function new_alien()
  a={}                          -- a new alien is allocated
  a.x=rnd(120)                  -- aliens appear on the top 1/3 of the screen 
  a.y=rnd(40)                   
  a.spr=flr(rnd(6))+2           -- and can be of any of the 6 alien sprites
  a.dx=rnd(4)-2                 -- direction and speed is random
  a.dy=rnd(5)-2                 -- but more often going down than up
  add(aliens,a)                 -- the new alien is added to our list of aliens
end

function _init()                -- called once at the start of the programm
  x=60                          -- player's fighter initial position
  y=100                         
  hold_fire=0                   -- used prevent continuous fire
  score=0                       
  best=0                        -- the high score
  bullets={}                    -- list of active bullets
  aliens={}                     -- the list of aliens
  for i=1,20 do new_alien() end -- creates 20 aliens
  cls()                         -- clear the screen
  print("saccharine",44,38,7)   -- and displays a  title screen
  print("PRESENTS",48,48,6)
  print("until death",43,58,8)
  print("x start",50,98,6)
  while not btn(5) do end       -- waits for x key to be pressed
end

function _draw()
  cls()                         -- clear the screen
  spr(1,x,y)                    -- displays player's fighter sprite

   for a in all(aliens) do      -- for each alien,
    a.x+=a.dx                   -- calculate its new position
    a.y+=a.dy
    if a.x>0 and a.y>0 and a.x<127 and a.y<127 then -- if alien is still inside the screen
      spr(a.spr,a.x,a.y)        -- displays it
      if a.x+2>=x and a.x+2<=x+7 and a.y+2>=y and a.y+2<=y+8 then -- alien collision with fighter
        del(aliens,a)           -- remove the alien from the list of aliens (we won't display it anymore)
        new_alien()             -- and replace it by a new one so that the number of aliens is always the same
        sfx(2)                  -- louder crash sound than for the aliens
        x=60                    -- respwan
        y=100                   
        bullets={}              -- clears all active bullets
        score=0                 -- start over
      end                       
    else                        
      del(aliens,a)             -- alien leaved the screen, delete it
      new_alien()               -- and replace it by a new one
    end                         
  end                           

  for b in all(bullets) do      -- parsing each active bullet
    b.y-=3                      -- move it up
    if b.y<0 then               -- the bullet exits the top of the screen
      del(bullets,b)            -- we remove it from the list of active bullets 
    else                        
      if pget(b.x,b.y)!=0 then  -- if it hits something
        del(bullets,b)          -- we delete it
        sfx(1)                  -- alien explosion
        score+=1
        for a in all(aliens) do -- search which alien we killed 
          if a.x<=b.x and a.x+5>=b.x and a.y<=b.y and a.y+4>=b.y then
            del(aliens,a)       -- delete it
            new_alien()         -- and replace it by a new one
          end                   
        end                     
      else                      
        pset(b.x,b.y,7)         -- displays the bullet
        pset(b.x,b.y+1,8)       -- and a red dot behind it
      end                       
    end                         
  end                           

  if (score>best) best=score    -- displays updated high score, and current score
  print(best,0,0,6)             
  print(score,0,8,6)            
end                             

function _update()              
  if (btn(0) and x>0  ) x-=2    -- going left
  if (btn(1) and x<120) x+=2    -- right
  if (btn(2) and y>50 ) y-=2    -- up
  if (btn(3) and y<120) y+=2    -- down

  hold_fire+=1                  -- increases the timer 30 times per second
  if (btn(4) or btn(5)) and hold_fire>8 then -- allowed to fire again
    sfx(0)
    add(bullets,{x=x+1,y=y})    -- left gun
    add(bullets,{x=x+5,y=y})    -- right gun
    hold_fire=0                 -- the timer is reset to zero, so we have
  end                           -- to wait it reaches 9 to fire again 
end

read the code, make it yours, improve the game, show off your changes and explain how you did
... or beat the score

P#21243 2016-05-24 08:58 ( Edited 2016-05-25 06:02)

Like you said, pretty simple, yet really fun and addictive game. I don't have PICO-8 yet, will have it once I get my PocketCHIP, and after that I will try to improve this and offer a different version c:

P#21248 2016-05-24 11:01 ( Edited 2016-05-24 15:01)

This code is very useful to beginners like myself. I'm new to creating and destroying objects in most languages, it's nice to have a solid example.

P#21304 2016-05-25 02:02 ( Edited 2016-05-25 06:02)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-06-02 13:12 | 0.017s | 4194k | Q:27