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.
Pokémon-6 rewards winning. Defeating a wild Pokémon earns experience, which fills a bar under the player’s health. A full bar levels the Pokémon up, raising its stats.
Experience
Each Pokémon keeps a running total of all the experience it has ever earned, currentExperience. Each level starts at a total given by experienceFromLevel() in src/objects/Pokemon.js. This game uses the Medium Fast curve from the real games, where reaching a level takes that level cubed:
173experienceFromLevel(level) {174 return level === 1 ? 0 : level * level * level;175}Level 2 starts at 8, level 3 at 27, level 4 at 64, and level 10 at 1,000, so each level takes more experience than the one before it. The constructor sets three fields from the curve:
70this.targetExperience = this.experienceFromLevel(level + 1);71this.currentExperience = this.experienceFromLevel(level);72this.levelExperience = this.experienceFromLevel(level);levelExperienceis where the current level starts.targetExperienceis where the next level starts.currentExperiencebegins at the start of the level.
The bar only shows progress through the current level. getLevelProgress() takes away levelExperience, so a level 3 Pokémon with 40 experience is 13 out of 37 along, which is the same as 40 - 27 out of 64 - 27.
Defeating a Pokémon awards experience with calculateExperienceToAward(). Each species in pokemonDefinitions has a baseExperience, so a new species needs one too, and a higher level gives more of it.
Victory
processVictory() in src/objects/BattleTurn.js now awards experience and checks for a level-up once the opponent has fainted:
200await this.processExperience();201await this.processLevelUp();processExperience() says how much was earned, adds it with gainExperience(), and awaits animateExperience() on the player’s panel, which fills the bar with the experience sound playing. If the bar fills all the way, animateExperience() in src/user-interface/battle/PlayerPanel.js finishes with a ding, and only resolves once the ding ends:
53sounds.play(SoundName.ExperienceGain);54await this.experienceBar.setValue(this.pokemon.getLevelProgress());55sounds.stop(SoundName.ExperienceGain);56
57if (this.pokemon.canLevelUp()) {58 await sounds.playAsync(SoundName.ExperienceFull);59}Levelling Up
processLevelUp() levels up for as long as the Pokémon has enough experience:
232while (playerPokemon.canLevelUp()) {233 await this.levelUp();234}levelUp() in src/objects/Pokemon.js adds 1 to level. It also has to move levelExperience and targetExperience along to the new level and recalculate the stats, or canLevelUp() stays true. You write that part in the challenge below.
It’s a loop, not an if, because one battle against a much stronger Pokémon can pass two levels at once. Each pass plays the level-up jingle, shows the message, and slides the bar into the new level:
243const jingle = game.sounds.playAsync(SoundName.LevelUp);244
245playerPokemon.levelUp();246
247await Promise.all([248 jingle,249 this.battle.showMessage(250 `${playerPokemon.name} grew to LV. ${playerPokemon.level}!`,251 ),252]);253
254await this.battle.panels.player.animateNextLevel();Promise.all() waits for both the jingle and the message, so pressing Enter early doesn’t cut the jingle off, and the next level-up can’t start over it.
The Experience Bar
ExperienceProgressBar in src/user-interface/battle/ExperienceProgressBar.js is a ProgressBar, like the health bar, so it slides with the setValue() you wrote in Pokémon-5. Its max is the experience the current level needs, targetExperience - levelExperience, and its value is getLevelProgress(), which stops at a full bar.
After a level-up, animateNextLevel() in src/user-interface/battle/PlayerPanel.js empties the bar, sets its max to the new level’s size, and slides it again:
68animateNextLevel() {69 const { experienceBar, healthBar, pokemon } = this;70
71 healthBar.max = pokemon.health;72 experienceBar.value = 0;73 experienceBar.max = pokemon.targetExperience - pokemon.levelExperience;74
75 return this.animateExperience();76}So the bar fills, empties, and slides on into the new level with whatever experience was left over. The health bar’s max follows the Pokémon’s new maximum health too. The stats screen shows the experience as an EXP row.
Give a level 5 Charmander some experience. 25 stays inside the level. 250 is enough for two levels, so the while loop in processLevelUp() runs twice. Your own game levels up once per win. Your starter begins at level 2, 15 experience into the 19 that level 3 needs. A level 1 wild Pokémon is worth 8 experience, so one win is enough:
Level up
Win a battle, and your Pokémon levels up again every time you press Enter, because levelUp() in src/objects/Pokemon.js only raises the level. targetExperience never moves, so canLevelUp() stays true forever. That battle never ends, so reload the preview once you’ve seen it.
Under the TODO, move the level’s start and end to the new level, then recalculate the stats for it:
- Set
this.levelExperienceto the experience the new level starts at, fromthis.experienceFromLevel(). - Set
this.targetExperienceto the experience the level after it starts at. - Recalculate the stats with
this.calculateStats().
Then win a battle, which is enough for your level 2 starter to level up, and check its stats with Escape. The Challenge tab gets a ✓ once you’re back on the map with a level-up behind you.
this.level++ runs first, so this.level is already the new level when your lines run.
The constructor in src/objects/Pokemon.js sets levelExperience and targetExperience from level the same way. Copy those two lines and use this.level in place of level.
Replace the TODO with:
this.levelExperience = this.experienceFromLevel(this.level);this.targetExperience = this.experienceFromLevel(this.level + 1);this.calculateStats();