Meta Description" name="description" />
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Simple Runner Game</title>
<style>
body { margin: 0; overflow: hidden; background: #87CEEB; font-family: Arial, sans-serif; }
canvas { display: block; background: #f0f0f0; }
#score { position: absolute; top: 10px; left: 10px; font-size: 20px; color: #333; }
</style>
</head>
<body onclick="jump()">
<div id="score">Score: 0</div>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const scoreElement = document.getElementById("score");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let score = 0;
let gameSpeed = 5;
let isGameOver = false;
const player = {
x: 50,
y: canvas.height - 70,
width: 40,
height: 40,
color: "#007bff",
dy: 0,
jumpForce: 12,
gravity: 0.6,
grounded: false
};
const obstacles = [];
function spawnObstacle() {
const height = 40 + Math.random() * 40;
obstacles.push({
x: canvas.width,
y: canvas.height - height - 20,
width: 30,
height: height,
color: "#ff4757"
});
}
function jump() {
if (player.grounded) {
player.dy = -player.jumpForce;
player.grounded = false;
}
if (isGameOver) {
location.reload(); // গেম ওভার হলে ক্লিক করলে আবার শুরু হবে
}
}
function update() {
if (isGameOver) return;
player.dy += player.gravity;
player.y += player.dy;
if (player.y + player.height > canvas.height - 20) {
player.y = canvas.height - player.height - 20;
player.dy = 0;
player.grounded = true;
}
if (Math.random() < 0.02) {
if (obstacles.length === 0 || obstacles[obstacles.length - 1].x < canvas.width - 250) {
spawnObstacle();
}
}
for (let i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].x -= gameSpeed;
// Collision Detection
if (player.x < obstacles[i].x + obstacles[i].width &&
player.x + player.width > obstacles[i].x &&
player.y < obstacles[i].y + obstacles[i].height &&
player.y + player.height > obstacles[i].y) {
isGameOver = true;
alert("Game Over! Score: " + score);
}
if (obstacles[i].x + obstacles[i].width < 0) {
obstacles.splice(i, 1);
score++;
scoreElement.innerText = "Score: " + score;
gameSpeed += 0.1;
}
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Ground
ctx.fillStyle = "#2ed573";
ctx.fillRect(0, canvas.height - 20, canvas.width, 20);
// Player
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
// Obstacles
for (let obs of obstacles) {
ctx.fillStyle = obs.color;
ctx.fillRect(obs.x, obs.y, obs.width, obs.height);
}
if (isGameOver) {
ctx.fillStyle = "rgba(0,0,0,0.5)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "white";
ctx.font = "30px Arial";
ctx.textAlign = "center";
ctx.fillText("Game Over! Click to Restart", canvas.width / 2, canvas.height / 2);
}
}
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>
1
1
5KB
5KB
129.0ms
140.0ms
129.0ms