This lesson was updated. Reset to the new version?
This browser isn't keeping your edits, so they're lost when you leave the page.
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()insrc/LevelMaker.jsnow gives bricks a tier as well as a colour. Thelevelpassed 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.74const design = levels[(level - 1) % levels.length];75const tier = Math.min(3, Math.floor((level - 1) / levels.length));levelstarts at 1 but arrays start at 0, so both lines uselevel - 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 thehit()method such that it only toggles theinPlayflag 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.jsthat we are now passing in an additional field toServeStateupon 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’sinPlayflag has been toggled tofalse. - 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.jscontains 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 oflevel. 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.
- Notice that we’re also passing the incremented level into