Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Build a Simon Game with JavaScript

Simon is the classic memory game: four coloured pads light up in a growing sequence, and you repeat it back. One wrong tap ends the run. It is a perfect beginner project because it teaches arrays, timers, event listeners

Simon is the classic memory game: four coloured pads light up in a growing sequence, and you repeat it back. One wrong tap ends the run. It is a perfect beginner project because it teaches arrays, timers, event listeners, and game state — all in under 150 lines of code.

The game has four pads, each with a colour and a tone. The computer picks a random pad, adds it to the sequence, and plays the sequence back with a short flash and beep for each step. The player then clicks the pads in the same order. Get it right and the sequence grows by one; get it wrong and the game ends with your score.

Here is the core of the game — the sequence logic:

const colors = ["green", "red", "yellow", "blue"];
let sequence = [];
let playerSequence = [];
let level = 0;

function nextRound() {
  playerSequence = [];
  level++;
  document.getElementById("level").textContent = `Level ${level}`;
  sequence.push(colors[Math.floor(Math.random() * 4)]);
  playSequence();
}

function playSequence() {
  let i = 0;
  const interval = setInterval(() => {
    flashPad(sequence[i]);
    i++;
    if (i >= sequence.length) clearInterval(interval);
  }, 600);
}

Each pad flash is a CSS class added for 300ms, plus a short beep from the Web Audio API so the game works without any audio files:

function flashPad(color) {
  const pad = document.getElementById(color);
  pad.classList.add("active");
  playTone(color);
  setTimeout(() => pad.classList.remove("active"), 300);
}

function playTone(color) {
  const freq = { green: 415, red: 310, yellow: 252, blue: 209 }[color];
  const ctx = new AudioContext();
  const osc = ctx.createOscillator();
  osc.frequency.value = freq;
  osc.connect(ctx.destination);
  osc.start();
  osc.stop(ctx.currentTime + 0.3);
}

Player input is just a click handler that checks each tap against the sequence. If the tap matches the current step, move on; if the whole sequence is matched, wait a second and start the next round. If it does not match, show the score and offer a restart:

document.querySelectorAll(".pad").forEach((pad) => {
  pad.addEventListener("click", () => {
    const color = pad.id;
    flashPad(color);
    playerSequence.push(color);
    const index = playerSequence.length - 1;
    if (playerSequence[index] !== sequence[index]) {
      alert(`Game over! You reached level ${level}`);
      sequence = [];
      level = 0;
      return;
    }
    if (playerSequence.length === sequence.length) {
      setTimeout(nextRound, 1000);
    }
  });
});

Two details make the game feel right. First, disable player clicks while the computer is playing the sequence — otherwise a fast clicker can corrupt the state. Second, keep the flash interval slightly longer than the flash itself so each step reads as a separate beat.

Ways to extend it: add a strict mode that restarts from level 1 on any mistake, store the best score in localStorage, add a speed that increases every five levels, or replace the tones with real sound files. The same sequence-and-replay pattern also powers typing games and rhythm trainers, so the logic transfers well beyond this one game.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.