can you make a portal and puzzle based two player platformer game
Ooh, a two-player portal puzzle platformer sounds awesome! Let me make sure I build the right thing. A few quick questions: 1. Should the two players use the SAME keyboard (like Player 1 on WASD, Player 2 on Arrow keys)? 2. For portals - do you want colored portal pairs that are already placed in the level (step into blue, come out orange), or should players be able to SHOOT/place their own portals like in the Portal game? 3. Should the goal be to get BOTH players to their own exit doors to finish each level? Tell me what sounds fun, or just say "you pick" and I'll make a first version!
yes to all of them but its a competition to reach the goal
Awesome! I built a 2-player portal platformer race: Player 1 (blue) uses WASD, Player 2 (orange) uses Arrow Keys. Jump through the swirly portals to teleport across the level, dodge spikes, and whoever touches their star goal FIRST wins the round! There's a scoreboard and you can press R to restart. Try it out and tell me what to add next — maybe more levels, a timer, or power-ups?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Portal Rush</title> <style> html, body { margin: 0; padding: 0; height: 100%; background: #10121c; font-family: 'Trebuchet MS', sans-serif; overflow: hidden; user-select: none; } #ui { position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: center; padding: 10px 20px; color: white; z-index: 10; pointer-events: none; } .scoreBox { font-size: 20px; font-weight: bold; padding: 6px 14px; border-radius: 10px; } #p1score { background: rgba(70,140,255,0.3); color: #7fc3ff; border: 2px solid #4aa3ff; } #p2score { background: rgba(255,150,60,0.3); color: #ffb366; border: 2px solid #ff9a3c; } #title { font-size: 22px; color: #fff; text-shadow: 0 0 8px #8855ff; } #msg { position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%); font-size: 48px; font-weight: bold; color: white; text-shadow: 0 0 20px #ffcc00; z-index: 20; display: none; text-align: center; } #restartHint { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); color: #aaa; font-size: 14px; z-index: 10; } canvas { display: block; margin: 0 auto; background: #1b1e2e; } </style> </head> <body> <div id="ui"> <div class="scoreBox" id="p1score">Blue (WASD): 0</div> <div id="title">⭐ Portal Rush ⭐</div> <div class="scoreBox" id="p2score">Orange (Arrows): 0</div> </div> <div id="msg"></div> <div id="restartHint">Press R to restart the round</div> <canvas id="game" width="900" height="560"></canvas> <script> const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); const msgEl = document.getElementById('msg'); const p1scoreEl = document.getElementById('p1score'); const p2scoreEl = document.getElementById('p2score'); const GRAVITY = 0.55; const FRICTION = 0.82; let scores = {p1: 0, p2: 0}; let roundOver = false; // Platforms: x, y, w, h const platforms = [ {x:0, y:530, w:900, h:30}, // ground {x:0, y:0, w:20, h:560}, // left wall {x:880, y:0, w:20, h:560}, // right wall {x:150, y:430, w:140, h:20}, {x:400, y:380, w:120, h:20}, {x:620, y:430, w:140, h:20}, {x:60, y:300, w:120, h:20}, {x:700, y:300, w:140, h:20}, {x:300, y:230, w:160, h:20}, {x:60, y:150, w:140, h:20}, {x:680, y:150, w:160, h:20}, {x:380, y:100, w:140, h:20}, ]; // Spikes (danger): x,y,w,h — touching resets the player to start const spikes = [ {x:300, y:510, w:60, h:20}, {x:500, y:510, w:60, h:20}, ]; // Portals: pairs, colored. Each has x,y,w,h and a linked partner index const portals = [ {x:90, y:260, w:16, h:60, color:'#5fd3ff', link:1}, {x:730, y:260, w:16, h:60, color:'#5fd3ff', link:0}, {x:330, y:170, w:16, h:60, color:'#ff66d9', link:3}, {x:420, y:60, w:16, h:60, color:'#ff66d9', link:2}, ]; const START_P1 = {x: 40, y: 480}; const START_P2 = {x: 820, y: 480}; const GOAL_P1 = {x: 820, y: 60, r: 22}; // orange-ish goal for p1? both race to own color goal const GOAL_P2 = {x: 40, y: 60, r: 22}; function makePlayer(start, color) { return { x: start.x, y: start.y, w: 28, h: 36, vx: 0, vy: 0, onGround: false, color: color, portalCooldown: 0, alive: true, }; } let p1 = makePlayer(START_P1, '#4aa3ff'); let p2 = makePlayer(START_P2, '#ff9a3c'); const keys = {}; window.addEventListener('keydown', e => { keys[e.key.toLowerCase()] = true; if (e.key.toLowerCase() === 'r') resetRound(); if (['arrowup','arrowdown','arrowleft','arrowright',' '].includes(e.key.toLowerCase())) e.preventDefault(); }); window.addEventListener('keyup', e => { keys[e.key.toLowerCase()] = false; }); function resetRound() { p1 = makePlayer(START_P1, '#4aa3ff'); p2 = makePlayer(START_P2, '#ff9a3c'); roundOver = false; msgEl.style.display = 'none'; } 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 updatePlayer(p, left, right, up) { if (!p.alive) return; if (keys[left]) p.vx -= 0.9; if (keys[right]) p.vx += 0.9; p.vx *= FRICTION; if (Math.abs(p.vx) > 6) p.vx = Math.sign(p.vx) * 6; if (keys[up] && p.onGround) { p.vy = -12.5; p.onGround = false; } p.vy += GRAVITY; if (p.vy > 15) p.vy = 15; // horizontal move + collide p.x += p.vx; for (const plat of platforms) { if (rectsOverlap(p, plat)) { if (p.vx > 0) p.x = plat.x - p.w; else if (p.vx < 0) p.x = plat.x + plat.w; p.vx = 0; } } // vertical move + collide p.y += p.vy; p.onGround = false; for (const plat of platforms) { if (rectsOverlap(p, plat)) { if (p.vy > 0) { p.y = plat.y - p.h; p.vy = 0; p.onGround = true; } else if (p.vy < 0) { p.y = plat.y + plat.h; p.vy = 0; } } } // spikes for (const s of spikes) { if (rectsOverlap(p, s)) { respawn(p); return; } } // portals if (p.portalCooldown > 0) p.portalCooldown--; if (p.portalCooldown === 0) { for (let i = 0; i < portals.length; i++) { const port = portals[i]; if (rectsOverlap(p, port)) { const target = portals[port.link]; p.x = target.x + target.w/2 - p.w/2; p.y = target.y + target.h/2 - p.h/2; p.portalCooldown = 25; break; } } } // bounds safety if (p.y > 600) respawn(p); } function respawn(p) { const start = (p === p1) ? START_P1 : START_P2; p.x = start.x; p.y = start.y; p.vx = 0; p.vy = 0; } function checkWin() { if (roundOver) return; const d1 = Math.hypot((p1.x + p1.w/2) - GOAL_P1.x, (p1.y + p1.h/2) - GOAL_P1.y); const d2 = Math.hypot((p2.x + p2.w/2) - GOAL_P2.x, (p2.y + p2.h/2) - GOAL_P2.y); if (d1 < GOAL_P1.r) { scores.p1++; endRound('Blue Wins! 🎉'); } else if (d2 < GOAL_P2.r) { scores.p2++; endRound('Orange Wins! 🎉'); } } function endRound(text) { roundOver = true; msgEl.textContent = text; msgEl.style.display = 'block'; p1scoreEl.textContent = 'Blue (WASD): ' + scores.p1; p2scoreEl.textContent = 'Orange (Arrows): ' + scores.p2; } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); // background grid ctx.strokeStyle = 'rgba(255,255,255,0.03)'; for (let gx = 0; gx < canvas.width; gx += 40) { ctx.beginPath(); ctx.moveTo(gx,0); ctx.lineTo(gx,canvas.height); ctx.stroke(); } for (let gy = 0; gy < canvas.height; gy += 40) { ctx.beginPath(); ctx.moveTo(0,gy); ctx.lineTo(canvas.width,gy); ctx.stroke(); } // platforms ctx.fillStyle = '#3a3f5c'; for (const plat of platforms) { ctx.fillRect(plat.x, plat.y, plat.w, plat.h); ctx.strokeStyle = '#555c88'; ctx.strokeRect(plat.x, plat.y, plat.w, plat.h); } // spikes ctx.fillStyle = '#e63946'; for (const s of spikes) { const count = Math.floor(s.w / 15); for (let i = 0; i < count; i++) { ctx.beginPath(); ctx.moveTo(s.x + i*15, s.y + s.h); ctx.lineTo(s.x + i*15 + 7.5, s.y); ctx.lineTo(s.x + i*15 + 15, s.y + s.h); ctx.closePath(); ctx.fill(); } } // portals (glowing) const t = Date.now() / 200; for (const port of portals) { ctx.save(); ctx.shadowColor = port.color; ctx.shadowBlur = 15 + Math.sin(t)*5; ctx.fillStyle = port.color; ctx.fillRect(port.x, port.y, port.w, port.h); ctx.restore(); } // goals drawGoal(GOAL_P1, '#4aa3ff', 'P1'); drawGoal(GOAL_P2, '#ff9a3c', 'P2'); // players drawPlayer(p1); drawPlayer(p2); } function drawGoal(g, color, label) { ctx.save(); ctx.shadowColor = '#ffee88'; ctx.shadowBlur = 20; ctx.fillStyle = '#ffee88'; drawStar(g.x, g.y, 5, g.r, g.r/2); ctx.restore(); ctx.strokeStyle = color; ctx.lineWidth = 3; ctx.beginPath(); ctx.arc(g.x, g.y, g.r + 6, 0, Math.PI*2); ctx.stroke(); ctx.lineWidth = 1; } function drawStar(cx, cy, spikesN, outerR, innerR) { let rot = Math.PI / 2 * 3; let x = cx, y = cy; const step = Math.PI / spikesN; ctx.beginPath(); ctx.moveTo(cx, cy - outerR); for (let i = 0; i < spikesN; i++) { x = cx + Math.cos(rot) * outerR; y = cy + Math.sin(rot) * outerR; ctx.lineTo(x, y); rot += step; x = cx + Math.cos(rot) * innerR; y = cy + Math.sin(rot) * innerR; ctx.lineTo(x, y); rot += step; } ctx.lineTo(cx, cy - outerR); ctx.closePath(); ctx.fill(); } function drawPlayer(p) { ctx.save(); ctx.shadowColor = p.color; ctx.shadowBlur = 10; ctx.fillStyle = p.color; ctx.fillRect(p.x, p.y, p.w, p.h); ctx.restore(); // eyes ctx.fillStyle = 'white'; ctx.fillRect(p.x + 5, p.y + 8, 6, 6); ctx.fillRect(p.x + 17, p.y + 8, 6, 6); ctx.fillStyle = 'black'; ctx.fillRect(p.x + 7, p.y + 10, 3, 3); ctx.fillRect(p.x + 19, p.y + 10, 3, 3); } function loop() { if (!roundOver) { updatePlayer(p1, 'a', 'd', 'w'); updatePlayer(p2, 'arrowleft', 'arrowright', 'arrowup'); checkWin(); } draw(); requestAnimationFrame(loop); } loop(); </script> </body> </html>