From 5fa22ff259d4813812ffc23f28536c0337695721 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 10 May 2014 14:38:09 +0100 Subject: [PATCH] Add lives, score and explosions! --- images/explosion.png | Bin 0 -> 638 bytes javascripts/game.js | 87 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 images/explosion.png diff --git a/images/explosion.png b/images/explosion.png new file mode 100644 index 0000000000000000000000000000000000000000..f61659e3d6c226fe3c3e0ac9a1435d70e20787cc GIT binary patch literal 638 zcmV-^0)hRBP)00004XF*Lt006O% z3;baP00001b5ch_0Itp)=>Px#22e~?MF0Q*|NsA`*`M720007XQchC<`LgndfB*mj z2uVaiR9M69nNe=TAPj~hMJm_WNtUbhAk%U+j>17ASOMEf*Q7QOGSZHh{ts0?0``w_ zx=ucnb0$%lE_E~t>Gc7SjBx~{cekNaRNjlg6a}h2B!5S$Li*)W2hA!IgEt|7)pDvq zvkCyJCInaO(lrI4s2}2ECSD=rGd-rp_!!G8+={OX5WiGjQ2<}tfD9A-!ctd(j23?3 zDLlkyhgS)`5SQazn+8)xAx4kvRQ1lNCPDY^dm>M%94CrBV)_ zeQgDT*+Gs~1A;L{dCo$jB`OEA`%u&VknsP}%>FJY8jpbBc|JqvoEY5du$Zt|nDSKw z)qaoOJFY_}G*PM^S$`Bm$WrKJZUaIE5<1q(DN+ofh`>Bw@!s+GRt)zLjT^spoTNjj z2to+u`KHq!0zt>B3c);=L#V{YT6{+cS_>pfUiEC2ui literal 0 HcmV?d00001 diff --git a/javascripts/game.js b/javascripts/game.js index bdabbb2..4b6ebdc 100644 --- a/javascripts/game.js +++ b/javascripts/game.js @@ -5,15 +5,19 @@ function preload () { game.load.image('bullet', 'images/bullet.png'); game.load.image('alien', 'images/alien.png'); game.load.image('bomb', 'images/bomb.png'); + game.load.spritesheet('explosion', 'images/explosion.png', 80, 80); } -var bulletTime = 0; +var bulletTime = 0, + initialPlayerPosition = 512; + lives = 3, + score = 0; function create () { game.physics.startSystem(Phaser.Physics.ARCADE); // Initialize player - player = game.add.sprite(512, 540, 'ship'); + player = game.add.sprite(initialPlayerPosition, 540, 'ship'); player.anchor.setTo(0.5, 0.5); game.physics.enable(player, Phaser.Physics.ARCADE); @@ -47,11 +51,30 @@ function create () { bombs.setAll('checkWorldBounds', true); bombs.setAll('outOfBoundsKill', true); + // Initialize explosions + explosions = game.add.group(); + explosions.createMultiple(10, 'explosion'); + explosions.setAll('anchor.x', 0.5); + explosions.setAll('anchor.y', 0.5); + explosions.forEach(setupExplosion, this); + + // Text bits + var style = { font: "32px silkscreen", fill: "#666666", align: "right" }; + livesText = game.add.text(game.world.bounds.width - 16, 16, "LIVES: " + lives, style); + livesText.anchor.set(1, 0); + + scoreText = game.add.text(16, 16, "SCORE: " + score, style); + scoreText.anchor.set(0, 0); + // Setup controls cursors = game.input.keyboard.createCursorKeys(); fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); } +function setupExplosion (explosion) { + explosion.animations.add('explode'); +} + function update () { playerMovement(); @@ -105,28 +128,68 @@ function fireBullet () { function bulletHitsAlien (bullet, alien) { bullet.kill(); - alien.kill(); + explode(alien); + score += 10; + updateScoreText(); } function bombHitsPlayer (bomb, player) { bomb.kill(); - player.kill(); + explode(player); + lives -= 1; + updateLivesText(); + if (lives > 0) { + respawnPlayer(); + } + else { + gameOver(); + } +} + +function explode (entity) { + entity.kill(); + + // And create an explosion :) + var explosion = explosions.getFirstExists(false); + explosion.reset(entity.body.x + (entity.width / 2), entity.body.y + (entity.height / 2)); + explosion.play('explode', 30, false, true); +} + +function updateLivesText () { + livesText.text = "LIVES: " + lives; +} + +function updateScoreText () { + scoreText.text = "SCORE: " + score; +} + +function respawnPlayer () { + player.body.x = initialPlayerPosition; + setTimeout(function() { + player.revive(); + }, 1000); +} + +function gameOver () { + var style = { font: "bold 32px silkscreen", fill: "#ffffff", align: "center" }; + livesText = game.add.text(game.world.centerX, game.world.centerY, "GAME OVER", style); + livesText.anchor.set(0.5, 0.5); } function createAliens () { for (var y = 0; y < 4; y++) { for (var x = 0; x < 10; x++) { - var alien = aliens.create(x * 75, y * 50, 'alien'); + var alien = aliens.create(x * 72, y * 48, 'alien'); alien.anchor.setTo(0.5, 0.5); alien.body.moves = false; } } aliens.x = 64; - aliens.y = 50; + aliens.y = 96; // All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly. - var tween = game.add.tween(aliens).to( { x: 284 }, 2500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true); + var tween = game.add.tween(aliens).to( { x: 300 }, 2500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true); // When the tween loops it calls descend tween.onLoop.add(descend, this); @@ -134,7 +197,7 @@ function createAliens () { function handleBombs () { aliens.forEachAlive(function (alien) { - chanceOfDroppingBomb = game.rnd.integerInRange(0, 1000); + chanceOfDroppingBomb = game.rnd.integerInRange(0, 20 * aliens.countLiving()); if (chanceOfDroppingBomb == 0) { dropBomb(alien); } @@ -144,7 +207,7 @@ function handleBombs () { function dropBomb (alien) { bomb = bombs.getFirstExists(false); - if (bomb) { + if (bomb && player.alive) { // And drop it bomb.reset(alien.x + aliens.x, alien.y + aliens.y + 16); @@ -153,4 +216,8 @@ function dropBomb (alien) { } } -function descend () { aliens.y += 8; } +function descend () { + if (player.alive) { + aliens.y += 8; + } +}