Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introducing JavaScript Finishing the Game Final Code Challenge

I don't know the anwswer

I don't know the answer

game.js
var game;
var player;
var platforms;
var badges;
var stars;
var poisons;
var cursors;
var jumpButton;
var scoreText;
var livesText;
var finalMessage;
var won = false;
var gameOver = false;
var currentScore = 0;
var lives = 3;
var winningScore = 100;

function createStars() {
    stars = game.add.physicsGroup();

    starCreate(425, 100, 'star');
    starCreate(315, 100, 'star');
    starCreate(175, 200, 'star');
        starCreate(500, 500, 'star');
    starCreate(675, 400, 'star');
    starCreate(775, 500, 'star');
    starCreate(750, 300, 'star');
}

function createPoisons() {
    poisons = game.add.physicsGroup();

    poisonCreate(675, 275, 'poison');
    poisonCreate(450, 375, 'poison');
    poisonCreate(375, 475, 'poison');

}


function createPlatforms() {
    platforms = game.add.physicsGroup();

    platforms.create(300, 150, 'platform');
    platforms.create(400, 250, 'platform');
    platforms.create(100, 250, 'platform');
    platforms.create(500, 350, 'platform');
    platforms.create(400, 450, 'platform');
    platforms.create(300, 550, 'platform');

    platforms.setAll('body.immovable', true);
}

function starCreate(left, top, starImage) {
    var star = stars.create(left, top, starImage);
    star.animations.add('spin');
    star.animations.play('spin', 8, true);
}

function poisonCreate(left, top, poisonImage) {
    var poison = poisons.create(left, top, poisonImage);
    poison.animations.add('bubble');
    poison.animations.play('bubble', 8, true);
}

function starCollect(player, star) {
    star.kill();
    currentScore = currentScore + 20;
    if (currentScore === winningScore) {
        won = true;
    }
}

function poisonCollect(player, poison) {
    poison.kill();
    lives = lives + 1;
    if (lives === 0) {
        player.kill();
        gameOver = true;
    } 
}

window.onload = function () {

    game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });

    function preload() {

        game.stage.backgroundColor = '#89889c';

        //Load images
        game.load.image('platform', 'platform.png');
        //Load spritesheets
        game.load.spritesheet('player', 'mikethefrog.png', 50, 60);
        game.load.spritesheet('poison', 'poison.png', 24, 62);
        game.load.spritesheet('star', 'star.png', 54, 52);
        game.load.spritesheet('badge', 'badge.png', 42, 54);
    }

    function create() {

        player = game.add.sprite(150, 600, 'player');
        player.animations.add('walk');
        player.anchor.setTo(0.5, 1);

        game.physics.arcade.enable(player);

        player.body.collideWorldBounds = true;
        player.body.gravity.y = 500;

        createStars();
        createPoisons();
        createPlatforms();

        cursors = game.input.keyboard.createCursorKeys();
        jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

        scoreText = game.add.text(16, 16, "SCORE: " + currentScore, { font: "24px Arial", fill: "white" });
        livesText = game.add.text(685, 16, "LIVES: " + lives, { font: "24px Arial", fill: "white" });

        finalMessage = game.add.text(game.world.centerX, 250, "", { font: "48px Arial", fill: "white" });
        finalMessage.anchor.setTo(0.5, 1);
    }

    function update() {
        scoreText.text = "SCORE: " + currentScore;
        livesText.text = "LIVES: " + lives;

        game.physics.arcade.collide(player, platforms);
        game.physics.arcade.overlap(player, stars, starCollect);
        game.physics.arcade.overlap(player, poisons, poisonCollect);

        player.body.velocity.x = 0;

        if (cursors.left.isDown) {
            player.animations.play('walk', 10, true);
            player.body.velocity.x = -350;
            player.scale.x = - 1;
        }
        else if (cursors.right.isDown) {
            player.animations.play('walk', 10, true);
            player.body.velocity.x = 350;
            player.scale.x = 1;
        }
        else {
            player.animations.stop();
        }

        if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down)) {
            player.body.velocity.y = -400;
        }
        if (won) {
            finalMessage.text = "YOU WIN!!!";
        }
        if (gameOver) {
            finalMessage.text = "GAME OVER!!!";
        }

    }

    function render() {

    }

};

5 Answers

Adam Beer
Adam Beer
11,314 Points

Hi Will! Please look again this video.

Click the preview button to view the game. Currently, there are 3 stars on screen. Click the Editor button and add two more stars, bringing the total number of stars to 5. Make sure none of the stars overlap with any other stars.

The challenge is asking to you add two more stars, bringing the total number of stars to 5.

I got this part but not the second part where it ask to change score

Adam Beer
Adam Beer
11,314 Points

Challenge Task 2 of 2

There are bottles of poison on screen. The player's life goes up when the player touches a bottle of poison. Change the game so the player's life count decreases by one when a bottle of poison is touched. Hint: the code to change is between lines 71 and 78.

So let's try this. For example. We change the lives. Thus it has decreased and doesn't increase

lives = lives - 1;
Adam Beer
Adam Beer
11,314 Points

And now you have 7 stars, but the question is asking for you "bringing the total number of stars to 5" so please fixed your code and delete 2 stars.

I read what you guys are saying and I watched the video. I still don't understand what to do on line 71 or 72 70.function poisonCollect(player, poison) { 71.poison.kill(); 72.lives = lives + 1; 73.if (lives === 0) { 74.player.kill(); 75.gameOver = true;

  1. }
    1. }
Adam Beer
Adam Beer
11,314 Points

Challenge Task 2 of 2

There are bottles of poison on screen. The player's life goes up when the player touches a bottle of poison. Change the game so the player's life count decreases by one when a bottle of poison is touched. Hint: the code to change is between lines 71 and 78.

This section is add to live when the player touches a bottle of poison. Rewrite the + symbol to - symbol.

function poisonCollect(player, poison) {
    poison.kill();
    lives = lives - 1;
    if (lives === 0) {
        player.kill();
        gameOver = true;
    }
}

Now when a player touches a glass of poison its life is decreasing. Hope this help.