I've created a complete HTML document for a charming casual game where you control a little girl who must catch falling strawberries to earn points while avoiding homework that costs lives.
html Copy code
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>🍓 Attrape-Fraises !</title>
<style>
:root {
--bg: #1a0a2e;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: var(--bg);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
overflow: hidden;
font-family: 'Segoe UI', 'Trebuchet MS', 'Helvetica Neue', sans-serif;
touch-action: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
cursor: none;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#ui-hint {
position: fixed;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
color: rgba(255, 255, 255, 0.7);
font-size: 14px;
letter-spacing: 0.5px;
pointer-events: none;
z-index: 10;
transition: opacity 0.5s;
text-align: center;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="ui-hint">Déplace ta souris ou ton doigt pour jouer ✨</div>
<script>
(function() {
// ─────────────────────────────────────
// CANVAS SETUP
// ─────────────────────────────────────
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const uiHint = document.getElementById('ui-hint');
let W, H, dpr;
let groundY; // Y position of the grass line
function resizeCanvas() {
dpr = Math.min(window.devicePixelRatio || 1, 2);
W = window.innerWidth;
H = window.innerHeight;
canvas.width = W * dpr;
canvas.height = H * dpr;
canvas.style.width = W + 'px';
canvas.style.height = H + 'px';
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(dpr, dpr);
groundY = H * 0.82;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// ─────────────────────────────────────
// AUDIO ENGINE
// ─────────────────────────────────────
let audioCtx = null;
function getAudio() {
if (!audioCtx) {
audioCtx = new(window.AudioContext || window.webkitAudioContext)();
}
if (audioCtx.state === 'suspended') audioCtx.resume();
return audioCtx;
}
function playSound(type) {
try {
const ac = getAudio();
const now = ac.currentTime;
const gain = ac.createGain();
gain.connect(ac.destination);
switch (type) {
case 'catch':
// Joyful ascending chime
{
const osc1 = ac.createOscillator();
const osc2 = ac.createOscillator();
osc1.type = 'sine';
osc2.type = 'sine';
osc1.frequency.setValueAtTime(523, now);
osc2.frequency.setValueAtTime(784, now + 0.06);
gain.gain.setValueAtTime(0.25, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.3);
osc1.connect(gain);
osc2.connect(gain);
osc1.start(now);
osc2.start(now + 0.06);
osc1.stop(now + 0.15);
osc2.stop(now + 0.3);
}
break;
case 'combo':
// Sparkly double chime
{
const osc = ac.createOscillator();
osc.type = 'triangle';
osc.frequency.setValueAtTime(880, now);
osc.frequency.setValueAtTime(1100, now + 0.05);
osc.frequency.setValueAtTime(1320, now + 0.1);
gain.gain.setValueAtTime(0.2, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.4);
osc.connect(gain);
osc.start(now);
osc.stop(now + 0.4);
}
break;
case 'hit':
// Sad descending buzz
{
const osc = ac.createOscillator();
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(300, now);
osc.frequency.exponentialRampToValueAtTime(80, now + 0.35);
gain.gain.setValueAtTime(0.18, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.4);
osc.connect(gain);
osc.start(now);
osc.stop(now + 0.4);
}
break;
case 'gameover':
// Sad melody
{
const notes = [440, 392, 349, 330];
notes.forEach((freq, i) => {
const osc = ac.createOscillator();
const g = ac.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, now + i * 0.2);
g.gain.setValueAtTime(0.2, now + i * 0.2);
g.gain.exponentialRampToValueAtTime(0.001, now + i * 0.2 + 0.35);
osc.connect(g);
g.connect(ac.destination);
osc.start(now + i * 0.2);
osc.stop(now + i * 0.2 + 0.35);
});
}
break;
case 'start':
// Happy start jingle
{
const notes = [523, 659, 784, 1047];
notes.forEach((freq, i) => {
const osc = ac.createOscillator();
const g = ac.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, now + i * 0.08);
g.gain.setValueAtTime(0.22, now + i * 0.08);
g.gain.exponentialRampToValueAtTime(0.001, now + i * 0.08 + 0.25);
osc.connect(g);
g.connect(ac.destination);
osc.start(now + i * 0.08);
osc.stop(now + i * 0.08 + 0.25);
});
}
break;
default:
break;
}
} catch (e) {
// Audio not available
}
}
// ─────────────────────────────────────
// GAME STATE
// ─────────────────────────────────────
const STATE = { MENU: 'menu', PLAYING: 'playing', GAMEOVER: 'gameover' };
let gameState = STATE.MENU;
let score = 0;
let bestScore = parseInt(localStorage.getItem('strawberryGameBest') || '0');
let lives = 3;
let combo = 0;
let maxCombo = 0;
let level = 1;
let shakeAmount = 0;
let shakeDuration = 0;
let flashAlpha = 0;
// Player
let playerX = W / 2;
let targetX = W / 2;
let playerY;
let playerHappy = 0;
let playerSad = 0;
let playerScale = 1;
let playerBob = 0;
// Items, particles, floating texts
let items = [];
let particles = [];
let floatingTexts = [];
let clouds = [];
let bgButterflies = [];
let grassBlades = [];
// Spawn timer
let spawnTimer = 0;
let spawnInterval = 1.4;
let gameTime = 0;
// ─────────────────────────────────────
// INITIALIZE / RESET
// ─────────────────────────────────────
function initGame() {
score = 0;
lives = 3;
combo = 0;
maxCombo = 0;
level = 1;
shakeAmount = 0;
shakeDuration = 0;
flashAlpha = 0;
playerHappy = 0;
playerSad = 0;
playerScale = 1;
playerBob = 0;
items = [];
particles = [];
floatingTexts = [];
spawnTimer = 0;
spawnInterval = 1.4;
gameTime = 0;
playerX = W / 2;
targetX = W / 2;
playerY = groundY - 10;
initClouds();
initGrassBlades();
initButterflies();
}
function initClouds() {
clouds = [];
for (let i = 0; i < 6; i++) {
clouds.push({
x: Math.random() * W,
y: Math.random() * H * 0.45,
w: 60 + Math.random() * 120,
h: 25 + Math.random() * 40,
speed: 0.15 + Math.random() * 0.5,
opacity: 0.35 + Math.random() * 0.45,
});
}
}
function initGrassBlades() {
grassBlades = [];
for (let i = 0; i < 80; i++) {
grassBlades.push({
x: Math.random() * W,
h: 8 + Math.random() * 22,
sway: Math.random() * Math.PI * 2,
swaySpeed: 0.01 + Math.random() * 0.03,
swayAmp: 2 + Math.random() * 5,
});
}
}
function initButterflies() {
bgButterflies = [];
for (let i = 0; i < 4; i++) {
bgButterflies.push({
x: Math.random() * W,
y: H * 0.2 + Math.random() * H * 0.5,
vx: -0.3 + Math.random() * 0.6,
vy: -0.2 + Math.random() * 0.4,
size: 4 + Math.random() * 8,
wingPhase: Math.random() * Math.PI * 2,
color: ['#FFB3BA', '#FFDFBA', '#FFFFBA', '#BAFFC9', '#BAE1FF'][Math.floor(Math.random() *
5)],
});
}
}
initClouds();
initGrassBlades();
initButterflies();
playerY = groundY - 10;
playerX = W / 2;
targetX = W / 2;
// ─────────────────────────────────────
// DRAWING FUNCTIONS
// ─────────────────────────────────────
function drawSky() {
const grad = ctx.createLinearGradient(0, 0, 0, groundY);
grad.addColorStop(0, '#87CEEB');
grad.addColorStop(0.55, '#B8E4F0');
grad.addColorStop(0.9, '#D4F1F9');
grad.addColorStop(1, '#E8F8E0');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, W, groundY);
// Subtle sun glow
const sunX = W * 0.78;
const sunY = H * 0.12;
const sunGrad = ctx.createRadialGradient(sunX, sunY, 20, sunX, sunY, 180);
sunGrad.addColorStop(0, 'rgba(255,255,220,0.9)');
sunGrad.addColorStop(0.3, 'rgba(255,250,200,0.4)');
sunGrad.addColorStop(0.7, 'rgba(255,240,180,0.05)');
sunGrad.addColorStop(1, 'rgba(255,200,150,0)');
ctx.fillStyle = sunGrad;
ctx.fillRect(sunX - 180, sunY - 180, 360, 360);
}
function drawClouds() {
clouds.forEach(cloud => {
ctx.fillStyle = `rgba(255,255,255,${cloud.opacity})`;
ctx.beginPath();
const cx = cloud.x;
const cy = cloud.y;
const rw = cloud.w / 2;
const rh = cloud.h / 2;
ctx.arc(cx - rw * 0.5, cy, rh * 0.9, 0, Math.PI * 2);
ctx.arc(cx + rw * 0.3, cy - rh * 0.3, rh * 0.7, 0, Math.PI * 2);
ctx.arc(cx + rw * 0.6, cy, rh * 0.85, 0, Math.PI * 2);
ctx.arc(cx, cy + rh * 0.1, rh * 0.75, 0, Math.PI * 2);
ctx.arc(cx - rw * 0.2, cy - rh * 0.25, rh * 0.65, 0, Math.PI * 2);
ctx.fill();
});
}
function drawGrass() {
// Main grass area
const grassGrad = ctx.createLinearGradient(0, groundY, 0, H);
grassGrad.addColorStop(0, '#7BC67E');
grassGrad.addColorStop(0.15, '#5DAE5F');
grassGrad.addColorStop(0.5, '#4A9A4C');
grassGrad.addColorStop(1, '#3B7A3D');
ctx.fillStyle = grassGrad;
ctx.fillRect(0, groundY, W, H - groundY);
// Grass blades
grassBlades.forEach(blade => {
const sway = Math.sin(blade.sway) * blade.swayAmp;
const bx = blade.x;
const by = groundY;
ctx.strokeStyle = blade.h > 16 ? '#4CAF50' : '#66BB6A';
ctx.lineWidth = 1.2;
ctx.beginPath();
ctx.moveTo(bx, by);
ctx.quadraticCurveTo(bx + sway, by - blade.h * 0.6, bx + sway * 1.4, by - blade.h);
ctx.stroke();
});
// Small flowers in grass
const flowerPositions = [
{ x: W * 0.08, y: groundY + 18 },
{ x: W * 0.22, y: groundY + 25 },
{ x: W * 0.45, y: groundY + 12 },
{ x: W * 0.63, y: groundY + 28 },
{ x: W * 0.79, y: groundY + 15 },
{ x: W * 0.91, y: groundY + 22 },
];
flowerPositions.forEach(fp => {
if (fp.x < W && fp.y < H) {
drawSmallFlower(fp.x, fp.y);
}
});
}
function drawSmallFlower(x, y) {
const colors = ['#FFD700', '#FFEB3B', '#FFF9C4', '#FFC107', '#FFE082'];
const petalColor = colors[Math.floor(Math.abs(x * 7) % colors.length)];
const size = 4 + Math.abs(Math.sin(x * 3.7)) * 3;
ctx.fillStyle = petalColor;
for (let i = 0; i < 5; i++) {
const angle = (i / 5) * Math.PI * 2;
const px = x + Math.cos(angle) * size;
const py = y + Math.sin(angle) * size;
ctx.beginPath();
ctx.arc(px, py, size * 0.55, 0, Math.PI * 2);
ctx.fill();
}
ctx.fillStyle = '#FFF';
ctx.beginPath();
ctx.arc(x, y, size * 0.4, 0, Math.PI * 2);
ctx.fill();
}
function drawButterflies() {
bgButterflies.forEach(bf => {
const wingFlap = Math.sin(bf.wingPhase) * 0.7;
ctx.save();
ctx.translate(bf.x, bf.y);
ctx.fillStyle = bf.color;
ctx.globalAlpha = 0.8;
// Left wing
ctx.beginPath();
ctx.ellipse(-bf.size * 0.5, 0, bf.size * 0.7, bf.size * 0.35 + wingFlap * bf.size * 0.3, -0.4, 0,
Math.PI * 2);
ctx.fill();
// Right wing
ctx.beginPath();
ctx.ellipse(bf.size * 0.5, 0, bf.size * 0.7, bf.size * 0.35 + wingFlap * bf.size * 0.3, 0.4, 0,
Math.PI * 2);
ctx.fill();
// Body
ctx.fillStyle = '#444';
ctx.beginPath();
ctx.arc(0, 0, bf.size * 0.2, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 1;
ctx.restore();
});
}
function drawGirl(x, y, scale = 1) {
ctx.save();
ctx.translate(x, y);
ctx.scale(scale, scale);
const bobOffset = Math.sin(playerBob) * 4;
const sadShake = playerSad > 0 ? Math.sin(playerSad * 20) * 3 * (playerSad / 30) : 0;
const happyBounce = playerHappy > 0 ? -Math.abs(Math.sin(playerHappy * 8)) * 8 : 0;
const totalOffsetY = bobOffset + sadShake + happyBounce;
// Shadow on ground
ctx.fillStyle = 'rgba(0,0,0,0.2)';
ctx.beginPath();
ctx.ellipse(0, 58, 22, 6, 0, 0, Math.PI * 2);
ctx.fill();
// Legs
ctx.fillStyle = '#FFD5B8';
ctx.fillRect(-8, 30, 6, 22);
ctx.fillRect(3, 30, 6, 22);
// Shoes
ctx.fillStyle = '#E85D75';
ctx.beginPath();
ctx.roundRect(-10, 50, 10, 8, 4);
ctx.fill();
ctx.beginPath();
ctx.roundRect(1, 50, 10, 8, 4);
ctx.fill();
// Socks
ctx.fillStyle = '#FFF';
ctx.fillRect(-8, 44, 6, 8);
ctx.fillRect(3, 44, 6, 8);
// Dress body
const dressGrad = ctx.createLinearGradient(0, -5, 0, 38);
dressGrad.addColorStop(0, '#FF7B9C');
dressGrad.addColorStop(0.5, '#FF5C85');
dressGrad.addColorStop(1, '#E84870');
ctx.fillStyle = dressGrad;
ctx.beginPath();
ctx.moveTo(-16, -2);
ctx.lineTo(-22, 36);
ctx.quadraticCurveTo(0, 44, 22, 36);
ctx.lineTo(16, -2);
ctx.closePath();
ctx.fill();
// Dress belt/sash
ctx.fillStyle = '#FFF';
ctx.fillRect(-15, 20, 30, 4);
ctx.fillStyle = '#FFD700';
ctx.beginPath();
ctx.arc(0, 22, 4, 0, Math.PI * 2);
ctx.fill();
// Dress collar
ctx.fillStyle = '#FFF';
ctx.beginPath();
ctx.moveTo(-10, -2);
ctx.lineTo(0, 6);
ctx.lineTo(10, -2);
ctx.quadraticCurveTo(0, -6, -10, -2);
ctx.fill();
// Arms
ctx.fillStyle = '#FFD5B8';
ctx.save();
ctx.translate(-16, 2);
ctx.rotate(-0.5);
ctx.fillRect(-3, -2, 7, 20);
ctx.restore();
ctx.save();
ctx.translate(16, 2);
ctx.rotate(0.5);
ctx.fillRect(-4, -2, 7, 20);
ctx.restore();
// Hands (small circles)
ctx.fillStyle = '#FFD5B8';
ctx.beginPath();
ctx.arc(-19, 20, 5, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(19, 20, 5, 0, Math.PI * 2);
ctx.fill();
// Head
ctx.fillStyle = '#FFD5B8';
ctx.beginPath();
ctx.arc(0, -14, 17, 0, Math.PI * 2);
ctx.fill();
// Hair
ctx.fillStyle = '#5C3A2E';
ctx.beginPath();
ctx.arc(0, -17, 18, Math.PI, Math.PI * 2);
ctx.fill();
// Hair sides
ctx.fillRect(-17, -18, 5, 20);
ctx.fillRect(12, -18, 5, 20);
// Pigtails
ctx.beginPath();
ctx.arc(-19, -10, 7, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(19, -10, 7, 0, Math.PI * 2);
ctx.fill();
// Hair bows
ctx.fillStyle = '#FF4081';
ctx.beginPath();
ctx.arc(-19, -8, 4, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(19, -8, 4, 0, Math.PI * 2);
ctx.fill();
// Bangs
ctx.fillStyle = '#5C3A2E';
ctx.beginPath();
ctx.ellipse(0, -24, 15, 7, 0, Math.PI, Math.PI * 2);
ctx.fill();
// Face
// Eyes
const eyeY = -17;
// White
ctx.fillStyle = '#FFF';
ctx.beginPath();
ctx.ellipse(-6, eyeY, 5.5, 6.5, 0, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.ellipse(6, eyeY, 5.5, 6.5, 0, 0, Math.PI * 2);
ctx.fill();
// Iris
ctx.fillStyle = '#3B2219';
ctx.beginPath();
ctx.arc(-5, eyeY + 1, 3.5, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(7, eyeY + 1, 3.5, 0, Math.PI * 2);
ctx.fill();
// Highlight
ctx.fillStyle = '#FFF';
ctx.beginPath();
ctx.arc(-3.5, eyeY - 1.5, 1.8, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(8.5, eyeY - 1.5, 1.8, 0, Math.PI * 2);
ctx.fill();
// Blush
ctx.fillStyle = 'rgba(255,180,180,0.6)';
ctx.beginPath();
ctx.ellipse(-10, -10, 4, 2.5, 0, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.ellipse(10, -10, 4, 2.5, 0, 0, Math.PI * 2);
ctx.fill();
// Mouth
if (playerSad > 5) {
// Sad mouth
ctx.strokeStyle = '#C94A5D';
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.arc(0, -5, 5, 0.2, Math.PI - 0.2);
ctx.stroke();
} else if (playerHappy > 5) {
// Happy open mouth
ctx.fillStyle = '#E85D75';
ctx.beginPath();
ctx.arc(0, -6, 4, 0, Math.PI);
ctx.fill();
} else {
// Neutral small smile
ctx.strokeStyle = '#C94A5D';
ctx.lineWidth = 1.3;
ctx.beginPath();
ctx.arc(0, -7, 4, 0.15, Math.PI - 0.15);
ctx.stroke();
}
// Basket
const basketY = 24;
ctx.fillStyle = '#C8945C';
ctx.beginPath();
ctx.ellipse(0, basketY, 16, 10, 0, Math.PI, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#A67342';
ctx.beginPath();
ctx.ellipse(0, basketY - 3, 16, 10, 0, Math.PI, Math.PI * 2);
ctx.fill();
// Basket rim
ctx.strokeStyle = '#D4A76A';
ctx.lineWidth = 2.5;
ctx.beginPath();
ctx.ellipse(0, basketY - 1, 17, 5, 0, 0, Math.PI * 2);
ctx.stroke();
// Basket weave lines
ctx.strokeStyle = 'rgba(139,90,43,0.4)';
ctx.lineWidth = 0.8;
for (let wy = basketY - 6; wy <= basketY + 4; wy += 3) {
ctx.beginPath();
const wyNorm = (wy - basketY) / 8;
const rx = 15 * Math.sqrt(Math.max(0, 1 - wyNorm * wyNorm));
ctx.moveTo(-rx, wy);
ctx.lineTo(rx, wy);
ctx.stroke();
}
ctx.restore();
}
function drawStrawberry(x, y, rotation = 0, scale = 1) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(rotation);
ctx.scale(scale, scale);
// Shadow
ctx.fillStyle = 'rgba(0,0,0,0.15)';
ctx.beginPath();
ctx.ellipse(2, 3, 13, 6, 0.1, 0, Math.PI * 2);
ctx.fill();
// Body
const berryGrad = ctx.createLinearGradient(-8, -10, 8, 10);
berryGrad.addColorStop(0, '#FF3B3B');
berryGrad.addColorStop(0.4, '#E82525');
berryGrad.addColorStop(0.75, '#CC1A1A');
berryGrad.addColorStop(1, '#A01010');
ctx.fillStyle = berryGrad;
ctx.beginPath();
ctx.moveTo(0, -14);
ctx.bezierCurveTo(-13, -8, -13, 6, -2, 13);
ctx.bezierCurveTo(3, 16, 8, 16, 12, 10);
ctx.bezierCurveTo(16, 3, 10, -8, 0, -14);
ctx.fill();
// Highlight
ctx.fillStyle = 'rgba(255,255,255,0.2)';
ctx.beginPath();
ctx.ellipse(-4, -4, 4, 7, -0.3, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = 'rgba(255,255,255,0.35)';
ctx.beginPath();
ctx.arc(-5, -6, 2.5, 0, Math.PI * 2);
ctx.fill();
// Seeds
ctx.fillStyle = '#FFEAA7';
const seedPositions = [
[-3, -1],
[4, -3],
[-5, 4],
[2, 2],
[-1, 7],
[6, 5],
[-4, -5],
[3, -7],
[0, -9],
[-6, 0],
[5, 0],
[-2, 10],
];
seedPositions.forEach(([sx, sy]) => {
ctx.beginPath();
ctx.ellipse(sx, sy, 1.2, 1.8, 0.2, 0, Math.PI * 2);
ctx.fill();
});
// Leaves / cap
ctx.fillStyle = '#2E8B2E';
ctx.beginPath();
ctx.moveTo(0, -13);
ctx.bezierCurveTo(-8, -20, -14, -16, -6, -10);
ctx.fill();
ctx.beginPath();
ctx.moveTo(0, -13);
ctx.bezierCurveTo(8, -20, 14, -16, 6, -10);
ctx.fill();
ctx.beginPath();
ctx.moveTo(0, -13);
ctx.bezierCurveTo(-3, -22, 3, -22, 0, -13);
ctx.fill();
// Stem
ctx.strokeStyle = '#3D6B3D';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, -14);
ctx.quadraticCurveTo(1, -20, 3, -23);
ctx.stroke();
ctx.restore();
}
function drawHomework(x, y, rotation = 0, scale = 1) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(rotation);
ctx.scale(scale, scale);
// Shadow
ctx.fillStyle = 'rgba(0,0,0,0.2)';
ctx.beginPath();
ctx.roundRect(-1, 1, 28, 34, 3);
ctx.fill();
// Paper
const paperGrad = ctx.createLinearGradient(-14, 0, 14, 0);
paperGrad.addColorStop(0, '#FEFEFE');
paperGrad.addColorStop(0.5, '#FFFEF9');
paperGrad.addColorStop(1, '#F9F6EE');
ctx.fillStyle = paperGrad;
ctx.beginPath();
ctx.roundRect(-14, -17, 28, 34, 3);
ctx.fill();
ctx.strokeStyle = '#DDD5C5';
ctx.lineWidth = 1;
ctx.stroke();
// Red margin line
ctx.strokeStyle = '#FF6B6B';
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(-8, -15);
ctx.lineTo(-8, 15);
ctx.stroke();
// Blue ruled lines
ctx.strokeStyle = '#B0D4F1';
ctx.lineWidth = 0.7;
for (let ly = -11; ly <= 13; ly += 4) {
ctx.beginPath();
ctx.moveTo(-12, ly);
ctx.lineTo(12, ly);
ctx.stroke();
}
// Scribble text
ctx.strokeStyle = '#555';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(-5, -9);
ctx.quadraticCurveTo(0, -8, 4, -9);
ctx.quadraticCurveTo(7, -10, 9, -8);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-5, -5);
ctx.quadraticCurveTo(2, -4, 8, -5);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-4, -1);
ctx.quadraticCurveTo(0, 0, 6, -1);
ctx.quadraticCurveTo(9, -2, 10, 0);
ctx.stroke();
// Red mark (like a grade)
ctx.fillStyle = '#FF3333';
ctx.font = 'bold 10px "Segoe UI", sans-serif';
ctx.fillText('F', 5, 10);
// Red circle
ctx.strokeStyle = '#FF3333';
ctx.lineWidth = 1.8;
ctx.beginPath();
ctx.arc(8, 7, 7, 0, Math.PI * 2);
ctx.stroke();
ctx.restore();
}
function createParticles(x, y, color, count = 15) {
for (let i = 0; i < count; i++) {
const angle = Math.random() * Math.PI * 2;
const speed = 2 + Math.random() * 8;
particles.push({
x,
y,
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed - Math.random() * 6,
life: 1,
decay: 0.015 + Math.random() * 0.04,
size: 1.5 + Math.random() * 4,
color,
});
}
// Limit particles
if (particles.length > 250) {
particles.splice(0, particles.length - 250);
}
}
function addFloatingText(x, y, text, color = '#FFF', size = 18) {
floatingTexts.push({
x,
y,
text,
color,
size,
life: 1,
vy: -2.5 - Math.random() * 2,
});
if (floatingTexts.length > 25) {
floatingTexts.shift();
}
}
// ─────────────────────────────────────
// SPAWN LOGIC
// ─────────────────────────────────────
function spawnItem() {
const isHomework = Math.random() < (0.18 + level * 0.035);
const x = 30 + Math.random() * (W - 60);
const baseSpeed = 1.8 + level * 0.35;
const speed = baseSpeed + Math.random() * 1.5;
items.push({
x,
y: -40,
type: isHomework ? 'homework' : 'strawberry',
speed,
rotation: (Math.random() - 0.5) * 1.2,
rotSpeed: (Math.random() - 0.5) * 0.04,
scale: 0.8 + Math.random() * 0.5,
wobble: Math.random() * Math.PI * 2,
wobbleSpeed: 0.02 + Math.random() * 0.04,
wobbleAmp: 0.3 + Math.random() * 1.2,
});
}
// ─────────────────────────────────────
// UPDATE
// ─────────────────────────────────────
function update(dt) {
if (gameState !== STATE.PLAYING) return;
// Cap dt to avoid large jumps
const cappedDt = Math.min(dt, 0.1);
gameTime += cappedDt;
// Update level based on score
level = Math.floor(score / 120) + 1;
if (level > 15) level = 15;
spawnInterval = Math.max(0.45, 1.4 - level * 0.07);
// Spawn timer
spawnTimer += cappedDt;
if (spawnTimer >= spawnInterval) {
spawnTimer -= spawnInterval;
spawnItem();
// Occasionally spawn extra items at higher levels
if (level > 4 && Math.random() < 0.25) {
setTimeout(() => {
if (gameState === STATE.PLAYING) spawnItem();
}, 150 + Math.random() * 300);
}
}
// Update player position (smooth follow)
const lerpFactor = 1 - Math.exp(-14 * cappedDt);
playerX += (targetX - playerX) * lerpFactor;
playerX = Math.max(30, Math.min(W - 30, playerX));
playerBob += cappedDt * 3;
if (playerHappy > 0) playerHappy -= cappedDt * 25;
if (playerSad > 0) playerSad -= cappedDt * 20;
if (playerScale > 1) playerScale -= cappedDt * 4;
if (playerScale < 1) playerScale = 1;
// Update shake
if (shakeDuration > 0) {
shakeDuration -= cappedDt;
shakeAmount *= 0.85;
if (shakeDuration <= 0) shakeAmount = 0;
}
// Update flash
if (flashAlpha > 0) {
flashAlpha -= cappedDt * 3;
if (flashAlpha < 0) flashAlpha = 0;
}
// Update items
for (let i = items.length - 1; i >= 0; i--) {
const item = items[i];
item.y += item.speed * 60 * cappedDt;
item.rotation += item.rotSpeed * 60 * cappedDt;
item.wobble += item.wobbleSpeed * 60 * cappedDt;
const wobbleX = Math.sin(item.wobble) * item.wobbleAmp;
// Check collision with player
const itemX = item.x + wobbleX;
const itemY = item.y;
const playerHitboxTop = playerY - 55;
const playerHitboxBottom = playerY + 10;
const playerHitboxLeft = playerX - 28;
const playerHitboxRight = playerX + 28;
const itemHitboxSize = item.type === 'strawberry' ? 16 : 14;
if (
itemX > playerHitboxLeft - itemHitboxSize &&
itemX < playerHitboxRight + itemHitboxSize &&
itemY > playerHitboxTop - itemHitboxSize &&
itemY < playerHitboxBottom + itemHitboxSize
) {
if (item.type === 'strawberry') {
// Caught strawberry!
combo++;
if (combo > maxCombo) maxCombo = combo;
const multiplier = Math.floor(combo / 6) + 1;
const points = 10 * multiplier;
score += points;
playerHappy = 28;
playerScale = 1.25;
flashAlpha = Math.min(flashAlpha + 0.2, 0.5);
createParticles(itemX, itemY, '#FF6B6B', 18);
createParticles(itemX, itemY, '#FFD700', 8);
addFloatingText(itemX, itemY - 10,
`+${points}${multiplier > 1 ? ' x' + multiplier : ''}`,
multiplier > 1 ? '#FFD700' : '#FFF', multiplier > 1 ? 22 : 17);
if (combo > 0 && combo % 6 === 0) {
playSound('combo');
addFloatingText(itemX, itemY - 30, 'COMBO!', '#FFD700', 26);
createParticles(itemX, itemY, '#FFD700', 25);
} else {
playSound('catch');
}
} else {
// Hit by homework!
combo = 0;
lives--;
playerSad = 30;
playerScale = 0.85;
shakeAmount = Math.max(shakeAmount, 8);
shakeDuration = 0.4;
createParticles(item