Learn to Code Space Invaders – Lesson 9 – Arrays and Loops (part 1)
31st July 2019Learn to Code Space Invaders – Lesson 11 – Repeat Until Loops
1st August 2019Space Invaders Lesson 10 – More Arrays and Loops
There are 2 parts to this lesson so don’t forget to scroll down a bit for the second half!
Code for Lesson 10
-- title: Space Invaders -- author: Bob Grant -- desc: Lesson 10 -- script: lua playerShip = { position = { x = 120, y = 128 }, spriteNum = 0, minX = 0, maxX = 232, speed = 1, bulletOffset = { x = 4, y = 4 } } playerBullets = {} maxPlayerBullets = 5 gameNeedsToBeInitialised = true function TIC() -- game initialisation -- check if game needs to be initialised if (gameNeedsToBeInitialised) then initialiseGame() gameNeedsToBeInitialised = false end -- updating movePlayerShip() checkPlayerFire() movePlayerBullet() -- drawing / rendering cls() drawPlayerBullet() drawPlayerShip() end -- end TIC function movePlayerShip() -- check move right button if(btn(2)) then playerShip.position.x = playerShip.position.x - playerShip.speed end -- check move left button if(btn(3)) then playerShip.position.x = playerShip.position.x + playerShip.speed end playerShip.position.x = checkLimits( playerShip.position.x, playerShip.minX, playerShip.maxX ) end -- movePlayerShip function checkLimits(value, min, max) if (value > max) then value = max elseif (value < min) then value = min else value = value end return value end -- checkLimits function checkPlayerFire() local bulletFired = false -- if fire button is pressed then if (btnp(4)) then -- find a bullet that's ready to fire for bullet = 1,maxPlayerBullets do if (bulletFired == false) then if (not playerBullets[bullet].active) then -- initialise bullet playerBullets[bullet].position = { x = playerShip.position.x + playerShip.bulletOffset.x, y = playerShip.position.y + playerShip.bulletOffset.y } -- mark bullet as active playerBullets[bullet].active = true -- stop other bullets from firing bulletFired = true end -- if not active end -- if not bulletfired end -- for end -- if button pressed end -- checkPlayerFire function movePlayerBullet() for bullet = 1, maxPlayerBullets do if (playerBullets[bullet].active) then -- move the bullet up the screen playerBullets[bullet].position.y = playerBullets[bullet].position.y - playerBullets[bullet].speed if (playerBullets[bullet].position.y < 0) then playerBullets[bullet].active = false end end end end -- movePlayerBullet function drawPlayerBullet() for bullet = 1, maxPlayerBullets do if (playerBullets[bullet].active) then -- draw player bullet -- line(startX, StartY, endX, endY, colour) line( playerBullets[bullet].position.x, playerBullets[bullet].position.y, playerBullets[bullet].position.x, playerBullets[bullet].position.y + playerBullets[bullet].length, playerBullets[bullet].colour ) end -- if end -- for end -- drawPlayerBullet function drawPlayerShip() spr(playerShip.spriteNum, playerShip.position.x, playerShip.position.y, 0) end -- drawPlayerShip function initialiseGame() initPlayerBulletsArray() end -- initialiseGame function initPlayerBulletsArray() for bullet = 1, maxPlayerBullets do playerBullets[bullet] = { position = { x = 0, y = 0 }, length = 5, colour = 14, speed = 2, active = false } end -- for end -- initPlayerBulletsArray