0,]']±>Sï}WÿÍu§ðp8·d%qy)6o;]ÉA¦ösï÷ôôô”°ÂVl†3 max) then value = max elseif (value < min) then value = min else value = value end return value end -- checkLimits function checkPlayerFire() local bulletFired = false local bullet = 1 -- if fire button is pressed then if (btnp(4)) then -- find a bullet that's ready to fire while (bullet <= maxPlayerBullets) and (bulletFired == false) do if (not playerBullets[bullet].active) then -- then init 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 sfx(1, 20, 20, 1, 15) end -- if not active bullet = bullet + 1 end -- while do loop end -- if button pressed end -- checkPlayerFire function movePlayerBullet() for bullet = 1, maxPlayerBullets do -- move bullet up the screen if (playerBullets[bullet].active) then playerBullets[bullet].position.y = playerBullets[bullet].position.y - playerBullets[bullet].speed if (playerBullets[bullet].position.y < 0) then playerBullets[bullet].active = false end -- bullet reaches the top end -- active end end -- movePlayerBullet function drawPlayerBullet() for bullet = 1, maxPlayerBullets do if (playerBullets[bullet].active) then -- draw player bullet -- line(startX, startY, endX, endY, colour) -- spr(id, x, y, colorkey, scale, flip, rotate, w, h) spr(52 + t%60//30, playerBullets[bullet].position.x, playerBullets[bullet].position.y, 0) end -- if end -- for loop end -- drawPlayerBullet function drawPlayerShip() spr(playerShip.spriteNumber + t%60//30, playerShip.position.x, playerShip.position.y, 0) end -- drawPlayerShip function initGame() playerShip.position.x = 120 playerShip.position.y = 128 playerLives = 3 playerScore = 0 alienMissiles = {} initPlayerBulletsArray() initAliens() end -- initGame function initPlayerBulletsArray() for bullet = 1, maxPlayerBullets do playerBullets[bullet] = { position = { x = 0, y = 0 }, height = 8, width = 4, length = 5, colour = 02, speed = 2, active = false } end -- for loop end -- InitPlayerBUlletsArray function initAliens() -- create alien grid for row = 1, alienRows do -- create a row of aliens aliens[row] = {} for column = 1, alienColumns do -- create alien aliens[row][column] = { position = { x = (column - 1) * alienHoriSpacing, y = alienTopOffset +(row - 1) * alienVertSpacing }, height = 8, width = 8, alive = true, alienBaseSprite = alienRowTypes[row].baseSprite } -- end row of aliens end -- for loop columns -- end alien grid end -- for loop rows end -- initAliens function drawAliens() for row = 1, alienRows do for column = 1, alienColumns do if (aliens[row][column].alive) then -- draws sprite on the screen ( sprite #, x, y) spr(aliens[row][column].alienBaseSprite + alienMoveFrameCounter, aliens[row][column].position.x, aliens[row][column].position.y, 0) -- work out if alien will fire if (math.random(50) <= fireProbability) then spawnAlienMissile(aliens[row][column].position) end end -- if alive or dead end -- for columns loop end -- for rows loop end -- drawAliens function moveAliens() local aliensAlive = 0 alienMoveCounter = alienMoveCounter - 1 if (alienMoveCounter <= 0) then -- do stepping sound alienStepSoundCounter = alienStepSoundCounter + 1 alienStepSoundCounter = alienStepSoundCounter % alienStepSoundNotes sfx(0, alienSoundBaseNote - alienStepSoundCounter, 6, 0, 6) -- increase the frame value alienMoveFrameCounter = alienMoveFrameCounter + 1 -- loop the frame counter alienMoveFrameCounter = alienMoveFrameCounter % alienMoveNumFrames if alienAtEdge() then -- move down for row = 1, alienRows do for column = 1, alienColumns do if (aliens[row][column].alive) then -- movement code here aliens[row][column].position.y = aliens[row][column].position.y + (alienVertSpeed) aliensAlive = aliensAlive + 1 if (aliens[row][column].position.y > alienMaxY) then -- game over! Alien landed gameState = stateGameOver end -- if landed end -- if alive end -- for columns loop end -- for rows loop alienDirection = -alienDirection else for row = 1, alienRows do for column = 1, alienColumns do if (aliens[row][column].alive) then -- movement code here aliens[row][column].position.x = aliens[row][column].position.x + (alienSpeed * alienDirection) aliensAlive = aliensAlive + 1 end -- if alive end -- for columns loop end -- for rows loop end -- if alienMoveDelay = calcAlienSpeed(aliensAlive) alienMoveCounter = alienMoveDelay --resets move counter if (aliensAlive == 0) then timer = 0 gameState = stateAllAliensDead end -- if all dead state end -- if alienMoveCounter end -- moveAliens function calcAlienSpeed(aliensAlive) local delay if (aliensAlive <= 1) then delay = 1 fireProbability = 5 elseif (aliensAlive <= 3) then delay = 3 fireProbability = 3 elseif (aliensAlive <= 4) then delay = 3 fireProbability = 1 elseif (aliensAlive <= 8) then delay = 8 fireProbability = 1 elseif (aliensAlive <= 20) then delay = 15 fireProbability = 1 elseif (aliensAlive <= 30) then delay = 30 fireProbability = 1 elseif (aliensAlive <= 40) then delay = 40 fireProbability = 1 else delay = 50 end -- if return delay end -- calcAlienSpeed() function alienAtEdge() -- for loop each alien for row = 1, alienRows do for column = 1, alienColumns do -- if alien is alive then if (aliens[row][column].alive) then -- if alien going right +1 if (alienDirection == 1) then -- if alien x + step > maxX then if(aliens[row][column].position.x + alienSpeed) > alienMaxX then return true end -- if x limit right else if(aliens[row][column].position.x - alienSpeed) < alienMinX then return true end -- if x limit left end -- left or right end -- if alive end -- for columns end -- for rows return false -- no alien bump end -- alienAtEdge function checkCollision(object1, object2) -- object.position position.x position.y -- object.width -- object.height local object1Left = object1.position.x local object1Right = object1.position.x + object1.width - 1 local object1Top = object1.position.y local object1Bottom = object1.position.y + object1.height - 1 local object2Left = object2.position.x local object2Right = object2.position.x + object2.width - 1 local object2Top = object2.position.y local object2Bottom = object2.position.y + object2.height - 1 if (object1Left < object2Right) and (object1Right > object2Left) and (object1Top < object2Bottom) and (object1Bottom > object2Top) then return true else return false end -- if collision end -- checkCollision function checkBulletCollisions() local bulletHasHitAlien = false for bullet = 1, maxPlayerBullets do if (playerBullets[bullet].active) then for row = 1, alienRows do for column = 1, alienColumns do if (aliens[row][column].alive) then bulletHasHitAlien = checkCollision( playerBullets[bullet], aliens[row][column] ) if (bulletHasHitAlien) then -- hit! alien dies aliens[row][column].alive = false -- bullet disappears playerBullets[bullet].active = false alienExplosion(aliens[row][column].position) playerScore = playerScore + alienRowTypes[row].score sfx(3, 58, 20, 2, 7) end -- if hit! end -- if alien alive end -- column loop end -- for row loop end -- if bullet active end -- for bullet loop end -- checkBulletCollisions function alienExplosion(exPosition) local explosion = { position = exPosition, ticCounter = 0, totalTics = 30, baseSprite = 84, numberFrames = 10 } table.insert(explosions, explosion) -- add explosion into array end -- alienExplosion function drawExplosions() local spriteNumber for index, explosion in ipairs(explosions) do spriteNumber = (explosion.ticCounter % explosion.numberFrames) + explosion.baseSprite spr(spriteNumber, explosion.position.x, explosion.position.y, 0) explosion.ticCounter = explosion.ticCounter + 1 if (explosion.ticCounter > explosion.totalTics) then table.remove(explosions, index) end -- if ticCounter end -- for loop end -- drawExplosions function drawScoreBoard() print("SCORE : "..playerScore, 0, 0, 12) for counter=0, playerLives - 2 do spr(0, 232 - (counter * 12), 0) end -- for loop end -- drawScoreBoard function spawnAlienMissile(alienPosition) if (#alienMissiles < maxAlienMissiles) then local missile = { position = { x = alienPosition.x + 4, y = alienPosition.y + 8 }, height = 8, width = 4, speed = math.random(5, 15)/10 } table.insert(alienMissiles, missile) end --if end -- spawnAlienMissile function drawAlienMissiles() for index, missile in ipairs(alienMissiles) do -- line(startX, StartY, endX, endY, colour) spr(80 + t%60//30, missile.position.x, missile.position.y, 0) end -- for end -- drawAlienMissiles function moveAlienMissiles() for index, missile in ipairs(alienMissiles) do missile.position.y = missile.position.y + missile.speed if (missile.position.y >= 136) then table.remove(alienMissiles, index) end -- if end -- for end -- moveAlienMissiles function checkMissileCollisions() for index, missile in ipairs(alienMissiles) do if (checkCollision(missile, playerShip)) then -- player ship has been hit local explosion = { position = playerShip.position, ticCounter = 0, totalTics = 60, baseSprite = 84, numberFrames = 10 } table.insert(explosions, explosion) -- add explosion into array table.remove(alienMissiles, index) sfx(3, 58, 20, 2, 15) jumpToNewLifeState() end -- if collosion end -- for end -- checkMissileCollisions function jumpToNewLifeState() gameState = stateNewLife timer = 0 playerLives = playerLives - 1 end -- jumpToNewLifeState