-- title: Space Invaders -- author: Bob Grant -- desc: Lesson 7 -- script: lua playerShip = { position = { x = 120, y = 128 }, spriteNum = 0, minX = 0, maxX = 232, speed = 1, bulletOffset = { x = 4, y = 4 } } playerBullet = { position = { x = 0, y = 0 }, length = 5, colour = 14, speed = 2 } function TIC() cls() movePlayerShip() checkPlayerFire() 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() -- check if the fire button is pressed if (btnp(4)) then -- if pressed make a bullet appear -- at the player ship playerBullet.position = { x = playerShip.position.x + playerShip.bulletOffset.x, y = playerShip.position.y + playerShip.bulletOffset.y } end -- move the bullet up the screen playerBullet.position.y = playerBullet.position.y - playerBullet.speed end -- checkPlayerFire function drawPlayerBullet() -- draw player bullet -- line(startX, StartY, endX, endY, colour) line( playerBullet.position.x, playerBullet.position.y, playerBullet.position.x, playerBullet.position.y + playerBullet.length, playerBullet.colour ) end -- drawPlayerBullet function drawPlayerShip() spr(playerShip.spriteNum, playerShip.position.x, playerShip.position.y, 0) end -- drawPlayerShip