6 min read
🎮intermediate

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?

Game state is all the information that describes what is happening in your game at any moment. It includes: - The player's score - How many lives they have left - What level they are on - Whether the game is running, paused, or over - The position of every object on screen Managing game state is what turns a simple moving square into an actual game. Without it, there is no goal, no challenge, and no reason to keep playing.

A Complete Game State Object

javascript
// 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

A good game gets harder as you progress. Here are common ways to increase difficulty: - More enemies or faster enemies each level - Less time to complete the level - Fewer power-ups available - More complex obstacles or patterns - Coins or targets placed in harder-to-reach spots The trick is making each level feel challenging but fair. If it gets too hard too fast, players quit. If it stays easy, they get bored. Great game designers find the sweet spot where players are always just barely succeeding.
Pro Tip

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.

Build a Complete Mini-Game

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