Gamedev Phaser Content Kit

Article 12: Win the game

Implementing winning in our game is quite easy: if you happen to destroy all the bricks, then you win:

function ballHitBrick(ball, brick) {
    brick.kill();
    score += 10;
    scoreText.setText('Points: '+score);
    if(score === brickInfo.count.row*brickInfo.count.col*10) {
        alert('You won the game, congratulations!');
        location.reload();
    }
}

If your score is the same as the number of bricks initially available on the screen - times 10 as you score 10 points instead of 1 for every brick - then show the proper message.

Compare your code

You can check the finished code for this lesson for yourself in the live demo below, and play with it to understand better how it works:

Next steps

Both losing and winning is already implemented, so the core gameplay of our game is finished. Now we can add something extra, like three lives instead of one.