Score & Lives — Game State Management
Learn how to track score, lives, levels, and game state to turn a simple demo into a real game with progression.
What is Game State?
A Complete Game State Object
// Keep all game data in one place
let gameState = {
score: 0,
lives: 3,
level: 1,
status: 'playing', // 'playing', 'paused', 'gameover', 'won'
player: { x: 200, y: 300, speed: 5 },
coins: [],
enemies: []
};
function updateHUD() {
// Draw score and lives on screen
ctx.fillStyle = 'white';
ctx.font = '20px Arial';
ctx.fillText('Score: ' + gameState.score, 20, 30);
ctx.fillText('Lives: ' + gameState.lives, 20, 60);
ctx.fillText('Level: ' + gameState.level, 20, 90);
}
function loseLife() {
gameState.lives--;
if (gameState.lives <= 0) {
gameState.status = 'gameover';
}
}Level Progression
Save the game state to localStorage so players can come back later! Just use JSON.stringify(gameState) to save it and JSON.parse(localStorage.getItem('save')) to load it. Even simple games feel more polished when they remember your high score.
Create a simple coin collector game that uses everything from the last three lessons: a player that moves with arrow keys, coins that appear randomly, collision detection to collect them, a score counter, 3 lives, and a game over screen. Bonus features: add enemies that move randomly and cost a life on contact, add level progression where more enemies spawn each level, and save the high score to localStorage!
Ready to build?
Put what you learned into practice — pick a project and start coding.
Start Building Free