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-3 gives the player a Pokémon. A new game picks one of the three starters at random, and hitting the Escape key shows its stats. The NPC’s line in src/config/characters.js and the TRAINER TIPS sign in src/config/signs.js now point to Escape.
Pokémon as Data
A Pokémon is simply a container of numbers. A Charmander and a Squirtle behave the same way and only their numbers differ, so there’s no class per Pokémon. Each species, such as Charmander, is listed once in pokemonDefinitions in src/config/pokemon.js. Every Charmander in the game is a separate Pokemon object built from that one entry:
7export const pokemonDefinitions = {8 Bulbasaur: {9 id: 1,10 baseHealth: 45,11 baseAttack: 49,12 baseDefense: 49,13 baseSpeed: 45,14 },idis the Pokémon’s number in the Pokédex: 1 for Bulbasaur, 4 for Charmander, and 7 for Squirtle.- The base stats are the species’ strengths, and they’re the same for every Charmander. They come from the real games, so Bulbapedia has them for any Pokémon you add.
For now, adding a species takes one entry here and nothing else. Later lessons add a few more fields to each entry.
Sprites by Number
assets/images/pokemon.png holds every Pokémon in Pokédex order, 15 to a row, each in a block of the same size with its front sprite, back sprite, and two icon sprites. loadPokemonSprites() in src/frames.js finds a Pokémon’s block from its id, the same way loadTileSprites() cuts each tile from its sheet:
71export function loadPokemonSprites(game, id) {72 const sheet = game.images.get(ImageName.Pokemon);73 const blockX = ((id - 1) % POKEDEX_COLUMNS) * POKEDEX_BLOCK_WIDTH;74 const blockY = Math.floor((id - 1) / POKEDEX_COLUMNS) * POKEDEX_BLOCK_HEIGHT;The Pokédex starts at 1, so id - 1 is the block’s position on the sheet. The front and back sprites wait for battles in Pokémon-4. The stats screen uses the icons.
Pick a Pokédex number to see which block it finds. % 15 gives the column, and dividing by 15 and rounding down gives the row.
pokemon.png has a 10-pixel border around its edge. blockX and blockY don’t skip it, so each block they find starts 10 pixels above and to the left of its art. The offsets inside the block, such as POKEDEX_SPRITE_X (11), include those 10 pixels:
Stats
Pokemon in src/objects/Pokemon.js takes a definition and a level, and works out its stats. Besides the base stats, each Pokémon rolls a random individual value (IV) from 0 to 31 for each stat when it’s created, so no two Charmanders are quite the same:
29// @see https://bulbapedia.bulbagarden.net/wiki/Individual_values30this.healthIV = getRandomPositiveInteger(0, 31);31this.attackIV = getRandomPositiveInteger(0, 31);32this.defenseIV = getRandomPositiveInteger(0, 31);33this.speedIV = getRandomPositiveInteger(0, 31);calculateStats() turns the base stat, the IV, and the level into the stat itself, with the same formulas as the real games. calculateStat() is for Attack, Defense, and Speed, and calculateHealth() is for maximum health. A higher base stat, a higher IV, and a higher level each give a higher stat. The formulas are written so that a stat grows steadily as the Pokémon levels up, which Pokémon-6 relies on.
Change the inputs to see what each one does. The sliders start on Charmander’s base stat at level 50, where each input makes a visible difference. The triangle on the level slider marks level 1, the starter’s level, where the other inputs barely change a stat. Switch stat to health to see calculateHealth(), which grows faster than the other stats because it adds the level too:
currentHealth starts at health, the maximum. Battles lower currentHealth, and health only changes with the level.
The Factory
createPokemon() in src/services/pokemonFactory.js is the Factory pattern from Zelda’s enemies and Angry Birds’ birds. It makes a Pokémon from its name, so the code asking for one doesn’t need to know where definitions live or what Pokemon’s constructor takes. Player in src/objects/Player.js uses it for the party:
33// There's only ever one Pokemon in the party, but this is an array so34// that it can be extended to contain more.35this.party = [createPokemon(game, pickRandomElement(pokemonNames))];pokemonNames is every key in pokemonDefinitions, so the starter is a random one of the three. Wild Pokémon in Pokémon-4 come from the same factory.
The Stats Screen
Escape in PlayerIdleState in src/states/player/PlayerIdleState.js shows the first Pokémon in the party:
59if (input.isKeyPressed(Input.KEYS.ESCAPE)) {60 void PokemonStatsState.show(this.game, player.party[0]);61
62 return;63}PokemonStatsState in src/states/PokemonStatsState.js is an overlay like DialogueState. It has a static show(), draws a PokemonStatsPanel, and calls this.close() when Escape is pressed again. The panel draws each stat as a row, with its label on the left and its value on the right. The icon bobs faster the healthier the Pokémon is, which shows once battles can hurt it.
Build the right Pokémon
Press Escape to see your Pokémon’s stats. Whatever its name, it’s a Ditto in disguise, with a ? after its name, because createPokemon() in src/services/pokemonFactory.js always builds from DITTO.
- Have the
returnunder the TODO build thePokemonfrom its real definition inpokemonDefinitionsand its realname, with no?. The constructor insrc/objects/Pokemon.jsshows the order it takes its arguments in. - Delete the
DITTOconstant at the top of the file, since nothing uses it now.
Then press Escape to check the stats. The screen shows your starter’s name without the ?, its own icon, and its own numbers instead of Ditto’s. The Challenge tab gets a ✓ once you’ve checked them with the right Pokémon.
pokemonDefinitions[name] looks up the entry under name, like pokemonDefinitions.Bulbasaur. It takes the place of DITTO in the call.
The Player constructor in src/objects/Player.js builds your starter once, when the game starts. Reload the preview after changing createPokemon(), so the game builds it again.
Replace the return with return new Pokemon(game, name, pokemonDefinitions[name], level);, then delete DITTO.