Untitled

 avatar
unknown
plain_text
a month ago
1.6 kB
4
Indexable
[3/16, 01:56] It's Me😊😊😊: <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Runner Game</title>
    <style>
        body { text-align: center; font-family: Arial, sans-serif; }
        canvas { background: black; display: block; margin: auto; }
    </style>
</head>
<body>
    <h1>Simple Runner Game</h1>
    <canvas id="gameCanvas" width="400" height="200"></canvas>
    <script src="script.js"></script>
</body>
</html>
[3/16, 01:56] It's Me😊😊😊: const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");

const WIDTH = 20;
const HEIGHT = 10;
const TILE_SIZE = 20;
let playerPos = 0;
let obstacles = [];

function drawGame() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // Draw obstacles
    for (let i = 0; i < obstacles.length; i++) {
        ctx.fillStyle = "red";
        ctx.fillRect(obstacles[i] * TILE_SIZE, (HEIGHT - 2) * TILE_SIZE, TILE_SIZE, TILE_SIZE);
    }

    // Draw player
    ctx.fillStyle = "white";
    ctx.fillRect(playerPos * TILE_SIZE, (HEIGHT - 1) * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}

function updateGame() {
    playerPos = (playerPos + 1) % WIDTH; // Move player right
    if (Math.random() < 0.2) { // Randomly place obstacles
        obstacles.push(Math.floor(Math.random() * WIDTH));
    }
    if (obstacles.length > 10) obstacles.shift(); // Remove old obstacles
}

function gameLoop() {
    updateGame();
    drawGame();
    setTimeout(gameLoop, 200);
}

gameLoop();
Editor is loading...
Leave a Comment