Code Studio · Level 9
Game Builder
Students add game actions that change state and print the player inventory.
Build a player inventory.
Example code
class Player {
constructor(name) {
this.name = name;
this.score = 0;
this.inventory = [];
}
collect(item, points) {
this.inventory.push(item);
this.score = this.score + points;
say(this.name + " found " + item);
}
showStatus() {
say("Score: " + this.score);
say("Items: " + this.inventory.join(", "));
}
}
let explorer = new Player("Nova");
explorer.collect("star", 10);
explorer.collect("badge", 5);
explorer.showStatus();
Program result
PLAYER 1
SCORE 0
- Run your code to see output.
Type real JavaScript, then run it.
Game builder complete.
You built a small game object that tracks score and inventory.
Try Game Challenge