Created
June 3, 2024 18:59
-
-
Save SH20RAJ/5d58b061a502634ca3bc55638c003bd9 to your computer and use it in GitHub Desktop.
Dino game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Chrome Dino Game</title> | |
<style> | |
body { | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
height: 100vh; | |
margin: 0; | |
background-color: #f7f7f7; | |
font-family: Arial, sans-serif; | |
} | |
canvas { | |
border: 2px solid #000; | |
} | |
</style> | |
</head> | |
<body> | |
<canvas id="gameCanvas" width="800" height="200"></canvas> | |
<script src="game.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canvas = document.getElementById('gameCanvas'); | |
const ctx = canvas.getContext('2d'); | |
const dinoImg = new Image(); | |
const crowImg = new Image(); | |
const treeImg = new Image(); | |
dinoImg.src = 'https://cdn.dribbble.com/users/2365084/screenshots/4712140/tree-left-wind-dribbble.gif'; | |
crowImg.src = 'https://cdn.dribbble.com/users/2365084/screenshots/4712100/media/cf004aeb4c215d922732fc01877440f0.gif'; | |
treeImg.src = 'https://cdn.dribbble.com/users/2365084/screenshots/4712140/tree-left-wind-dribbble.gif'; | |
const dino = { | |
x: 50, | |
y: 150, | |
width: 44, | |
height: 47, | |
dy: 0, | |
gravity: 0.6, | |
jumpHeight: -12, | |
grounded: true | |
}; | |
const ground = { | |
x: 0, | |
y: 180, | |
width: canvas.width, | |
height: 20 | |
}; | |
const obstacles = []; | |
let gameSpeed = 4; | |
let score = 0; | |
let gameOver = false; | |
let frameCount = 0; | |
document.addEventListener('keydown', (e) => { | |
if (e.code === 'Space' && dino.grounded) { | |
dino.dy = dino.jumpHeight; | |
dino.grounded = false; | |
} | |
}); | |
function spawnObstacle() { | |
const type = Math.random() < 0.5 ? 'crow' : 'tree'; | |
const obstacle = { | |
x: canvas.width, | |
y: type === 'crow' ? 100 : 160, | |
width: type === 'crow' ? 50 : 20, | |
height: type === 'crow' ? 50 : 40, | |
type: type | |
}; | |
obstacles.push(obstacle); | |
} | |
function update() { | |
ctx.clearRect(0, 0, canvas.width, canvas.height); | |
// Ground logic | |
ground.x -= gameSpeed; | |
if (ground.x <= -ground.width) { | |
ground.x = 0; | |
} | |
ctx.fillStyle = 'green'; | |
ctx.fillRect(ground.x, ground.y, ground.width, ground.height); | |
ctx.fillRect(ground.x + ground.width, ground.y, ground.width, ground.height); | |
// Dino logic | |
dino.dy += dino.gravity; | |
dino.y += dino.dy; | |
if (dino.y >= 150) { | |
dino.y = 150; | |
dino.dy = 0; | |
dino.grounded = true; | |
} | |
ctx.drawImage(dinoImg, dino.x, dino.y, dino.width, dino.height); | |
// Obstacle logic | |
if (frameCount % 100 === 0) { | |
spawnObstacle(); | |
} | |
for (let i = 0; i < obstacles.length; i++) { | |
let obs = obstacles[i]; | |
obs.x -= gameSpeed; | |
if (obs.x + obs.width < 0) { | |
obstacles.splice(i, 1); | |
score++; | |
} | |
if (obs.type === 'crow') { | |
ctx.drawImage(crowImg, obs.x, obs.y, obs.width, obs.height); | |
} else { | |
ctx.drawImage(treeImg, obs.x, obs.y, obs.width, obs.height); | |
} | |
// Collision detection | |
if ( | |
dino.x < obs.x + obs.width && | |
dino.x + dino.width > obs.x && | |
dino.y < obs.y + obs.height && | |
dino.y + dino.height > obs.y | |
) { | |
gameOver = true; | |
} | |
} | |
// Score display | |
ctx.fillStyle = 'black'; | |
ctx.font = '20px Arial'; | |
ctx.fillText('Score: ' + score, 10, 30); | |
if (!gameOver) { | |
frameCount++; | |
requestAnimationFrame(update); | |
} else { | |
ctx.fillStyle = 'red'; | |
ctx.font = '30px Arial'; | |
ctx.fillText('Game Over', canvas.width / 2 - 70, canvas.height / 2); | |
ctx.font = '20px Arial'; | |
ctx.fillText('Press Space to Restart', canvas.width / 2 - 100, canvas.height / 2 + 30); | |
document.addEventListener('keydown', restartGame); | |
} | |
} | |
function restartGame(e) { | |
if (e.code === 'Space') { | |
document.removeEventListener('keydown', restartGame); | |
gameOver = false; | |
score = 0; | |
frameCount = 0; | |
obstacles.length = 0; | |
dino.y = 150; | |
dino.dy = 0; | |
update(); | |
} | |
} | |
update(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment