Portable build

The arcade as one HTML file

One document, no libraries, no build step. Copy the code below into any HTML editor or emulator, or download it and open it in a browser. It holds five of the six games — Neon Snake, Block 2048, Memory Drift, Brick Storm and Reflex Grid — with the same rules and scoring as the versions here. GS;GO needs the full app.

Live preview
arcade.html20,858 characters
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Nebula Arcade</title>
<style>
  * { box-sizing: border-box; }
  body {
    margin: 0; min-height: 100vh; padding: 20px 16px 40px;
    display: flex; flex-direction: column; align-items: center;
    background: radial-gradient(circle at 12% -8%, rgba(111,0,255,.18), transparent 42%),
                radial-gradient(circle at 92% -4%, rgba(0,255,194,.12), transparent 38%), #05070a;
    color: #e2e8f0;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  }
  h1 { margin: 0; font-size: 16px; letter-spacing: .32em; text-transform: uppercase; }
  .sub { margin: 6px 0 18px; font-size: 10px; letter-spacing: .22em; text-transform: uppercase; color: #7c8aa0; }
  .tabs { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 18px; }
  .tab { cursor: pointer; border: 1px solid #1e293b; background: #0b1220; color: #93a1b5;
         padding: 8px 11px; font: inherit; font-size: 10px; letter-spacing: .18em; text-transform: uppercase; border-radius: 2px; }
  .tab.on { border-color: #00ffc2; color: #00ffc2; background: rgba(0,255,194,.08); }
  #stage { position: relative; background: #0b1220; border: 1px solid #1e293b; border-radius: 2px; padding: 12px; }
  .hud { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 8px;
         font-size: 10px; letter-spacing: .18em; text-transform: uppercase; color: #7c8aa0; }
  .hud b { color: #00ffc2; font-size: 13px; }
  canvas { display: block; background: #05070a; border: 1px solid #1e293b; border-radius: 2px; touch-action: none; max-width: 100%; }
  .board, .grid { display: grid; gap: 6px; background: #0f172a; padding: 6px; border-radius: 3px; }
  .cell { width: 62px; height: 62px; border-radius: 3px; display: grid; place-items: center;
          font-size: 20px; font-weight: 700; background: #111c2e; }
  .mcard, .rcell { width: 62px; height: 62px; border-radius: 3px; background: #16233a; border: 0;
                   display: grid; place-items: center; font-size: 24px; cursor: pointer; }
  .mcard { color: transparent; }
  .mcard.up { background: #24405f; color: #fff; }
  .mcard.done { background: rgba(0,255,194,.16); color: #fff; cursor: default; }
  .rcell { background: #111c2e; }
  .rcell.lit { background: #ff5d8f; }
  .over { position: absolute; inset: 0; display: grid; align-content: center; justify-items: center; gap: 4px;
          text-align: center; background: rgba(5,7,10,.9); border-radius: 2px; padding: 18px; }
  .over p { margin: 0; font-size: 10px; letter-spacing: .22em; text-transform: uppercase; color: #7c8aa0; }
  .over strong { font-size: 44px; line-height: 1; color: #00ffc2; }
  .over button { margin-top: 12px; cursor: pointer; border: 0; border-radius: 2px; background: #00ffc2; color: #05070a;
                 padding: 12px 18px; font: inherit; font-size: 11px; font-weight: 700; letter-spacing: .18em; text-transform: uppercase; }
  .note { margin-top: 16px; max-width: 460px; text-align: center; font-size: 11px; line-height: 1.7; color: #7c8aa0; }
</style>
</head>
<body>
<h1>Nebula Arcade</h1>
<p class="sub">One file &middot; No libraries</p>
<div class="tabs" id="tabs"></div>
<div id="stage"></div>
<p class="note">Arrow keys or WASD on a keyboard. Swipe or tap on a touch screen.</p>
<script>
(function () {
  'use strict';

  var stage = document.getElementById('stage');
  var tabsEl = document.getElementById('tabs');
  var activeId = null;
  var teardown = null;

  function hud(root, left, right) {
    var bar = document.createElement('div');
    bar.className = 'hud';
    var l = document.createElement('span');
    l.innerHTML = left;
    var r = document.createElement('span');
    r.textContent = right;
    bar.appendChild(l);
    bar.appendChild(r);
    root.appendChild(bar);
    return { left: l, right: r };
  }

  function endPanel(root, score, replay, note) {
    var box = document.createElement('div');
    box.className = 'over';
    box.innerHTML = '<p>Final score</p><strong>' + score + '</strong><p>' + note + '</p>';
    var again = document.createElement('button');
    again.type = 'button';
    again.textContent = 'Play again';
    again.onclick = replay;
    box.appendChild(again);
    root.appendChild(box);
  }

  function keys(handler) {
    window.addEventListener('keydown', handler);
    return function () { window.removeEventListener('keydown', handler); };
  }

  function swipes(el, handler) {
    var start = null;
    function down(e) { var t = e.touches[0]; start = { x: t.clientX, y: t.clientY }; }
    function up(e) {
      if (!start) return;
      var t = e.changedTouches[0];
      var dx = t.clientX - start.x;
      var dy = t.clientY - start.y;
      start = null;
      if (Math.abs(dx) < 24 && Math.abs(dy) < 24) return;
      handler(Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? 'right' : 'left') : (dy > 0 ? 'down' : 'up'));
    }
    el.addEventListener('touchstart', down, { passive: true });
    el.addEventListener('touchend', up, { passive: true });
    return function () {
      el.removeEventListener('touchstart', down);
      el.removeEventListener('touchend', up);
    };
  }

  // ---------- Neon Snake ----------
  function snake(root, replay) {
    var GRID = 20, SIZE = 400, cell = SIZE / GRID;
    var h = hud(root, 'Score <b>0</b>', 'Neon Snake');
    var canvas = document.createElement('canvas');
    canvas.width = SIZE;
    canvas.height = SIZE;
    root.appendChild(canvas);
    var ctx = canvas.getContext('2d');
    var body = [{ x: 10, y: 10 }, { x: 9, y: 10 }, { x: 8, y: 10 }];
    var dir = { x: 1, y: 0 }, queued = { x: 1, y: 0 };
    var food = { x: 14, y: 10 }, points = 0, over = false, timer = null;

    function place() {
      do {
        food = { x: Math.floor(Math.random() * GRID), y: Math.floor(Math.random() * GRID) };
      } while (body.some(function (s) { return s.x === food.x && s.y === food.y; }));
    }

    function draw() {
      ctx.fillStyle = '#05070a';
      ctx.fillRect(0, 0, SIZE, SIZE);
      ctx.strokeStyle = 'rgba(255,255,255,.05)';
      for (var i = 1; i < GRID; i += 1) {
        ctx.beginPath(); ctx.moveTo(i * cell, 0); ctx.lineTo(i * cell, SIZE); ctx.stroke();
        ctx.beginPath(); ctx.moveTo(0, i * cell); ctx.lineTo(SIZE, i * cell); ctx.stroke();
      }
      ctx.fillStyle = '#ff5d8f';
      ctx.beginPath();
      ctx.arc(food.x * cell + cell / 2, food.y * cell + cell / 2, cell * 0.3, 0, Math.PI * 2);
      ctx.fill();
      body.forEach(function (seg, index) {
        ctx.fillStyle = index === 0 ? '#00ffc2' : '#00a889';
        ctx.fillRect(seg.x * cell + 1.5, seg.y * cell + 1.5, cell - 3, cell - 3);
      });
    }

    function step() {
      if (over) return;
      dir = queued;
      var head = { x: body[0].x + dir.x, y: body[0].y + dir.y };
      var wall = head.x < 0 || head.y < 0 || head.x >= GRID || head.y >= GRID;
      var self = body.some(function (s) { return s.x === head.x && s.y === head.y; });
      if (wall || self) {
        over = true;
        clearInterval(timer);
        endPanel(root, points, replay, 'You hit something');
        return;
      }
      body.unshift(head);
      if (head.x === food.x && head.y === food.y) {
        points += 10;
        h.left.innerHTML = 'Score <b>' + points + '</b>';
        place();
        clearInterval(timer);
        timer = setInterval(step, Math.max(70, 145 - points * 0.6));
      } else {
        body.pop();
      }
      draw();
    }

    function turn(next) {
      if (next.x === -dir.x && next.y === -dir.y) return;
      queued = next;
    }

    var dirs = { up: { x: 0, y: -1 }, down: { x: 0, y: 1 }, left: { x: -1, y: 0 }, right: { x: 1, y: 0 } };
    var offKeys = keys(function (e) {
      var map = {
        arrowup: 'up', w: 'up', arrowdown: 'down', s: 'down',
        arrowleft: 'left', a: 'left', arrowright: 'right', d: 'right'
      };
      var name = map[e.key.toLowerCase()];
      if (!name) return;
      e.preventDefault();
      turn(dirs[name]);
    });
    var offSwipe = swipes(canvas, function (direction) { turn(dirs[direction]); });

    draw();
    timer = setInterval(step, 145);

    return function () {
      over = true;
      clearInterval(timer);
      offKeys();
      offSwipe();
    };
  }

  // ---------- Block 2048 ----------
  function block2048(root, replay) {
    var h = hud(root, 'Score <b>0</b>', 'Merge to 2048');
    var board = document.createElement('div');
    board.className = 'board';
    board.style.gridTemplateColumns = 'repeat(4, 62px)';
    root.appendChild(board);

    var grid = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
    var score = 0;
    var over = false;
    var colors = {
      2: '#2b3d55', 4: '#3f5a7a', 8: '#ffd166', 16: '#ffb347', 32: '#ff8f3c',
      64: '#ff6b35', 128: '#00ffc2', 256: '#00e0ac', 512: '#00c299', 1024: '#00a889', 2048: '#00ffc2'
    };

    function add() {
      var empty = [];
      for (var r = 0; r < 4; r += 1) {
        for (var c = 0; c < 4; c += 1) if (!grid[r][c]) empty.push([r, c]);
      }
      if (!empty.length) return;
      var spot = empty[Math.floor(Math.random() * empty.length)];
      grid[spot[0]][spot[1]] = Math.random() < 0.9 ? 2 : 4;
    }

    function paint() {
      board.innerHTML = '';
      for (var r = 0; r < 4; r += 1) {
        for (var c = 0; c < 4; c += 1) {
          var value = grid[r][c];
          var d = document.createElement('div');
          d.className = 'cell';
          d.textContent = value || '';
          if (value) {
            d.style.background = colors[value] || '#00ffc2';
            d.style.color = value > 4 ? '#05070a' : '#dbe6f5';
          }
          board.appendChild(d);
        }
      }
      h.left.innerHTML = 'Score <b>' + score + '</b>';
    }

    function canMove() {
      for (var r = 0; r < 4; r += 1) {
        for (var c = 0; c < 4; c += 1) {
          if (!grid[r][c]) return true;
          if (c < 3 && grid[r][c] === grid[r][c + 1]) return true;
          if (r < 3 && grid[r][c] === grid[r + 1][c]) return true;
        }
      }
      return false;
    }

    function line(dir, i) {
      var out = [];
      for (var j = 0; j < 4; j += 1) {
        if (dir === 'left') out.push([i, j]);
        else if (dir === 'right') out.push([i, 3 - j]);
        else if (dir === 'up') out.push([j, i]);
        else out.push([3 - j, i]);
      }
      return out;
    }

    function move(dir) {
      if (over) return;
      var moved = false;
      var gained = 0;
      for (var i = 0; i < 4; i += 1) {
        var cells = line(dir, i);
        var values = [];
        cells.forEach(function (p) { if (grid[p[0]][p[1]]) values.push(grid[p[0]][p[1]]); });
        var out = [];
        for (var k = 0; k < values.length; k += 1) {
          if (k + 1 < values.length && values[k] === values[k + 1]) {
            out.push(values[k] * 2);
            gained += values[k] * 2;
            k += 1;
          } else {
            out.push(values[k]);
          }
        }
        while (out.length < 4) out.push(0);
        cells.forEach(function (p, index) {
          if (grid[p[0]][p[1]] !== out[index]) moved = true;
          grid[p[0]][p[1]] = out[index];
        });
      }
      if (!moved) return;
      score += gained;
      add();
      paint();
      if (!canMove()) {
        over = true;
        endPanel(root, score, replay, 'No moves left');
      }
    }

    var map = {
      arrowleft: 'left', a: 'left', arrowright: 'right', d: 'right',
      arrowup: 'up', w: 'up', arrowdown: 'down', s: 'down'
    };
    var offKeys = keys(function (e) {
      var dir = map[e.key.toLowerCase()];
      if (!dir) return;
      e.preventDefault();
      move(dir);
    });
    var offSwipe = swipes(board, move);

    add();
    add();
    paint();

    return function () {
      over = true;
      offKeys();
      offSwipe();
    };
  }

  // ---------- Memory Drift ----------
  function memory(root, replay) {
    var icons = ['🍒', '🔷', '🎯', '🌙', '🔥', '💠', '🧿', '🎲'];
    var deck = icons.concat(icons).sort(function () { return Math.random() - 0.5; });
    var h = hud(root, 'Moves <b>0</b>', 'Find the pairs');
    var wrap = document.createElement('div');
    wrap.className = 'grid';
    wrap.style.gridTemplateColumns = 'repeat(4, 62px)';
    root.appendChild(wrap);

    var cards = [];
    var first = null;
    var busy = false;
    var moves = 0;
    var matched = 0;
    var done = false;
    var pending = null;

    deck.forEach(function (icon, index) {
      var card = document.createElement('button');
      card.type = 'button';
      card.className = 'mcard';
      card.textContent = icon;
      card.onclick = function () { flip(index); };
      wrap.appendChild(card);
      cards.push({ el: card, icon: icon, up: false, done: false });
    });

    function flip(index) {
      var card = cards[index];
      if (done || busy || card.up || card.done) return;
      card.up = true;
      card.el.className = 'mcard up';
      if (first === null) {
        first = index;
        return;
      }
      moves += 1;
      h.left.innerHTML = 'Moves <b>' + moves + '</b>';
      var other = cards[first];
      if (other.icon === card.icon) {
        other.done = true;
        card.done = true;
        other.el.className = 'mcard done';
        card.el.className = 'mcard done';
        first = null;
        matched += 1;
        if (matched === icons.length) {
          done = true;
          endPanel(root, Math.max(0, 900 - moves * 40), replay, 'Board cleared');
        }
        return;
      }
      busy = true;
      var previous = first;
      first = null;
      pending = setTimeout(function () {
        cards[previous].up = false;
        cards[previous].el.className = 'mcard';
        card.up = false;
        card.el.className = 'mcard';
        busy = false;
      }, 620);
    }

    return function () {
      done = true;
      clearTimeout(pending);
    };
  }

  // ---------- Brick Storm ----------
  function brick(root, replay) {
    var W = 420, H = 330;
    var h = hud(root, 'Score <b>0</b>', 'Lives 3');
    var canvas = document.createElement('canvas');
    canvas.width = W;
    canvas.height = H;
    root.appendChild(canvas);
    var ctx = canvas.getContext('2d');

    var cols = 8, rows = 5, bw = W / cols, bh = 18, top = 34;
    var bricks = [];
    var live = 0;
    for (var r = 0; r < rows; r += 1) {
      for (var c = 0; c < cols; c += 1) {
        bricks.push({ x: c * bw, y: top + r * bh, alive: true, row: r });
        live += 1;
      }
    }
    var palette = ['#57d9ff', '#00ffc2', '#ffd166', '#ff8f3c', '#ff5d8f'];
    var paddle = { x: W / 2 - 40, w: 80, y: H - 22, h: 10 };
    var ball = { x: W / 2, y: H - 60, vx: 2.6, vy: -3.1, r: 6 };
    var score = 0, lives = 3, ended = false, frame = 0;

    function resetBall() {
      ball.x = W / 2;
      ball.y = H - 60;
      ball.vx = Math.random() < 0.5 ? -2.6 : 2.6;
      ball.vy = -3.1;
    }

    function draw() {
      ctx.fillStyle = '#05070a';
      ctx.fillRect(0, 0, W, H);
      bricks.forEach(function (b) {
        if (!b.alive) return;
        ctx.fillStyle = palette[b.row % palette.length];
        ctx.fillRect(b.x + 2, b.y + 2, bw - 4, bh - 4);
      });
      ctx.fillStyle = '#00ffc2';
      ctx.fillRect(paddle.x, paddle.y, paddle.w, paddle.h);
      ctx.beginPath();
      ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2);
      ctx.fillStyle = '#ffffff';
      ctx.fill();
    }

    function tick() {
      if (ended) return;
      ball.x += ball.vx;
      ball.y += ball.vy;

      if (ball.x < ball.r) { ball.x = ball.r; ball.vx = Math.abs(ball.vx); }
      if (ball.x > W - ball.r) { ball.x = W - ball.r; ball.vx = -Math.abs(ball.vx); }
      if (ball.y < ball.r) { ball.y = ball.r; ball.vy = Math.abs(ball.vy); }

      if (ball.vy > 0 && ball.y + ball.r >= paddle.y && ball.y - ball.r <= paddle.y + paddle.h &&
          ball.x >= paddle.x && ball.x <= paddle.x + paddle.w) {
        ball.y = paddle.y - ball.r;
        ball.vy = -Math.abs(ball.vy);
        var offset = (ball.x - (paddle.x + paddle.w / 2)) / (paddle.w / 2);
        ball.vx = offset * 3.4;
      }

      for (var i = 0; i < bricks.length; i += 1) {
        var b = bricks[i];
        if (!b.alive) continue;
        if (ball.x + ball.r > b.x && ball.x - ball.r < b.x + bw &&
            ball.y + ball.r > b.y && ball.y - ball.r < b.y + bh) {
          b.alive = false;
          live -= 1;
          score += 10;
          h.left.innerHTML = 'Score <b>' + score + '</b>';
          ball.vy = -ball.vy;
          break;
        }
      }

      if (live === 0) {
        ended = true;
        endPanel(root, score, replay, 'Wall cleared');
        return;
      }

      if (ball.y - ball.r > H) {
        lives -= 1;
        h.right.textContent = 'Lives ' + lives;
        if (lives <= 0) {
          ended = true;
          endPanel(root, score, replay, 'Out of lives');
          return;
        }
        resetBall();
      }

      draw();
      frame = requestAnimationFrame(tick);
    }

    function pointAt(clientX) {
      var rect = canvas.getBoundingClientRect();
      var x = (clientX - rect.left) * (W / rect.width);
      paddle.x = Math.max(0, Math.min(W - paddle.w, x - paddle.w / 2));
    }
    var onMove = function (e) { pointAt(e.clientX); };
    var onTouch = function (e) {
      if (e.touches[0]) {
        e.preventDefault();
        pointAt(e.touches[0].clientX);
      }
    };
    window.addEventListener('mousemove', onMove);
    canvas.addEventListener('touchmove', onTouch, { passive: false });
    var offKeys = keys(function (e) {
      var k = e.key.toLowerCase();
      if (k === 'arrowleft' || k === 'a') {
        e.preventDefault();
        paddle.x = Math.max(0, paddle.x - 26);
      }
      if (k === 'arrowright' || k === 'd') {
        e.preventDefault();
        paddle.x = Math.min(W - paddle.w, paddle.x + 26);
      }
    });

    draw();
    frame = requestAnimationFrame(tick);

    return function () {
      ended = true;
      cancelAnimationFrame(frame);
      window.removeEventListener('mousemove', onMove);
      canvas.removeEventListener('touchmove', onTouch);
      offKeys();
    };
  }

  // ---------- Reflex Grid ----------
  function reflex(root, replay) {
    var h = hud(root, 'Score <b>0</b>', '30s');
    var wrap = document.createElement('div');
    wrap.className = 'grid';
    wrap.style.gridTemplateColumns = 'repeat(4, 62px)';
    root.appendChild(wrap);

    var cells = [];
    var score = 0;
    var left = 30;
    var lit = -1;
    var ended = false;

    for (var i = 0; i < 16; i += 1) {
      (function (index) {
        var cell = document.createElement('button');
        cell.type = 'button';
        cell.className = 'rcell';
        cell.onclick = function () {
          if (ended || lit !== index) return;
          score += 10;
          h.left.innerHTML = 'Score <b>' + score + '</b>';
          cells[index].className = 'rcell';
          lit = -1;
          spawn();
        };
        wrap.appendChild(cell);
        cells.push(cell);
      })(i);
    }

    function spawn() {
      if (ended) return;
      if (lit >= 0) cells[lit].className = 'rcell';
      lit = Math.floor(Math.random() * 16);
      cells[lit].className = 'rcell lit';
    }

    function stop() {
      if (ended) return;
      ended = true;
      clearInterval(spawnTimer);
      clearInterval(clock);
      endPanel(root, score, replay, 'Time up');
    }

    var spawnTimer = setInterval(spawn, 620);
    var clock = setInterval(function () {
      left -= 1;
      h.right.textContent = left + 's';
      if (left <= 0) stop();
    }, 1000);

    spawn();

    return function () {
      ended = true;
      clearInterval(spawnTimer);
      clearInterval(clock);
    };
  }

  var games = [
    { id: 'snake', name: 'Neon Snake', start: snake },
    { id: '2048', name: 'Block 2048', start: block2048 },
    { id: 'memory', name: 'Memory Drift', start: memory },
    { id: 'brick', name: 'Brick Storm', start: brick },
    { id: 'reflex', name: 'Reflex Grid', start: reflex }
  ];

  function open(id) {
    if (typeof teardown === 'function') teardown();
    teardown = null;
    activeId = id;
    stage.innerHTML = '';
    tabsEl.innerHTML = '';
    games.forEach(function (g) {
      var b = document.createElement('button');
      b.type = 'button';
      b.className = 'tab' + (g.id === activeId ? ' on' : '');
      b.textContent = g.name;
      b.onclick = function () { open(g.id); };
      tabsEl.appendChild(b);
    });
    var game = games.filter(function (g) { return g.id === id; })[0];
    teardown = game.start(stage, function () { open(id); }) || null;
  }

  open('snake');
})();
</script>
</body>
</html>