<div id="wrap">
<canvas id="game" width="360" height="640"></canvas>
<div id="ui">
<div id="msg" class="small">Click the tile in the hit zone. Miss and it’s over.</div>
<button id="play">Play</button>
</div>
</div>
/* Minimal Magic Tiles MVP
- 4 columns
- Tap black tiles in the bottom hit zone
- Miss or tap empty = game over
*/
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const playBtn = document.getElementById('play');
const msgEl = document.getElementById('msg');
const W = canvas.width;
const H = canvas.height;
// Grid
const COLS = 4;
const colW = W / COLS;
// Hit zone
const HIT_H = 80; // height of the hit zone
const HIT_Y = H - HIT_H; // y position
const HIT_SLACK = 12; // leniency in pixels
// Game state
let tiles = []; // {col, y, h}
let running = false;
let score = 0;
let best = 0;
let speed = 20; // px/s
let spawnEvery = 2000; // ms
let lastTime = 0;
let spawnTimer = 0;
function reset() {
tiles = [];
score = 0;
speed = 20;
spawnEvery = 2000;
spawnTimer = 0;
lastTime = performance.now();
}
function start() {
reset();
running = true;
msgEl.textContent = 'Good luck.';
requestAnimationFrame(loop);
}
function gameOver(text = 'Game over') {
running = false;
best = Math.max(best, score);
msgEl.textContent = `${text} | Score ${score} | Best ${best}`;
playBtn.textContent = 'Play again';
}
function spawnTile() {
const col = Math.floor(Math.random() * COLS);
const h = 110; // tile height
tiles.push({ col, y: -h, h });
}
function update(dt) {
// Increase difficulty over time
speed += dt * 4; // very light ramp
spawnEvery = Math.max(180, spawnEvery - dt * 8);
// Move tiles
for (const t of tiles) t.y += speed * dt / 1000;
// Check miss: tile passed bottom of hit zone without a tap
for (const t of tiles) {
if (t.y + t.h >= H && !t.passed) {
// If it reaches the bottom, that means you missed it in the hit...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.