بازی دایناسور گوگل (T-Rex Runner) یک بازی ساده و سرگرمکننده است که زمانی که ارتباط شما با اینترنت قطع میشود در مرورگر گوگل کروم نمایش داده میشود. در زیر یک نسخه ساده از این بازی را با استفاده از HTML و JavaScript ایجاد کردهام. شما میتوانید این کد را کپی کرده و در یک فایل HTML ذخیره کنید و سپس آن را در مرورگر خود باز کنید تا بازی را شروع کنید.
html Copy code
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>بازی دایناسور گوگل</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f7f7f7;
font-family: 'Arial', sans-serif;
}
canvas {
background-color: #fff;
border: 2px solid #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="600" height="200"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let dino = {
x: 50,
y: 150,
width: 20,
height: 20,
gravity: 0.6,
jumpHeight: 10,
velocityY: 0,
onGround: false
};
let obstacles = [];
let score = 0;
let gameOver = false;
function jump() {
if (dino.onGround) {
dino.velocityY = -dino.jumpHeight;
dino.onGround = false;
}
}
function spawnObstacle() {
let obstacle = {
x: canvas.width,
y: 160,
width: 20,
height: 20
};
obstacles.push(obstacle);
}
function update() {
// Gravity
if (!dino.onGround) {
dino.velocityY += dino.gravity;
dino.y += dino.velocityY;
}
// Check if dino is on the ground
if (dino.y >= 150) {
dino.y = 150;
dino.onGround = true;
dino.velocityY = 0;
}
// Move obstacles
for (let i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].x -= 5;
if (obstacles[i].x < 0) {
obstacles.splice(i, 1);
score++;
}
// Check collision
if (dino.x < obstacles[i].x + obstacles[i].width &&
dino.x + dino.width > obstacles[i].x &&
dino.y < obstacles[i].y + obstacles[i].height &&
dino.y + dino.height > obstacles[i].y) {
gameOver = true;
}
}
if (gameOver) {
clearInterval(gameInterval);
alert('بازی تمام شد! امتیاز شما: ' + score);
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'green';
ctx.fillRect(dino.x, dino.y, dino.width, dino.height);
ctx.fillStyle = 'red';
for (let obstacle of obstacles) {
ctx.fillRect(obstacle.x, obstacle.y - obstacle.height, obstacle.width, obstacle.height);
}
ctx.fillStyle = 'black';
ctx.fillText('امتیاز: ' + score, 10, 20);
}
function gameLoop() {
update();
draw();
}
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
jump();
}
});
const gameInterval = setInterval(function() {
spawnObstacle();
}, 1500);
setInterval(gameLoop, 20);
</script>
</body>
</html>
شما میتوانید این کد را به دلخواه تغییر دهید و ویژگیهای بیشتری به آن اضافه کنید. امیدوارم از بازی لذت ببرید!