make a platfromer
A platformer sounds awesome! Quick questions so I build the right one: 1) Should your character just run and jump across platforms to reach a goal flag, or do you also want enemies to avoid/stomp? 2) Any theme — like space, jungle, or just simple blocky world? If you're not sure, I'll just build a solid first level with jumping, platforms, coins, and a goal flag, and we can add enemies next!
yes
Awesome, let's go! I built a complete first platformer level for you: run with A/D or arrow keys, jump with W/Space/Up, collect the yellow coins, avoid falling in gaps, and reach the green flag to win. There's also a simple enemy that walks back and forth — touch it from the side and you lose a life, but jump ON it to squash it! Try it out and tell me what to add next — more levels? Double jump? A different theme like space or jungle?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Platformer</title> <style> html, body { margin: 0; padding: 0; overflow: hidden; background: linear-gradient(#87CEEB, #c9f0ff); font-family: 'Trebuchet MS', sans-serif; height: 100%; } #ui { position: absolute; top: 10px; left: 10px; z-index: 10; color: #222; font-size: 22px; font-weight: bold; text-shadow: 1px 1px 2px white; } #msg { position: absolute; top: 40%; left: 50%; transform: translate(-50%,-50%); font-size: 40px; font-weight: bold; color: #ff4444; text-shadow: 2px 2px 4px white; display: none; z-index: 20; text-align: center; } #restartBtn { display: none; position: absolute; top: 58%; left: 50%; transform: translate(-50%,-50%); font-size: 20px; padding: 10px 20px; border-radius: 10px; border: none; background: #4caf50; color: white; font-weight: bold; cursor: pointer; z-index: 20; } canvas { display: block; } </style> </head> <body> <div id="ui">Coins: <span id="coins">0</span> / <span id="totalCoins">0</span> Lives: <span id="lives">3</span></div> <div id="msg"></div> <button id="restartBtn" onclick="restartGame()">Restart</button> <canvas id="game"></canvas> <script> const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); function resize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } window.addEventListener('resize', resize); resize(); const GRAVITY = 0.7; const FRICTION = 0.8; let keys = {}; window.addEventListener('keydown', e => { keys[e.code] = true; if (["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].includes(e.code)) e.preventDefault(); }); window.addEventListener('keyup', e => keys[e.code] = false); // Level definition (world coordinates) const levelWidth = 3200; const groundY = 560; let platforms = [ {x:0, y:groundY, w:700, h:100}, {x:800, y:groundY, w:300, h:100}, {x:1200, y:470, w:200, h:30}, {x:1500, y:400, w:200, h:30}, {x:1800, y:groundY, w:400, h:100}, {x:2300, y:470, w:150, h:30}, {x:2550, y:380, w:150, h:30}, {x:2800, y:groundY, w:400, h:100} ]; let coins = [ {x:850, y:520}, {x:950, y:520}, {x:1050, y:520}, {x:1270, y:430}, {x:1570, y:360}, {x:1900, y:520}, {x:2000, y:520}, {x:2100, y:520}, {x:2360, y:430}, {x:2610, y:340}, {x:2900, y:520}, {x:3000, y:520} ]; const totalCoins = coins.length; document.getElementById('totalCoins').textContent = totalCoins; let enemies = [ {x:850, y:groundY-30, w:30, h:30, dir:1, minX:800, maxX:1080, alive:true}, {x:1850, y:groundY-30, w:30, h:30, dir:1, minX:1800, maxX:2180, alive:true}, {x:2850, y:groundY-30, w:30, h:30, dir:1, minX:2800, maxX:3150, alive:true} ]; let goal = {x: 3100, y: groundY-80, w:40, h:80}; let player, camX, coinCount, lives, gameOver, win; function initGame() { player = { x: 50, y: 400, w: 34, h: 46, vx: 0, vy: 0, onGround: false, facing: 1 }; camX = 0; coinCount = 0; lives = 3; gameOver = false; win = false; coins.forEach(c => c.collected = false); enemies.forEach(e => { e.alive = true; e.x = e.minX + 50; e.dir = 1; }); document.getElementById('coins').textContent = coinCount; document.getElementById('lives').textContent = lives; document.getElementById('msg').style.display = 'none'; document.getElementById('restartBtn').style.display = 'none'; } function restartGame() { initGame(); } function rectsOverlap(a, b) { return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y; } function update() { if (gameOver) return; // horizontal input if (keys['ArrowLeft'] || keys['KeyA']) { player.vx -= 1.2; player.facing = -1; } if (keys['ArrowRight'] || keys['KeyD']) { player.vx += 1.2; player.facing = 1; } player.vx *= FRICTION; if (Math.abs(player.vx) > 8) player.vx = 8 * Math.sign(player.vx); // jump if ((keys['Space'] || keys['ArrowUp'] || keys['KeyW']) && player.onGround) { player.vy = -15; player.onGround = false; } player.vy += GRAVITY; if (player.vy > 20) player.vy = 20; // move horizontally and check collisions player.x += player.vx; player.onGround = false; for (const p of platforms) { if (rectsOverlap(player, p)) { if (player.vx > 0) player.x = p.x - player.w; else if (player.vx < 0) player.x = p.x + p.w; player.vx = 0; } } // move vertically player.y += player.vy; for (const p of platforms) { if (rectsOverlap(player, p)) { if (player.vy > 0) { player.y = p.y - player.h; player.vy = 0; player.onGround = true; } else if (player.vy < 0) { player.y = p.y + p.h; player.vy = 0; } } } // fell off world if (player.y > canvas.height + 300) { loseLife(); } // coins for (const c of coins) { if (!c.collected) { const cb = {x:c.x-10, y:c.y-10, w:20, h:20}; const pb = {x:player.x, y:player.y, w:player.w, h:player.h}; if (rectsOverlap(cb, pb)) { c.collected = true; coinCount++; document.getElementById('coins').textContent = coinCount; } } } // enemies for (const e of enemies) { if (!e.alive) continue; e.x += e.dir * 2; if (e.x < e.minX) { e.x = e.minX; e.dir = 1; } if (e.x + e.w > e.maxX) { e.x = e.maxX - e.w; e.dir = -1; } const pb = {x:player.x, y:player.y, w:player.w, h:player.h}; if (rectsOverlap(pb, e)) { // stomp check: player falling and above enemy if (player.vy > 0 && (player.y + player.h) - e.y < 20) { e.alive = false; player.vy = -10; } else { loseLife(); } } } // goal const pb = {x:player.x, y:player.y, w:player.w, h:player.h}; if (rectsOverlap(pb, goal)) { winGame(); } // camera camX = player.x - canvas.width/2; if (camX < 0) camX = 0; if (camX > levelWidth - canvas.width) camX = levelWidth - canvas.width; } function loseLife() { lives--; document.getElementById('lives').textContent = lives; if (lives <= 0) { endGame(false); } else { player.x = Math.max(50, player.x - 150); player.y = 400; player.vx = 0; player.vy = 0; } } function endGame(didWin) { gameOver = true; win = didWin; const msg = document.getElementById('msg'); msg.style.display = 'block'; msg.textContent = didWin ? "🎉 You Win! 🎉" : "💀 Game Over 💀"; msg.style.color = didWin ? "#2e7d32" : "#ff4444"; document.getElementById('restartBtn').style.display = 'block'; } function winGame() { endGame(true); } function draw() { ctx.clearRect(0,0,canvas.width,canvas.height); // background hills ctx.fillStyle = '#9fe08a'; for (let i=0;i<10;i++){ let hx = i*400 - (camX*0.3)%400; ctx.beginPath(); ctx.arc(hx, 620, 120, Math.PI, 2*Math.PI); ctx.fill(); } ctx.save(); ctx.translate(-camX, 0); // platforms ctx.fillStyle = '#8b5a2b'; for (const p of platforms) { ctx.fillStyle = '#6b3f1a'; ctx.fillRect(p.x, p.y, p.w, p.h); ctx.fillStyle = '#4caf50'; ctx.fillRect(p.x, p.y, p.w, 10); } // coins for (const c of coins) { if (!c.collected) { ctx.beginPath(); ctx.fillStyle = '#ffd700'; ctx.arc(c.x, c.y, 10, 0, Math.PI*2); ctx.fill(); ctx.strokeStyle = '#b8860b'; ctx.stroke(); } } // enemies for (const e of enemies) { if (e.alive) { ctx.fillStyle = '#c0392b'; ctx.fillRect(e.x, e.y, e.w, e.h); ctx.fillStyle = 'white'; ctx.fillRect(e.x+5, e.y+8, 6, 6); ctx.fillRect(e.x+19, e.y+8, 6, 6); ctx.fillStyle = 'black'; ctx.fillRect(e.x+7, e.y+10, 2, 2); ctx.fillRect(e.x+21, e.y+10, 2, 2); } } // goal flag ctx.fillStyle = '#555'; ctx.fillRect(goal.x, goal.y, 6, goal.h); ctx.fillStyle = '#e74c3c'; ctx.beginPath(); ctx.moveTo(goal.x+6, goal.y); ctx.lineTo(goal.x+40, goal.y+15); ctx.lineTo(goal.x+6, goal.y+30); ctx.closePath(); ctx.fill(); // player ctx.fillStyle = '#2980ff'; ctx.fillRect(player.x, player.y, player.w, player.h); // face ctx.fillStyle = 'white'; const eyeX = player.facing > 0 ? player.x + player.w - 12 : player.x + 4; ctx.fillRect(eyeX, player.y+10, 8, 8); ctx.fillStyle = 'black'; ctx.fillRect(eyeX + (player.facing>0?3:1), player.y+12, 3, 3); ctx.restore(); } function loop() { update(); draw(); requestAnimationFrame(loop); } initGame(); loop(); </script> </body> </html>