Skip to content

Instantly share code, notes, and snippets.

Created March 7, 2012 18:27
Show Gist options
  • Save anonymous/1994954 to your computer and use it in GitHub Desktop.
Save anonymous/1994954 to your computer and use it in GitHub Desktop.
Space invaders main file
hero = nil
enemies = nil
bgLines = nil
explosion = nil
killCount=0
GAME_PLAYING = 0
GAME_DEAD = 1
GAME_WON = 2
state = GAME_PLAYING
heroSize=100
-- Use this function to perform your initial setup
function setup()
    state = GAME_PLAYING
    iparameter("BackgroundSpawnRate",0,3,2)
    hero = Invader()
    hero.position = vec2(WIDTH/2, 150)
    enemies = EnemyHorde()
    enemies.heroBullets = hero.bullets
    bgLines = StreamingLines()
end
function touchingAtPos()
    if CurrentTouch.state == BEGAN or 
       CurrentTouch.state == MOVING then
        return vec2( CurrentTouch.x, CurrentTouch.y )
    end 
    return nil
end
function showScore()
    watch("killCount")
end
-- This function gets called once every frame
function draw()
    background(0, 0, 0, 255)
    bgLines.spawnRate = BackgroundSpawnRate
    bgLines:update()
    bgLines:draw()
    showScore()
    
    if state == GAME_PLAYING then 
        -- Process touch
        touch = touchingAtPos()
        if touch then
            if touch.x < (hero.position.x - 10) then
                hero.position.x = hero.position.x - 3
            elseif touch.x > (hero.position.x + 10) then
                hero.position.x = hero.position.x + 3
            end
        end
        if killCount == 20 then
            enemies.spawnPattern = ENEMY_SPAWN_HARD
        end
        enemies:draw()
        hero:draw()
        -- Check if hero is hit
        for i,v in ipairs(enemies.units) do
            if v:dist(hero.position) < heroSize then
                state = GAME_DEAD
                explosion = Explosion(hero.position)
            end
        end
    elseif state == GAME_DEAD then
        if explosion then
            explosion:draw()
            if explosion:isDone() then
                explosion = nil
            end
        end
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment