HTML
HTML
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Platformer Game</title>
<style>
body { margin: 0; padding: 0; overflow: hidden; background: #87CEEB; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Set up the canvas
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Colors
const BLUE = "#0000FF";
const GREEN = "#008000";
const WHITE = "#FFFFFF";
// Player object
const player = {
x: 100,
y: SCREEN_HEIGHT - 150,
width: 50,
height: 50,
speed: 5,
dx: 0,
dy: 0,
gravity: 0.8,
jumpPower: -15,
isJumping: false
};
// Platform object
const platforms = [
{ x: 0, y: SCREEN_HEIGHT - 50, width: SCREEN_WIDTH, height: 50 },
{ x: 200, y: SCREEN_HEIGHT - 200, width: 200, height: 20 },
{ x: 400, y: SCREEN_HEIGHT - 300, width: 200, height: 20 }
];
// Gravity effect
if (player.y + player.height < SCREEN_HEIGHT) {
player.dy += player.gravity;
} else {
player.y = SCREEN_HEIGHT - player.height;
player.dy = 0;
player.isJumping = false;
}
function keyUp(e) {
if (e.key === "ArrowLeft" || e.key === "a" || e.key === "ArrowRight" ||
e.key === "d") {
player.dx = 0;
}
}
function drawPlatforms() {
ctx.fillStyle = GREEN;
for (let platform of platforms) {
ctx.fillRect(platform.x, platform.y, platform.width, platform.height);
}
}
</script>
</body>
</html>