|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <style> |
| 7 | + #dino { |
| 8 | + width: 40px; |
| 9 | + height: 40px; |
| 10 | + background-color: green; |
| 11 | + position: absolute; |
| 12 | + bottom: 0; |
| 13 | + animation: jump 0.5s ease-in-out infinite; |
| 14 | + } |
| 15 | + #obstacle { |
| 16 | + width: 20px; |
| 17 | + height: 40px; |
| 18 | + background-color: red; |
| 19 | + position: absolute; |
| 20 | + bottom: 0; |
| 21 | + right: 0; |
| 22 | + } |
| 23 | + @keyframes jump { |
| 24 | + 0%, |
| 25 | + 100% { |
| 26 | + transform: translateY(0); |
| 27 | + } |
| 28 | + 50% { |
| 29 | + transform: translateY(-40px); |
| 30 | + } |
| 31 | + } |
| 32 | + </style> |
| 33 | + </head> |
| 34 | + <body> |
| 35 | + <div id="dino"></div> |
| 36 | + <div id="obstacle"></div> |
| 37 | + <script> |
| 38 | + const dino = document.getElementById("dino"); |
| 39 | + const obstacle = document.getElementById("obstacle"); |
| 40 | + let score = 0; |
| 41 | + let isJumping = false; |
| 42 | + |
| 43 | + document.addEventListener("keydown", jump); |
| 44 | + |
| 45 | + function jump(event) { |
| 46 | + if (event.keyCode === 32 && !isJumping) { |
| 47 | + isJumping = true; |
| 48 | + let jumpUp = 0; |
| 49 | + let jumpDown = 0; |
| 50 | + let jumpInterval = setInterval(() => { |
| 51 | + jumpUp += 20; |
| 52 | + dino.style.bottom = jumpUp + "px"; |
| 53 | + if (jumpUp >= 200) { |
| 54 | + clearInterval(jumpInterval); |
| 55 | + let jumpDownInterval = setInterval(() => { |
| 56 | + jumpUp -= 20; |
| 57 | + dino.style.bottom = jumpUp + "px"; |
| 58 | + if (jumpUp === 0) { |
| 59 | + clearInterval(jumpDownInterval); |
| 60 | + isJumping = false; |
| 61 | + } |
| 62 | + }, 30); |
| 63 | + } |
| 64 | + }, 30); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function checkCollision() { |
| 69 | + const dinoPosition = dino.getBoundingClientRect(); |
| 70 | + const obstaclePosition = obstacle.getBoundingClientRect(); |
| 71 | + |
| 72 | + if ( |
| 73 | + dinoPosition.left < obstaclePosition.left + obstaclePosition.width && |
| 74 | + dinoPosition.left + dinoPosition.width > obstaclePosition.left && |
| 75 | + dinoPosition.top < obstaclePosition.top + obstaclePosition.height && |
| 76 | + dinoPosition.top + dinoPosition.height > obstaclePosition.top |
| 77 | + ) { |
| 78 | + alert(`Game Over! Your score: ${score}`); |
| 79 | + location.reload(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + function updateScore() { |
| 84 | + score++; |
| 85 | + if (score % 10 === 0) { |
| 86 | + alert(`Congratulations! Your score: ${score}`); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + function moveObstacle() { |
| 91 | + const obstaclePosition = obstacle.getBoundingClientRect(); |
| 92 | + if (obstaclePosition.right <= 0) { |
| 93 | + obstacle.style.right = "100%"; |
| 94 | + updateScore(); |
| 95 | + } else { |
| 96 | + obstacle.style.right = `${obstaclePosition.right - 5}px`; |
| 97 | + } |
| 98 | + checkCollision(); |
| 99 | + } |
| 100 | + |
| 101 | + setInterval(moveObstacle, 20); |
| 102 | + </script> |
| 103 | + </body> |
| 104 | +</html> |
0 commit comments