<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Endless Vertical Platformer</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
}
canvas {
display: block;
margin: auto;
background-color: #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const PLAYER_SIZE = 20;
const PLATFORM_WIDTH = 80;
const PLATFORM_HEIGHT = 20;
const PLATFORM_GAP = 200;
const PLATFORM_SPEED = 2;
const PLAYER_SPEED = 5;
let playerX = canvas.width / 2;
let playerY = canvas.height / 2;
let platforms = [];
let score = 0;
function drawPlayer() {
ctx.fillStyle = '#fff';
ctx.fillRect(playerX - PLAYER_SIZE / 2, playerY - PLAYER_SIZE / 2, PLAYER_SIZE, PLAYER_SIZE);
}
function drawPlatforms() {
ctx.fillStyle = '#fff';
platforms.forEach(platform => {
ctx.fillRect(platform.x, platform.y, PLATFORM_WIDTH, PLATFORM_HEIGHT);
});
}
function movePlatforms() {
platforms.forEach(platform => {
platform.y -= PLATFORM_SPEED;
if (platform.y + PLATFORM_HEIGHT < 0) {
platforms.shift();
score++;
}
});
}
function generatePlatforms() {
const lastPlatform = platforms[platforms.length - 1];
if (!lastPlatform || lastPlatform.y + PLATFORM_GAP < canvas.height) {
const x = Math.random() * (canvas.width - PLATFORM_WIDTH);
const y = canvas.height + PLATFORM_GAP;
platforms.push({ x, y });
}
}
function update() {
movePlatforms();
generatePlatforms();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawPlatforms();
requestAnimationFrame(draw);
}
function handleKeyPress(e) {
if (e.key === 'ArrowLeft' && playerX > PLAYER_SIZE / 2) {
playerX -= PLAYER_SPEED;
} else if (e.key === 'ArrowRight' && playerX < canvas.width - PLAYER_SIZE / 2) {
playerX += PLAYER_SPEED;
} else if (e.key === 'ArrowUp' && playerY > PLAYER_SIZE / 2) {
playerY -= PLAYER_SPEED;
} else if (e.key === 'ArrowDown' && playerY < canvas.height - PLAYER_SIZE / 2) {
playerY += PLAYER_SPEED;
}
}
document.addEventListener('keydown', handleKeyPress);
function mainLoop() {
update();
draw();
}
setInterval(mainLoop, 1000 / 60); // 60 FPS
</script>
</body>
</html>
1
1
1KB
3KB
88.0ms
92.0ms
92.0ms