<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Trap Runner</title>
<style>
html, body {
margin: 0; padding: 0; overflow: hidden;
background: linear-gradient(#1b1f3b, #0a0c1a);
font-family: Arial, sans-serif;
height: 100%;
}
#ui {
position: absolute;
top: 10px; left: 10px;
color: #fff;
font-size: 20px;
z-index: 10;
text-shadow: 2px 2px 2px #000;
}
#msg {
position: absolute;
top: 40%;
width: 100%;
text-align: center;
color: #fff;
font-size: 36px;
font-weight: bold;
text-shadow: 3px 3px 4px #000;
display: none;
z-index: 20;
}
#restartBtn {
display: none;
position: absolute;
top: 55%;
left: 50%;
transform: translateX(-50%);
font-size: 22px;
padding: 10px 24px;
border-radius: 12px;
border: none;
background: #ffcc00;
color: #222;
font-weight: bold;
cursor: pointer;
z-index: 20;
}
canvas {
display: block;
margin: auto;
background: #2a2f55;
}
#howto {
position: absolute;
bottom: 8px;
left: 10px;
color: #ccc;
font-size: 14px;
z-index: 10;
}
</style>
</head>
<body>
<div id="ui">Level: <span id="levelNum">1</span> / <span id="levelTotal">6</span></div>
<div id="msg"></div>
<button id="restartBtn" onclick="restartLevel()">Try Again</button>
<div id="howto">Arrow keys / A D to move, Space or Up to jump</div>
<canvas id="game" width="900" height="500"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const msgEl = document.getElementById('msg');
const restartBtn = document.getElementById('restartBtn');
const levelNumEl = document.getElementById('levelNum');
const levelTotalEl = document.getElementById('levelTotal');
const GRAVITY = 0.48;
const W = canvas.width, H = canvas.height;
let levels = [
{
platforms: [
{x:0, y:460, w:220, h:40},
{x:260, y:460, w:150, h:40, trap:true},
{x:450, y:460, w:150, h:40},
{x:640, y:460, w:260, h:40}
],
spikes: [
{x:300, y:440, w:60, h:20}
],
goal: {x:820, y:400, w:40, h:60},
start: {x:40, y:400}
},
{
platforms: [
{x:0, y:460, w:150, h:40},
{x:190, y:460, w:120, h:40, trap:true},
{x:350, y:400, w:120, h:40},
{x:510, y:460, w:120, h:40, trap:true},
{x:670, y:460, w:230, h:40}
],
spikes: [
{x:360, y:380, w:40, h:20},
{x:700, y:440, w:60, h:20}
],
goal: {x:850, y:400, w:40, h:60},
start: {x:40, y:400}
},
{
platforms: [
{x:0, y:460, w:120, h:40},
{x:160, y:400, w:100, h:40, trap:true},
{x:300, y:460, w:100, h:40},
{x:440, y:350, w:100, h:40, trap:true},
{x:580, y:460, w:100, h:40},
{x:720, y:400, w:180, h:40}
],
spikes: [
{x:310, y:440, w:60, h:20},
{x:590, y:440, w:60, h:20}
],
goal: {x:850, y:340, w:40, h:60},
start: {x:40, y:400}
},
{
platforms: [
{x:0, y:460, w:100, h:40},
{x:140, y:460, w:90, h:40, trap:true},
{x:270, y:420, w:90, h:40, trap:true},
{x:400, y:380, w:90, h:40},
{x:530, y:420, w:90, h:40, trap:true},
{x:660, y:460, w:90, h:40},
{x:790, y:400, w:110, h:40}
],
spikes: [
{x:150, y:440, w:60, h:20},
{x:410, y:360, w:60, h:20},
{x:670, y:440, w:60, h:20}
],
goal: {x:820, y:340, w:40, h:60},
start: {x:40, y:400}
},
{
platforms: [
{x:0, y:460, w:100, h:40},
{x:130, y:400, w:80, h:40, trap:true},
{x:250, y:460, w:80, h:40, trap:true},
{x:370, y:320, w:80, h:40},
{x:490, y:400, w:80, h:40, trap:true},
{x:610, y:460, w:80, h:40, trap:true},
{x:730, y:380, w:170, h:40}
],
spikes: [
{x:260, y:440, w:60, h:20},
{x:380, y:300, w:40, h:20},
{x:620, y:440, w:60, h:20}
],
goal: {x:860, y:320, w:40, h:60},
start: {x:40, y:400}
},
{
platforms: [
{x:0, y:460, w:90, h:40},
{x:120, y:460, w:80, h:40, trap:true},
{x:230, y:400, w:80, h:40, trap:true},
{x:340, y:460, w:80, h:40},
{x:450, y:340, w:80, h:40, trap:true},
{x:560, y:420, w:80, h:40, trap:true},
{x:670, y:460, w:80, h:40, trap:true},
{x:780, y:400, w:120, h:40}
],
spikes: [
{x:130, y:440, w:60, h:20},
{x:340, y:440, w:60, h:20},
{x:460, y:320, w:40, h:20},
{x:680, y:440, w:60, h:20}
],
goal: {x:820, y:340, w:40, h:60},
start: {x:40, y:400}
}
];
let currentLevel = 0;
let keys = {};
let player, platforms, spikes, goal;
let gameOver = false;
let won = false;
let allWon = false;
function loadLevel(i) {
let lvl = levels[i];
platforms = lvl.platforms.map(p => ({...p, fallen:false, fallY:0, triggered:false, timer:0}));
spikes = lvl.spikes;
goal = lvl.goal;
player = {
x: lvl.start.x, y: lvl.start.y,
w: 28, h: 38,
vx: 0, vy: 0,
onGround: false,
facing: 1
};
gameOver = false;
won = false;
allWon = false;
msgEl.style.display = 'none';
restartBtn.style.display = 'none';
levelNumEl.textContent = (i+1);
levelTotalEl.textContent = levels.length;
}
function restartLevel() {
loadLevel(currentLevel);
}
document.addEventListener('keydown', e => {
keys[e.code] = true;
if ((e.code === 'Space' || e.code === 'Enter') && (gameOver || allWon)) {
allWon = false;
loadLevel(currentLevel);
} else if ((e.code === 'Space' || e.code === 'Enter') && won) {
if (currentLevel + 1 >= levels.length) {
currentLevel = 0;
allWon = true;
won = false;
showMsg("🏆 YOU BEAT THE WHOLE GAME! 🏆 Press Space to play again");
} else {
currentLevel++;
loadLevel(currentLevel);
}
}
});
document.addEventListener('keyup', e => keys[e.code] = false);
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 || won || allWon) return;
let speed = 4.2;
if (keys['ArrowLeft'] || keys['KeyA']) { player.vx = -speed; player.facing = -1; }
else if (keys['ArrowRight'] || keys['KeyD']) { player.vx = speed; player.facing = 1; }
else player.vx = 0;
if ((keys['Space'] || keys['ArrowUp'] || keys['KeyW']) && player.onGround) {
player.vy = -13.5;
player.onGround = false;
}
player.vy += GRAVITY;
if (player.vy > 18) player.vy = 18;
player.x += player.vx;
player.y += player.vy;
player.onGround = false;
for (let p of platforms) {
if (p.fallen) continue;
let py = p.y + p.fallY;
let pRect = {x: p.x, y: py, w: p.w, h: p.h};
if (p.trap && !p.triggered) {
let px = player.x + player.w/2;
if (px > p.x + 10 && px < p.x + p.w - 10 &&
player.y + player.h <= py + 15 && player.y + player.h >= py - 30) {
p.triggered = true;
}
}
if (p.triggered && !p.fallen) {
p.timer++;
if (p.timer > 14) {
p.fallY += 5;
if (p.fallY > 500) p.fallen = true;
}
}
let prevBottom = player.y + player.h - player.vy;
if (player.vx !== undefined) {
if (player.x + player.w > pRect.x && player.x < pRect.x + pRect.w) {
if (prevBottom <= pRect.y + 2 && player.y + player.h >= pRect.y && player.vy >= 0) {
player.y = pRect.y - player.h;
player.vy = 0;
player.onGround = true;
}
}
}
}
if (player.y > H + 50) {
gameOver = true;
showMsg("You fell! 💀");
}
for (let s of spikes) {
if (rectsOverlap(player, s)) {
gameOver = true;
showMsg("Ouch! Spikes got you 💀");
}
}
if (rectsOverlap(player, goal)) {
won = true;
if (currentLevel + 1 >= levels.length) {
showMsg("Last Level Complete! 🎉 Press Space to see your victory");
} else {
showMsg("Level Complete! 🎉 Press Space for next");
}
}
if (player.x < 0) player.x = 0;
if (player.x + player.w > W) player.x = W - player.w;
}
function showMsg(text) {
msgEl.textContent = text;
msgEl.style.display = 'block';
restartBtn.style.display = 'block';
}
function draw() {
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = '#2a2f55';
ctx.fillRect(0, 0, W, H);
for (let p of platforms) {
if (p.fallen) continue;
let py = p.y + p.fallY;
if (p.trap) {
ctx.fillStyle = p.triggered ? '#e84c4c' : '#c97b3d';
} else {
ctx.fillStyle = '#6fcf97';
}
ctx.fillRect(p.x, py, p.w, p.h);
ctx.fillStyle = 'rgba(0,0,0,0.2)';
ctx.fillRect(p.x, py, p.w, 6);
}
ctx.fillStyle = '#ddd';
for (let s of spikes) {
let spikeCount = Math.floor(s.w / 20);
for (let i = 0; i < spikeCount; i++) {
ctx.beginPath();
ctx.moveTo(s.x + i*20, s.y + s.h);
ctx.lineTo(s.x + i*20 + 10, s.y);
ctx.lineTo(s.x + i*20 + 20, s.y + s.h);
ctx.closePath();
ctx.fill();
}
}
ctx.fillStyle = '#ffd166';
ctx.fillRect(goal.x, goal.y, goal.w, goal.h);
ctx.fillStyle = '#fff';
ctx.font = '12px Arial';
ctx.fillText("GOAL", goal.x - 2, goal.y - 6);
ctx.fillStyle = '#ff6b6b';
ctx.fillRect(player.x, player.y, player.w, player.h);
ctx.fillStyle = '#fff';
let eyeX = player.facing === 1 ? player.x + player.w - 8 : player.x + 4;
ctx.fillRect(eyeX, player.y + 8, 5, 5);
}
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
loadLevel(currentLevel);
loop();
</script>
</body>
</html>