Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Adding Menacing Aliens To Your Space Scene

Now comes the exciting part where we introduce menacing aliens to shoot at! Adding aliens to your Space Invaders game in Codea Lua will make it much more engaging. I'll go step-by-step through the code to explain how to accomplish this.

Step 1: Declare Alien Variables

Firstly, we'll declare some variables and arrays to keep track of the aliens. Add these at the top of your code, just after the `bullets = {}` line:

alienRows = 3

alienCols = 5

aliens = {}

alienSpeed = 5

alienDir = 1  -- 1 for right, -1 for left

alienYStep = 20  -- Amount to move down at each edge

```

Step 2: Initialize Aliens

In your `setup()` function, initialize the alien positions. Create a nested for-loop to position the aliens in a grid:

for row = 1, alienRows do

    for col = 1, alienCols do

        table.insert(aliens, { x = col * 100, y = HEIGHT - row * 100 })

    end

end

```

Step 3: Draw Aliens

In your `draw()` function, add another loop to draw the aliens:

for _, alien in ipairs(aliens) do

    sprite( asset.builtin.Space_Art.Green_Ship, alien.x, alien.y)

end

```

Step 4: Move Aliens

Also, in the `draw()` function, we'll make the aliens move. Check for boundaries and update positions:

-- Move the aliens

for _, alien in ipairs(aliens) do

    alien.x = alien.x + alienSpeed * alienDir

end

-- Check for boundary collision

if aliens[1].x < 0 or aliens[#aliens].x > WIDTH then

    alienDir = -alienDir

    for _, alien in ipairs(aliens) do

        alien.y = alien.y - alienYStep

    end

end

```

Step 5: Putting It All Together

Here's the complete code listing:

bullets = {}

-- Step 1

alienRows = 3

alienCols = 5

aliens = {}

alienSpeed = 5

alienDir = 1

alienYStep = 20

function setup()

    viewer.mode = FULLSCREEN

    shipX = WIDTH/2

    shipY = HEIGHT/4

    

    -- Step 2

    for row = 1, alienRows do

        for col = 1, alienCols do

            table.insert(aliens, { x = col * 100, y = HEIGHT - row * 100 })

        end

    end

end

function draw()

    background(40, 40, 50)

    sprite(asset.builtin.Space_Art.Red_Ship, shipX, shipY)

    

    -- Step 3

    for _, alien in ipairs(aliens) do

        sprite(asset.builtin.Space_Art.Green_Ship, alien.x, alien.y)

    end

    

    for i, bullet in ipairs(bullets) do

        ellipse(bullet.x, bullet.y, 10)

        bullet.y = bullet.y + 5

        

        if bullet.y > HEIGHT then

            table.remove(bullets, i)

        end

    end

    

    -- Step 4

    for _, alien in ipairs(aliens) do

        alien.x = alien.x + alienSpeed * alienDir

    end

    

    if aliens[1].x < 0 or aliens[#aliens].x > WIDTH then

        alienDir = -alienDir

        for _, alien in ipairs(aliens) do

            alien.y = alien.y - alienYStep

        end

    end

end

function touched(touch)

    if touch.state == BEGAN then

        table.insert(bullets, {x = shipX, y = shipY})

    end

    

    if touch.x < shipX then

        shipX = shipX - 5

    elseif touch.x > shipX then

        shipX = shipX + 5

    end

end

```

And there you go! Now you have aliens that move left and right, and also descend upon reaching the edges.

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.