Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 789346 of Track the score and win

  • Revision slug: Games/Workflows/2D_Breakout_game_pure_JavaScript/Track_the_score_and_win
  • Revision title: Track the score and win
  • Revision id: 789346
  • Created:
  • Creator: trevorh
  • Is current revision? No
  • Comment Remove iframe use

Revision Content

{{draft}}

8. Track the score and win

This is the 8th step out of 10 of the Gamedev Canvas tutorial.

Destroying the bricks is really cool, but to be even more awesome we could keep track of the points awarded for every brick we hit. That way we can see our score throughout the game, and eventually impress our friends. We will need a variable for that:

var score = 0;

And then there's the drawScore() function:

function drawScore() {
    ctx.font = "16px Arial";
    ctx.fillStyle = "#0095DD";
    ctx.fillText("Score: "+score, 8, 20);
}

Drawing the font is similar to drawing a shape. The font definition looks exactly like the one in CSS - we can set the size and font type in the font method. Then there's fillStyle for the color of the font and fillText to set the text. First parameter is the text itself where we can add our variable to show the current number of points, and the last two parameters are the coordinates. Adding that to our draw() function executed on every frame makes it up to date with everything else on the screen:

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawBricks();
    drawBall();
    drawPaddle();
    drawScore();
    collisionDetection();
    // ...
}

Collecting the points works good, but we won't be removing them forever - they will end at some point. It's the main purpose of the game after all, so on every brick we hit we will increase the score and check if we collected all the points already:

score++;
if(score == brickRowCount*brickColumnCount) {
    alert("YOU WIN, CONGRATS!");
    document.location.reload();
}

Let's add that to the collisionDetection() function:

function collisionDetection() {
    for(c=0; c<brickColumnCount; c++) {
        for(r=0; r<brickRowCount; r++) {
            var b = bricks[c][r];
            if(b.status == 1) {
                if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
                    dy = -dy;
                    b.status = 0;
                    score++;
                    if(score == brickRowCount*brickColumnCount) {
                        alert("YOU WIN, CONGRATS!");
                        document.location.reload();
                    }
                }
            }
        }
    }
}

Thanks to this we'll be able to actually win the game when we destroy all the bricks, which is quite important when it comes to games. The document.location.reload() will reload the page and start the game again after we click the confirmation alert.

{{JSFiddleEmbed("https://jsfiddle.net/end3r/mvfkcydz/","","320")}}

Exercise: add more points per brick hit, print out the number of collected points in the alert.

Now, you can get back to this tutorial's index page or continue to the ninth chapter: Mouse controls.

Revision Source

<p>{{draft}}</p>

<h2 id="8._Track_the_score_and_win">8. Track the score and win</h2>

<div class="summary">
<p>This is the <strong>8th step</strong> out of 10 of the <a href="https://developer.mozilla.org/en-US/docs/Games/Workflows/Breakout_game_from_scratch">Gamedev Canvas tutorial</a>.</p>
</div>

<p>Destroying the bricks is really cool, but to be even more awesome we could keep track of the points awarded for every brick we hit. That way we can see our score throughout the game, and eventually impress our friends. We will need a variable for that:</p>

<pre class="brush: js">
var score = 0;</pre>

<p>And then there's the <code>drawScore()</code> function:</p>

<pre class="brush: js">
function drawScore() {
    ctx.font = "16px Arial";
    ctx.fillStyle = "#0095DD";
    ctx.fillText("Score: "+score, 8, 20);
}</pre>

<p>Drawing the font is similar to drawing a shape. The font definition looks exactly like the one in CSS - we can set the size and font type in the font method. Then there's <code>fillStyle</code> for the color of the font and <code>fillText</code> to set the text. First parameter is the text itself where we can add our variable to show the current number of points, and the last two parameters are the coordinates. Adding that to our <code>draw()</code> function executed on every frame makes it up to date with everything else on the screen:</p>

<pre class="brush: js">
function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawBricks();
    drawBall();
    drawPaddle();
    drawScore();
    collisionDetection();
    // ...
}</pre>

<p>Collecting the points works good, but we won't be removing them forever - they will end at some point. It's the main purpose of the game after all, so on every brick we hit we will increase the score and check if we collected all the points already:</p>

<pre class="brush: js">
score++;
if(score == brickRowCount*brickColumnCount) {
    alert("YOU WIN, CONGRATS!");
    document.location.reload();
}</pre>

<p>Let's add that to the <code>collisionDetection()</code> function:</p>

<pre class="brush: js">
function collisionDetection() {
    for(c=0; c&lt;brickColumnCount; c++) {
        for(r=0; r&lt;brickRowCount; r++) {
            var b = bricks[c][r];
            if(b.status == 1) {
                if(x &gt; b.x &amp;&amp; x &lt; b.x+brickWidth &amp;&amp; y &gt; b.y &amp;&amp; y &lt; b.y+brickHeight) {
                    dy = -dy;
                    b.status = 0;
                    score++;
                    if(score == brickRowCount*brickColumnCount) {
                        alert("YOU WIN, CONGRATS!");
                        document.location.reload();
                    }
                }
            }
        }
    }
}</pre>

<p>Thanks to this we'll be able to actually win the game when we destroy all the bricks, which is quite important when it comes to games. The <code>document.location.reload()</code> will reload the page and start the game again after we click the confirmation alert.</p>

{{JSFiddleEmbed("https://jsfiddle.net/end3r/mvfkcydz/","","320")}}

<div class="summary">
<p>Exercise: add more points per brick hit, print out the number of collected points in the alert.</p>
</div>

<p>Now, you can get back to <a href="https://developer.mozilla.org/en-US/docs/Games/Workflows/Breakout_game_from_scratch">this tutorial's index page</a> or continue to the ninth chapter: <a href="https://developer.mozilla.org/en-US/docs/Games/Workflows/Breakout_game_from_scratch/Mouse_controls">Mouse controls</a>.</p>
Revert to this revision