🎅 Chimney Drop - Santa Christmas Game 🎁

Santa is flying over the rooftops! Tap the screen, click or press the space bar to drop a present, and time it so it falls straight into the chimney. Three missed presents and Christmas Eve is over!
Presents: 0  |  Lives: ❤️❤️❤️  |  Best: 0
Press "Start", then tap to drop your first present!

It is Christmas Eve 🎄 and Santa is flying over the rooftops with a sleigh full of presents. Tap the screen, click or press the space bar to drop a present, and time it so it falls straight down a chimney. Presents keep a little of the sleigh's speed as they fall, and the houses keep moving, so you have to aim ahead. Each present that lands in a chimney scores a point; the rooftops speed up and the chimneys get narrower as you go, and three presents lost in the snow end the night.

How many presents can you deliver before Christmas morning? 🎁

Here is the code
CSS
.chim-game {
    text-align: center;
}

.chim-game .instructions {
    max-width: 500px;
    margin: 0 auto 15px;
    font-size: 1em;
    color: #555;
}

.chim-info {
    margin-bottom: 10px;
    font-size: 1.2em;
}

.chim-canvas {
    display: block;
    width: 100%;
    max-width: 360px;
    height: auto;
    margin: 0 auto;
    border: 3px solid #8b1e2d;
    border-radius: 12px;
    background: #14204a;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    cursor: pointer;
}

.chim-message {
    min-height: 1.5em;
    margin: 12px 0;
    font-size: 1.1em;
    font-weight: bold;
    color: #8b1e2d;
}
HTML
<div class="chim-game">
    <h1>🎅 Chimney Drop - Santa Christmas Game 🎁</h1>
    <div class="instructions">
        Santa is flying over the rooftops! Tap the screen, click or press the space bar to drop a present, and time it so it falls straight into the chimney. Three missed presents and Christmas Eve is over!
    </div>
    <div class="chim-info">Presents: <span id="chimScore">0</span> &nbsp;|&nbsp; Lives: <span id="chimLives">❤️❤️❤️</span> &nbsp;|&nbsp; Best: <span id="chimBest">0</span></div>
    <canvas id="chimCanvas" class="chim-canvas" width="360" height="540" aria-label="Santa's sleigh over the rooftops"></canvas>
    <div class="chim-message" id="chimMessage">Press "Start", then tap to drop your first present!</div>
    <button class="start-btn" id="chimStart">Start</button>
</div>
JS
const canvas = document.getElementById('chimCanvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('chimScore');
const livesDisplay = document.getElementById('chimLives');
const bestDisplay = document.getElementById('chimBest');
const message = document.getElementById('chimMessage');
const startButton = document.getElementById('chimStart');
const hiddenScore = document.getElementById('hiddenScore');

const W = canvas.width;
const H = canvas.height;
const SLEIGH_Y = 90;
const GRAVITY = 520; // px per second squared
const ROOF_Y = 400; // top of the chimneys
const MAX_LIVES = 3;
const GIFT_COLORS = ['#e63946', '#2a9d8f', '#f4a261', '#7b5ea7'];

let sleighX, sleighDir, sleighSpeed, houses, gifts, stars, snow, score, lives, running, waiting, lastTime, animationId, flash, sparkles, cooldown;
let best = 0;

try {
    best = parseInt(localStorage.getItem('chimBest'), 10) || 0;
} catch (e) {
    best = 0;
}
bestDisplay.textContent = best;

// The share buttons are rendered after this script, so look them up when needed.
function setShareButtons(display) {
    const shareButtons = document.querySelector('.share-buttons');
    if (shareButtons) {
        shareButtons.style.display = display;
    }
}

function hearts(n) {
    let s = '';
    for (let i = 0; i < MAX_LIVES; i++) {
        s += i < n ? '❤️' : '🤍';
    }
    return s;
}

function chimneyWidth() {
    // Chimneys get narrower as the score climbs, down to a minimum
    return Math.max(26, 54 - score * 1.2);
}

function roofSpeed() {
    // The rooftops scroll faster as the night goes on
    return Math.min(170, 55 + score * 4);
}

function addHouse(x) {
    const w = 110 + Math.random() * 40;
    houses.push({
        x: x,
        w: w,
        h: 70 + Math.random() * 40,
        chimX: x + 20 + Math.random() * (w - 40 - chimneyWidth()),
        chimW: chimneyWidth(),
        color: ['#6b2737', '#3d5a80', '#5e4b8b', '#2f6b4f'][Math.floor(Math.random() * 4)],
        filled: false
    });
}

function reset() {
    sleighX = W / 2;
    sleighDir = 1;
    sleighSpeed = 120;
    houses = [];
    gifts = [];
    sparkles = [];
    snow = [];
    stars = [];
    score = 0;
    lives = MAX_LIVES;
    flash = 0;
    cooldown = 0;
    let x = -40;
    while (x < W + 200) {
        addHouse(x);
        x = houses[houses.length - 1].x + houses[houses.length - 1].w + 20 + Math.random() * 30;
    }
    for (let i = 0; i < 40; i++) {
        snow.push({ x: Math.random() * W, y: Math.random() * H, r: 1 + Math.random() * 2, s: 20 + Math.random() * 30 });
    }
    for (let i = 0; i < 30; i++) {
        stars.push({ x: Math.random() * W, y: Math.random() * 260, r: Math.random() * 1.5 + 0.5 });
    }
    scoreDisplay.textContent = 0;
    livesDisplay.textContent = hearts(lives);
    hiddenScore.value = 0;
}

function startGame() {
    cancelAnimationFrame(animationId);
    reset();
    running = true;
    waiting = true;
    setShareButtons('none');
    startButton.textContent = 'Restart';
    startButton.blur();
    message.textContent = 'Tap, click or press space to drop a present!';
    lastTime = performance.now();
    animationId = requestAnimationFrame(loop);
}

function dropGift() {
    if (!running) {
        return;
    }
    if (waiting) {
        waiting = false;
        message.textContent = 'Aim for the chimneys! 🎁';
    }
    if (cooldown > 0) {
        return;
    }
    cooldown = 0.35;
    gifts.push({
        x: sleighX,
        y: SLEIGH_Y + 18,
        vx: sleighDir * sleighSpeed * 0.35,
        vy: 0,
        color: GIFT_COLORS[Math.floor(Math.random() * GIFT_COLORS.length)]
    });
}

function loseLife(text) {
    lives -= 1;
    livesDisplay.textContent = hearts(lives);
    flash = 0.35;
    message.textContent = text;
    if (lives <= 0) {
        endGame();
    }
}

function endGame() {
    running = false;
    cancelAnimationFrame(animationId);
    if (score > best) {
        best = score;
        bestDisplay.textContent = best;
        try {
            localStorage.setItem('chimBest', String(best));
        } catch (e) {
            // ignore storage errors
        }
        message.textContent = 'Christmas Eve is over! New best: ' + score + ' presents 🏆';
    } else {
        message.textContent = 'Christmas Eve is over! You delivered ' + score + ' presents. Best: ' + best;
    }
    setShareButtons('flex');
    draw();
}

function update(dt) {
    cooldown = Math.max(0, cooldown - dt);
    flash = Math.max(0, flash - dt);

    // Santa flies back and forth across the sky
    sleighSpeed = Math.min(230, 120 + score * 3);
    sleighX += sleighDir * sleighSpeed * dt;
    if (sleighX > W - 40) {
        sleighX = W - 40;
        sleighDir = -1;
    } else if (sleighX < 40) {
        sleighX = 40;
        sleighDir = 1;
    }

    // Snow keeps falling, even while waiting
    for (const f of snow) {
        f.y += f.s * dt;
        if (f.y > H) {
            f.y = -5;
            f.x = Math.random() * W;
        }
    }

    for (const p of sparkles) {
        p.x += p.vx * dt;
        p.y += p.vy * dt;
        p.life -= dt;
    }
    sparkles = sparkles.filter(function (p) { return p.life > 0; });

    if (waiting) {
        return;
    }

    // Rooftops scroll from right to left
    const speed = roofSpeed();
    for (const h of houses) {
        h.x -= speed * dt;
        h.chimX -= speed * dt;
    }
    houses = houses.filter(function (h) { return h.x + h.w > -20; });
    const last = houses[houses.length - 1];
    if (last.x + last.w < W + 160) {
        addHouse(last.x + last.w + 20 + Math.random() * 40);
    }

    // Presents fall with gravity
    for (const g of gifts) {
        g.vy += GRAVITY * dt;
        g.x += g.vx * dt;
        g.y += g.vy * dt;
        if (!g.done && g.y >= ROOF_Y) {
            g.done = true;
            const house = houses.find(function (h) {
                return g.x >= h.chimX && g.x <= h.chimX + h.chimW;
            });
            if (house) {
                score += 1;
                house.filled = true;
                scoreDisplay.textContent = score;
                hiddenScore.value = score;
                message.textContent = score % 5 === 0 ? 'Ho ho ho! ' + score + ' presents delivered! 🎅' : 'Down the chimney! 🎄';
                for (let i = 0; i < 12; i++) {
                    sparkles.push({ x: g.x, y: ROOF_Y, vx: (Math.random() - 0.5) * 160, vy: -Math.random() * 160, life: 0.6 });
                }
            } else {
                loseLife('Oops, the present landed in the snow! ❄️');
            }
        }
    }
    gifts = gifts.filter(function (g) { return !g.done; });
}

function drawHouse(h) {
    const top = H - h.h;
    // chimney
    ctx.fillStyle = '#7a3b2e';
    ctx.fillRect(h.chimX, ROOF_Y, h.chimW, top - ROOF_Y + 10);
    ctx.fillStyle = '#f1f1f1';
    ctx.fillRect(h.chimX - 4, ROOF_Y - 6, h.chimW + 8, 8);
    if (h.filled) {
        ctx.fillStyle = 'rgba(255, 210, 90, 0.6)';
        ctx.fillRect(h.chimX + 3, ROOF_Y + 2, h.chimW - 6, 8);
    }
    // roof
    ctx.fillStyle = '#e9eef7';
    ctx.beginPath();
    ctx.moveTo(h.x - 8, top);
    ctx.lineTo(h.x + h.w / 2, top - 40);
    ctx.lineTo(h.x + h.w + 8, top);
    ctx.closePath();
    ctx.fill();
    // walls
    ctx.fillStyle = h.color;
    ctx.fillRect(h.x, top, h.w, h.h);
    // windows
    ctx.fillStyle = '#ffd36b';
    ctx.fillRect(h.x + 16, top + 18, 18, 18);
    ctx.fillRect(h.x + h.w - 34, top + 18, 18, 18);
}

function drawSleigh() {
    const x = sleighX;
    const y = SLEIGH_Y;
    ctx.save();
    ctx.translate(x, y);
    ctx.scale(sleighDir, 1);
    // reindeer
    ctx.font = '26px serif';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.save();
    ctx.scale(-1, 1);
    ctx.fillText('🦌', -48, 0);
    ctx.restore();
    // reins
    ctx.strokeStyle = '#c9a227';
    ctx.lineWidth = 2;
    ctx.beginPath();
    ctx.moveTo(12, -2);
    ctx.lineTo(36, 0);
    ctx.stroke();
    // sleigh body
    ctx.fillStyle = '#c62828';
    ctx.beginPath();
    ctx.moveTo(-26, -6);
    ctx.lineTo(14, -6);
    ctx.lineTo(18, 10);
    ctx.lineTo(-22, 10);
    ctx.closePath();
    ctx.fill();
    ctx.strokeStyle = '#ffd36b';
    ctx.lineWidth = 3;
    ctx.beginPath();
    ctx.moveTo(-28, 15);
    ctx.lineTo(20, 15);
    ctx.quadraticCurveTo(28, 15, 26, 6);
    ctx.stroke();
    ctx.restore();
    ctx.font = '24px serif';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText('🎅', x - sleighDir * 6, y - 14);
}

function drawGift(g) {
    ctx.fillStyle = g.color;
    ctx.fillRect(g.x - 8, g.y - 8, 16, 16);
    ctx.fillStyle = '#ffd36b';
    ctx.fillRect(g.x - 2, g.y - 8, 4, 16);
    ctx.fillRect(g.x - 8, g.y - 2, 16, 4);
}

function draw() {
    const sky = ctx.createLinearGradient(0, 0, 0, H);
    sky.addColorStop(0, '#0b1433');
    sky.addColorStop(1, '#27407a');
    ctx.fillStyle = sky;
    ctx.fillRect(0, 0, W, H);

    ctx.fillStyle = '#fff';
    for (const s of stars) {
        ctx.beginPath();
        ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
        ctx.fill();
    }
    ctx.fillStyle = '#ffe9a8';
    ctx.beginPath();
    ctx.arc(W - 60, 50, 22, 0, Math.PI * 2);
    ctx.fill();

    for (const h of houses) {
        drawHouse(h);
    }
    ctx.fillStyle = '#f4f8ff';
    ctx.fillRect(0, H - 14, W, 14);

    // where the present would fall right now
    if (running) {
        ctx.strokeStyle = 'rgba(255, 255, 255, 0.15)';
        ctx.setLineDash([4, 6]);
        ctx.beginPath();
        ctx.moveTo(sleighX, SLEIGH_Y + 20);
        ctx.lineTo(sleighX, ROOF_Y);
        ctx.stroke();
        ctx.setLineDash([]);
    }

    for (const g of gifts) {
        drawGift(g);
    }
    drawSleigh();

    ctx.fillStyle = '#ffd36b';
    for (const p of sparkles) {
        ctx.globalAlpha = Math.max(0, p.life / 0.6);
        ctx.fillRect(p.x - 2, p.y - 2, 4, 4);
    }
    ctx.globalAlpha = 1;

    ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
    for (const f of snow) {
        ctx.beginPath();
        ctx.arc(f.x, f.y, f.r, 0, Math.PI * 2);
        ctx.fill();
    }

    if (flash > 0) {
        ctx.fillStyle = 'rgba(230, 57, 70, ' + (flash * 0.8) + ')';
        ctx.fillRect(0, 0, W, H);
    }

    if (running && waiting) {
        ctx.fillStyle = 'rgba(0, 0, 0, 0.35)';
        ctx.fillRect(0, H / 2 - 30, W, 60);
        ctx.fillStyle = '#fff';
        ctx.font = 'bold 20px sans-serif';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText('Tap to drop a present 🎁', W / 2, H / 2);
    }
}

function loop(now) {
    const dt = Math.min(0.05, (now - lastTime) / 1000);
    lastTime = now;
    update(dt);
    if (!running) {
        return;
    }
    draw();
    animationId = requestAnimationFrame(loop);
}

canvas.addEventListener('touchstart', function (event) {
    event.preventDefault();
    dropGift();
}, { passive: false });

canvas.addEventListener('mousedown', function () {
    dropGift();
});

document.addEventListener('keydown', function (event) {
    if (!running) {
        return;
    }
    if (event.key === ' ' || event.key === 'ArrowDown' || event.key === 'Enter') {
        event.preventDefault();
        dropGift();
    }
});

startButton.addEventListener('click', startGame);

reset();
running = false;
draw();