diff --git a/Dino game/index.html b/Dino game/index.html new file mode 100644 index 0000000..abb8247 --- /dev/null +++ b/Dino game/index.html @@ -0,0 +1,104 @@ + + +
+ + + + + + + + + + diff --git a/Dino game/script.js b/Dino game/script.js new file mode 100644 index 0000000..2ef632a --- /dev/null +++ b/Dino game/script.js @@ -0,0 +1,75 @@ +const dino = document.querySelector(".dino"); +const obstacle = document.querySelector(".obstacle"); +const scoreDisplay = document.querySelector(".score"); + +let isJumping = false; +let score = 0; + +document.addEventListener("keydown", jump); + +function jump(event) { + if (event.keyCode === 32 && !isJumping) { + isJumping = true; + + let jumpUp = 0; + const jumpInterval = setInterval(() => { + if (jumpUp >= 100) { + clearInterval(jumpInterval); + let fallDown = 0; + const fallInterval = setInterval(() => { + if (fallDown <= 0) { + clearInterval(fallInterval); + isJumping = false; + } else { + jumpUp -= 5; + fallDown -= 5; + dino.style.bottom = jumpUp + "px"; + } + }, 20); + } else { + jumpUp += 5; + dino.style.bottom = jumpUp + "px"; + } + }, 20); + } +} + +function updateScore() { + score++; + scoreDisplay.textContent = score; +} + +function checkCollision() { + if (score > 0) { + const dinoPosition = dino.getBoundingClientRect(); + const obstaclePosition = obstacle.getBoundingClientRect(); + + if ( + dinoPosition.left < obstaclePosition.left + obstaclePosition.width && + dinoPosition.left + dinoPosition.width > obstaclePosition.left && + dinoPosition.top < obstaclePosition.top + obstaclePosition.height && + dinoPosition.top + dinoPosition.height > obstaclePosition.top + ) { + alert(`Game Over! Your score: ${score}`); + location.reload(); // Reload the game + } + } +} + +function moveObstacle() { + let obstaclePosition = 300; + + const moveInterval = setInterval(() => { + if (obstaclePosition <= -20) { + clearInterval(moveInterval); + updateScore(); + moveObstacle(); + } else { + obstaclePosition -= 5; + obstacle.style.right = obstaclePosition + "px"; + checkCollision(); + } + }, 20); +} + +moveObstacle(); diff --git a/Dino game/style.css b/Dino game/style.css new file mode 100644 index 0000000..96194c9 --- /dev/null +++ b/Dino game/style.css @@ -0,0 +1,43 @@ +body { + margin: 0; + padding: 0; + display: flex; + justify-content: center; + align-items: center; + min-height: 100vh; + background-color: #f7f7f7; +} + +.game-container { + position: relative; + width: 300px; + height: 150px; + border: 1px solid #000; + overflow: hidden; +} + +.dino { + position: absolute; + bottom: 0; + left: 30px; + width: 30px; + height: 30px; + background-color: #666; +} + +.obstacle { + position: absolute; + bottom: 0; + right: 0; + width: 20px; + height: 30px; + background-color: #333; +} + +.score { + position: absolute; + top: 10px; + right: 10px; + font-size: 24px; + font-weight: bold; +}