04/09/2024
كود لعبة الديناصور
Dinosaur Game
body {
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: ;
}
{
position: relative;
width: 600px;
height: 200px;
background-color: ;
border: 1px solid ;
overflow: hidden;
}
{
position: absolute;
bottom: 0;
width: 40px;
height: 40px;
background-color: #333;
}
.obstacle {
position: absolute;
bottom: 0;
width: 20px;
height: 40px;
background-color: #555;
}
const dino = document.getElementById('dino');
const game = document.getElementById('game');
let isJumping = false;
let gravity = 0.9;
let jumpHeight = 150;
let position = 0;
function handleKeyUp(event) {
if (event.keyCode === 32) {
if (!isJumping) {
jump();
}
}
}
function jump() {
isJumping = true;
let upInterval = setInterval(() => {
if (position >= jumpHeight) {
clearInterval(upInterval);
let downInterval = setInterval(() => {
if (position {
if (obstaclePosition < -20) {
clearInterval(leftInterval);
game.removeChild(obstacle);
} else if (obstaclePosition > 0 && obstaclePosition < 40 && position < 40) {
clearInterval(leftInterval);
document.body.innerHTML = 'Game Over';
} else {
obstaclePosition -= 10;
obstacle.style.left = obstaclePosition + 'px';
}
}, 20);
setTimeout(createObstacle, randomTime);
}
createObstacle();
document.addEventListener('keyup', handleKeyUp);