Reset this lesson?

Your edits to this lesson are deleted and its starting files load again.

Breakout-5 lets the player beat a level and move on to the next design. We also now differentiate between the different tiers of bricks, making updates to the gameplay and the scoring as a result.

Important Code

  • createMap() in src/LevelMaker.js now gives bricks a tier as well as a colour. The level passed in picks the design, and once the player has played every design, the designs repeat with tougher bricks:

    src/LevelMaker.js
    73
    // After the last design, the designs repeat with tougher bricks, up to the toughest tier.
    74
    const design = levels[(level - 1) % levels.length];
    75
    const tier = Math.min(3, Math.floor((level - 1) / levels.length));
    • level starts at 1 but arrays start at 0, so both lines use level - 1.
    • % (remainder) wraps the level back to the first design after the last one. With 5 designs, level 6 uses design 0 again.
    • Dividing by the number of designs counts how many times the player has been through all of them. That count is the tier, and Math.min() stops it at 3, the toughest tier in the sprite sheet.
  • Take a look at Brick.js. You’ll notice that we’ve updated the hit() method such that it only toggles the inPlay flag if the brick being hit is of the lowest tier. Otherwise, it simply decrements the tier of the brick.

    Brick.js
    if (this.tier > 0) {
    this.tier--;
    } else {
    this.inPlay = false;
    }
    • The tier of the brick just means how many hits that brick will take to break.
  • We also use the tier to calculate which brick sprite to render:

    Brick.js
    this.sprites[this.colour * (this.numberOfColours - 1) + this.tier].render(
    this.x,
    this.y
    );
  • In PlayState.js, we’ve updated the scoring algorithm to attribute higher values to bricks of higher tiers. Whereas previously we always added 10 points for any ball/brick collision, we now take tier into account:

    PlayState.js
    this.score += this.baseScore * (brick.tier + 1);
  • You’ll notice in TitleScreenState.js that we are now passing in an additional field to ServeState upon transition, namely, level.

    • We’ll continue to do this once each level has been beaten, incrementing level as needed.
    • In PlayState.js, we’ve written a new method, checkVictory(), that checks if the current level has been beaten, by checking if every brick’s inPlay flag has been toggled to false.
    • This method is called within Playstate::update(), since it makes sense to check if the level has been beaten each time a brick is hit.
  • VictoryState.js contains the code for producing our victory screen. It is only ever activated when the user beats a level. This is also where we increment the value of level. Upon increasing the level, we must generate a new brick layout:

    VictoryState.js
    stateMachine.change('serve', {
    ball: this.ball,
    bricks: LevelMaker.createMap(this.level + 1),
    paddle: this.paddle,
    health: this.health,
    score: this.score,
    userInterface: this.userInterface,
    level: this.level + 1,
    });
    • Notice that we’re also passing the incremented level into LevelMaker.createMap(), so the player gets the next design, and tougher bricks once the designs repeat.
Powered by WebContainers
Files
Preparing Environment