|
| 1 | +const cloudsElement = document.querySelector('.clouds'); |
| 2 | +const roadElement = document.querySelector('.road'); |
| 3 | +const scoreElement = document.querySelector('.score-value'); |
| 4 | + |
| 5 | +function getRandomNumber(min, max) { |
| 6 | + return min + Math.floor(Math.random() * (max - min)); |
| 7 | +} |
| 8 | + |
| 9 | +const world = { |
| 10 | + gameOver: false, |
| 11 | + updateMs: 40, |
| 12 | + score: 0, |
| 13 | + objects: { |
| 14 | + clouds: [], |
| 15 | + roadLines: [], |
| 16 | + foods: [], |
| 17 | + dino: { |
| 18 | + isJumping: false, |
| 19 | + jumpTicks: 0, |
| 20 | + location: { |
| 21 | + y: -40, |
| 22 | + }, |
| 23 | + element: document.querySelector('.dino'), |
| 24 | + }, |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +function init() { |
| 29 | + let currentCloud = 1; |
| 30 | + const maxClouds = 4; |
| 31 | + for (let i = 0; i < 20; i += 1) { |
| 32 | + const cloudImage = document.createElement('img'); |
| 33 | + cloudImage.src = `cloud-${currentCloud}.png`; |
| 34 | + cloudImage.classList.add('cloud'); |
| 35 | + const cloud = { |
| 36 | + element: cloudImage, |
| 37 | + velocity: getRandomNumber(1, 3), |
| 38 | + scale: getRandomNumber(0.7, 1.4), |
| 39 | + location: { |
| 40 | + x: getRandomNumber(-20, 100), |
| 41 | + y: getRandomNumber(-20, 60), |
| 42 | + }, |
| 43 | + }; |
| 44 | + cloudImage.style.transform = `scale(${cloud.scale})`; |
| 45 | + world.objects.clouds.push(cloud); |
| 46 | + cloudImage.style.opacity = cloud.location.y + '%'; |
| 47 | + cloudImage.style.top = cloud.location.y + '%'; |
| 48 | + cloudImage.style.left = cloud.location.x + '%'; |
| 49 | + cloudsElement.append(cloudImage); |
| 50 | + currentCloud += 1; |
| 51 | + if (currentCloud > maxClouds) { |
| 52 | + currentCloud = 1; |
| 53 | + } |
| 54 | + } |
| 55 | + for (let i = 0; i < 4; i += 1) { |
| 56 | + const roadLineElement = document.createElement('div'); |
| 57 | + roadLineElement.classList.add('road-line'); |
| 58 | + const roadLine = { |
| 59 | + element: roadLineElement, |
| 60 | + velocity: 1.5, |
| 61 | + location: { |
| 62 | + y: 40, |
| 63 | + x: (i * 20) + (20 * i), |
| 64 | + }, |
| 65 | + }; |
| 66 | + world.objects.roadLines.push(roadLine); |
| 67 | + roadLine.element.style.top = roadLine.location.y + '%'; |
| 68 | + roadLine.element.style.left = roadLine.location.x + '%'; |
| 69 | + roadElement.append(roadLineElement); |
| 70 | + } |
| 71 | + for (let i = 0; i < 5; i++) { |
| 72 | + const foodImage = document.createElement('img'); |
| 73 | + foodImage.classList.add('food'); |
| 74 | + foodImage.src = Math.random() > 0.5 ? 'waffle.png' : 'cheesecake.png'; |
| 75 | + foodImage.style.position = 'absolute'; |
| 76 | + const food = { |
| 77 | + element: foodImage, |
| 78 | + velocity: 1.5, |
| 79 | + location: { |
| 80 | + y: 5, |
| 81 | + x: 100 + (i * 100) + getRandomNumber(0, 50), |
| 82 | + }, |
| 83 | + }; |
| 84 | + food.element.style.top = food.location.y + '%'; |
| 85 | + food.element.style.left = food.location.x + '%'; |
| 86 | + roadElement.append(foodImage); |
| 87 | + world.objects.foods.push(food); |
| 88 | + } |
| 89 | + |
| 90 | + draw(); |
| 91 | +} |
| 92 | + |
| 93 | +function draw() { |
| 94 | + world.objects.clouds.forEach((cloud) => { |
| 95 | + cloud.element.style.opacity = cloud.location.y + '%'; |
| 96 | + cloud.element.style.left = cloud.location.x + '%'; |
| 97 | + cloud.element.style.transform = `scale(${cloud.scale})`; |
| 98 | + }); |
| 99 | + world.objects.roadLines.forEach((roadLine) => { |
| 100 | + roadLine.element.style.left = roadLine.location.x + '%'; |
| 101 | + }); |
| 102 | + world.objects.foods.forEach((food) => { |
| 103 | + food.element.style.left = food.location.x + '%'; |
| 104 | + }); |
| 105 | + if (world.objects.dino.isJumping && !world.objects.dino.element.src.endsWith('dino/jump.gif')) { |
| 106 | + world.objects.dino.element.src = 'dino/jump.gif'; |
| 107 | + } else if (!world.objects.dino.isJumping && !world.objects.dino.element.src.endsWith('dino/walk.gif')) { |
| 108 | + world.objects.dino.element.src = 'dino/walk.gif'; |
| 109 | + } |
| 110 | + world.objects.dino.element.style.top = world.objects.dino.location.y + '%'; |
| 111 | + |
| 112 | + if (!world.gameOver) { |
| 113 | + setTimeout(update, world.updateMs); |
| 114 | + } else { |
| 115 | + world.objects.dino.element.src = 'dino/jump.gif'; |
| 116 | + world.objects.dino.element.style.transform = 'rotate(-90deg)'; |
| 117 | + } |
| 118 | + |
| 119 | + scoreElement.textContent = world.score; |
| 120 | +} |
| 121 | + |
| 122 | +function update() { |
| 123 | + const dinoRect = world.objects.dino.element.getBoundingClientRect(); |
| 124 | + const cloudsRect = cloudsElement.getBoundingClientRect(); |
| 125 | + const roadRect = roadElement.getBoundingClientRect(); |
| 126 | + world.objects.clouds.forEach((cloud) => { |
| 127 | + const cloudRect = cloud.element.getBoundingClientRect(); |
| 128 | + if ((cloudRect.left + cloudRect.width) - 20 <= cloudsRect.left) { |
| 129 | + cloud.location.x = getRandomNumber(100, 120); |
| 130 | + cloud.location.y = getRandomNumber(-20, 60); |
| 131 | + cloud.scale = getRandomNumber(0.7, 1.4); |
| 132 | + } else { |
| 133 | + cloud.location.x -= cloud.velocity; |
| 134 | + } |
| 135 | + }); |
| 136 | + world.objects.roadLines.forEach((roadLine) => { |
| 137 | + const roadLineRect = roadLine.element.getBoundingClientRect(); |
| 138 | + if ((roadLineRect.left + roadLineRect.width) - 10 <= roadRect.left) { |
| 139 | + roadLine.location.x = 100; |
| 140 | + } else { |
| 141 | + roadLine.location.x -= roadLine.velocity; |
| 142 | + } |
| 143 | + }); |
| 144 | + world.objects.foods.forEach((food, i) => { |
| 145 | + const foodRect = food.element.getBoundingClientRect(); |
| 146 | + if (foodRect.left <= dinoRect.right - 80 |
| 147 | + && dinoRect.bottom - 80 > foodRect.top |
| 148 | + && foodRect.left >= dinoRect.left + 20) { |
| 149 | + world.gameOver = true; |
| 150 | + } |
| 151 | + if ((foodRect.left + foodRect.width) <= roadRect.left) { |
| 152 | + const farthestFoodX = Math.max(...world.objects.foods.map((food) => food.location.x)); |
| 153 | + food.location.x = farthestFoodX + 100 + getRandomNumber(0, 50); |
| 154 | + world.score += 1; |
| 155 | + } else { |
| 156 | + food.location.x -= food.velocity; |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + if (world.objects.dino.isJumping) { |
| 161 | + if (world.objects.dino.jumpTicks <= 12) { |
| 162 | + world.objects.dino.location.y -= 9; |
| 163 | + } else if (world.objects.dino.jumpTicks >= 16) { |
| 164 | + world.objects.dino.location.y += 9; |
| 165 | + } |
| 166 | + world.objects.dino.jumpTicks += 1; |
| 167 | + if (world.objects.dino.jumpTicks === 28) { |
| 168 | + world.objects.dino.isJumping = false; |
| 169 | + world.objects.dino.location.y = -40; |
| 170 | + world.objects.dino.jumpTicks = 0; |
| 171 | + world.updateMs *= 0.95; |
| 172 | + } |
| 173 | + } |
| 174 | + draw(); |
| 175 | +} |
| 176 | + |
| 177 | +init(); |
| 178 | + |
| 179 | +document.addEventListener('keydown', (event) => { |
| 180 | + if (event.code === 'ArrowUp') { |
| 181 | + world.objects.dino.isJumping = true; |
| 182 | + } |
| 183 | +}); |
| 184 | + |
| 185 | +document.addEventListener('click', () => { |
| 186 | + world.objects.dino.isJumping = true; |
| 187 | +}); |
0 commit comments