Spaces:
Running
Running
Update index.html
Browse files- index.html +121 -127
index.html
CHANGED
|
@@ -132,145 +132,127 @@
|
|
| 132 |
let gameOver = false;
|
| 133 |
|
| 134 |
function spawnEnemy() {
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
if (enemies.length >= 10) { // ๋์์ ์กด์ฌํ ์ ์๋ ์ ์ ์ ์ ํ
|
| 140 |
-
return;
|
| 141 |
-
}
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
y: Math.random() * (canvas.height - 150),
|
| 147 |
-
width: 250,
|
| 148 |
-
height: 250,
|
| 149 |
-
speed: side === 0 ? 1 : -1,
|
| 150 |
-
image: new Image(),
|
| 151 |
-
imageLeft: 'Renovators Default Right.png',
|
| 152 |
-
imageRight: 'RenovatorsDefaultLeft.png',
|
| 153 |
-
imageShootLeft: 'RenovatorsgunLeft.png',
|
| 154 |
-
imageShootRight: 'RenovatorsGunRight.png',
|
| 155 |
-
imageHitLeft: 'RenovatorsExplodeLeft.png',
|
| 156 |
-
imageHitRight: 'RenovatorsExplodeRight.png',
|
| 157 |
-
lastShootTime: Date.now()
|
| 158 |
-
};
|
| 159 |
-
enemy.image.src = side === 0 ? enemy.imageLeft : enemy.imageRight;
|
| 160 |
-
enemies.push(enemy);
|
| 161 |
-
}
|
| 162 |
-
function updateGame() {
|
| 163 |
-
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
bullet.x += bullet.speed;
|
| 172 |
-
ctx.fillStyle = 'yellow';
|
| 173 |
-
ctx.fillRect(bullet.x, bullet.y, 10, 5);
|
| 174 |
-
});
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
ctx.drawImage(enemy.image, enemy.x, enemy.y, enemy.width, enemy.height);
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
playerHitSound.play();
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
setTimeout(() => {
|
| 200 |
enemies.splice(enemyIndex, 1); // ์ ์ ๊ฑฐ
|
| 201 |
-
|
| 202 |
-
enemiesRemaining--;
|
| 203 |
-
enemyCount.textContent = enemiesRemaining;
|
| 204 |
-
}, 100);
|
| 205 |
-
enemyHitSound.play();
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
|
|
|
| 209 |
}
|
| 210 |
-
}
|
| 211 |
-
});
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
ctx.fillStyle = 'orange';
|
| 221 |
-
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
bullet.y + bullet.height > player.y + 30
|
| 229 |
-
) {
|
| 230 |
-
player.health--;
|
| 231 |
-
healthFill.style.width = (player.health / 5 * 100) + '%';
|
| 232 |
-
player.image.src = player.direction === 1 ? player.imageHitRight : player.imageHitLeft;
|
| 233 |
-
enemyBullets.splice(index, 1); // ์ด์ ์ ๊ฑฐ
|
| 234 |
-
playerHitSound.play();
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
bullet.x >= 0 && bullet.x <= canvas.width
|
| 248 |
-
);
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
setTimeout(() => {
|
| 264 |
-
message.textContent = "Your mission is to defeat the Renovators who have attacked New York. Defeat them all and save New York in 2030!";
|
| 265 |
-
setTimeout(() => {
|
| 266 |
-
message.textContent = "";
|
| 267 |
-
gameStarted = true;
|
| 268 |
-
}, 5000);
|
| 269 |
-
}, 3000);
|
| 270 |
-
}, 4000);
|
| 271 |
-
}, 4000);
|
| 272 |
-
}, 4000);
|
| 273 |
-
}, 0);
|
| 274 |
|
| 275 |
function endGame(messageText) {
|
| 276 |
gameOver = true;
|
|
@@ -278,11 +260,23 @@
|
|
| 278 |
replayButton.style.display = 'block';
|
| 279 |
}
|
| 280 |
|
|
|
|
| 281 |
function checkCollision(rect1, rect2) {
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
}
|
| 287 |
|
| 288 |
function enemyShoot(enemy) {
|
|
|
|
| 132 |
let gameOver = false;
|
| 133 |
|
| 134 |
function spawnEnemy() {
|
| 135 |
+
if (!gameStarted || gameOver) {
|
| 136 |
+
return;
|
| 137 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
if (enemies.length >= 10) { // ๋์์ ์กด์ฌํ ์ ์๋ ์ ์ ์ ์ ํ
|
| 140 |
+
return;
|
| 141 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
const side = Math.random() < 0.5 ? 0 : canvas.width;
|
| 144 |
+
const enemy = {
|
| 145 |
+
x: side,
|
| 146 |
+
y: Math.random() * (canvas.height - 150),
|
| 147 |
+
width: 250,
|
| 148 |
+
height: 250,
|
| 149 |
+
speed: side === 0 ? 1 : -1,
|
| 150 |
+
image: new Image(),
|
| 151 |
+
imageLeft: 'Renovators Default Right.png',
|
| 152 |
+
imageRight: 'RenovatorsDefaultLeft.png',
|
| 153 |
+
imageShootLeft: 'RenovatorsgunLeft.png',
|
| 154 |
+
imageShootRight: 'RenovatorsGunRight.png',
|
| 155 |
+
imageHitLeft: 'RenovatorsExplodeLeft.png',
|
| 156 |
+
imageHitRight: 'RenovatorsExplodeRight.png',
|
| 157 |
+
lastShootTime: Date.now()
|
| 158 |
+
};
|
| 159 |
+
enemy.image.src = side === 0 ? enemy.imageLeft : enemy.imageRight;
|
| 160 |
+
enemies.push(enemy);
|
| 161 |
+
}
|
| 162 |
|
| 163 |
+
function updateGame() {
|
| 164 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
+
player.direction = mouseX > player.x ? 1 : -1;
|
| 167 |
+
player.image.src = player.direction === 1 ? player.imageRight : player.imageLeft;
|
| 168 |
+
ctx.drawImage(player.image, player.x, player.y, player.width, player.height);
|
|
|
|
| 169 |
|
| 170 |
+
// ํ๋ ์ด์ด๊ฐ ๋ฐ์ฌํ ์ด์ ์ฒ๋ฆฌ
|
| 171 |
+
bullets.forEach((bullet, index) => {
|
| 172 |
+
bullet.x += bullet.speed;
|
| 173 |
+
ctx.fillStyle = 'yellow';
|
| 174 |
+
ctx.fillRect(bullet.x, bullet.y, 10, 5);
|
| 175 |
+
});
|
|
|
|
| 176 |
|
| 177 |
+
// ์ ์ฒ๋ฆฌ
|
| 178 |
+
enemies.forEach((enemy, enemyIndex) => {
|
| 179 |
+
enemy.x += enemy.speed;
|
| 180 |
+
ctx.drawImage(enemy.image, enemy.x, enemy.y, enemy.width, enemy.height);
|
| 181 |
|
| 182 |
+
// ํ๋ ์ด์ด์ ์ ์ด ์ถฉ๋ํ์ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 183 |
+
if (checkCollision(player, enemy)) {
|
| 184 |
+
player.health--;
|
| 185 |
+
healthFill.style.width = (player.health / 5 * 100) + '%';
|
| 186 |
+
player.image.src = player.direction === 1 ? player.imageHitRight : player.imageHitLeft;
|
|
|
|
| 187 |
enemies.splice(enemyIndex, 1); // ์ ์ ๊ฑฐ
|
| 188 |
+
playerHitSound.play();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
if (player.health <= 0) {
|
| 191 |
+
endGame("Game Over! Go back to the past and change the current result!");
|
| 192 |
+
}
|
| 193 |
}
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
// ํ๋ ์ด์ด์ ์ด์์ด ์ ์ ๋ง์ท์ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 196 |
+
bullets.forEach((bullet, bulletIndex) => {
|
| 197 |
+
if (checkCollision(bullet, enemy)) {
|
| 198 |
+
// ์ด์์ด ์ ์ ๋ง์ท์ ๋ ์ฒ๋ฆฌ
|
| 199 |
+
enemy.image.src = enemy.speed > 0 ? enemy.imageHitRight : enemy.imageHitLeft;
|
| 200 |
+
setTimeout(() => {
|
| 201 |
+
enemies.splice(enemyIndex, 1); // ์ ์ ๊ฑฐ
|
| 202 |
+
bullets.splice(bulletIndex, 1); // ์ด์ ์ ๊ฑฐ
|
| 203 |
+
enemiesRemaining--;
|
| 204 |
+
enemyCount.textContent = enemiesRemaining;
|
| 205 |
+
}, 100);
|
| 206 |
+
enemyHitSound.play();
|
| 207 |
+
|
| 208 |
+
if (enemiesRemaining <= 0) {
|
| 209 |
+
endGame("You have successfully protected New York. Return to the time machine.");
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
});
|
| 213 |
|
| 214 |
+
// ์ ์ ์ด์ ๋ฐ์ฌ
|
| 215 |
+
enemyShoot(enemy);
|
| 216 |
+
});
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
// ์ ์ ์ด์ ์ฒ๋ฆฌ
|
| 219 |
+
enemyBullets.forEach((bullet, index) => {
|
| 220 |
+
bullet.x += bullet.speed;
|
| 221 |
+
ctx.fillStyle = 'orange';
|
| 222 |
+
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
// ์ด์์ด ํ๋ ์ด์ด์ ์ถฉ๋ํ์ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 225 |
+
if (
|
| 226 |
+
bullet.x < player.x + player.width - 30 &&
|
| 227 |
+
bullet.x + bullet.width > player.x + 30 &&
|
| 228 |
+
bullet.y < player.y + player.height - 30 &&
|
| 229 |
+
bullet.y + bullet.height > player.y + 30
|
| 230 |
+
) {
|
| 231 |
+
player.health--;
|
| 232 |
+
healthFill.style.width = (player.health / 5 * 100) + '%';
|
| 233 |
+
player.image.src = player.direction === 1 ? player.imageHitRight : player.imageHitLeft;
|
| 234 |
+
enemyBullets.splice(index, 1); // ์ด์ ์ ๊ฑฐ
|
| 235 |
+
playerHitSound.play();
|
| 236 |
|
| 237 |
+
if (player.health <= 0) {
|
| 238 |
+
endGame("Game Over! Go back to the past and change the current result!");
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
+
});
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
// ํ๋ฉด ๋ฐ์ผ๋ก ๋๊ฐ ์ด์ ์ ๊ฑฐ
|
| 244 |
+
bullets = bullets.filter(bullet =>
|
| 245 |
+
bullet.x >= 0 && bullet.x <= canvas.width
|
| 246 |
+
);
|
| 247 |
+
enemyBullets = enemyBullets.filter(bullet =>
|
| 248 |
+
bullet.x >= 0 && bullet.x <= canvas.width
|
| 249 |
+
);
|
| 250 |
+
|
| 251 |
+
// ๊ฒ์์ด ์ข
๋ฃ๋์ง ์์ผ๋ฉด ๊ณ์ ์
๋ฐ์ดํธ
|
| 252 |
+
if (!gameOver) {
|
| 253 |
+
requestAnimationFrame(updateGame);
|
| 254 |
+
}
|
| 255 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
function endGame(messageText) {
|
| 258 |
gameOver = true;
|
|
|
|
| 260 |
replayButton.style.display = 'block';
|
| 261 |
}
|
| 262 |
|
| 263 |
+
// ์ด์๊ณผ ์บ๋ฆญํฐ์ ์ถฉ๋ ๊ฒ์ฌ (์ด๋ฏธ์ง ํฌ๊ธฐ ๊ธฐ์ค)
|
| 264 |
function checkCollision(rect1, rect2) {
|
| 265 |
+
// ์ด์(rect1)๊ณผ ์บ๋ฆญํฐ(rect2)์ ์ค์ ์์ญ์ ๋น๊ต
|
| 266 |
+
const bulletLeft = rect1.x;
|
| 267 |
+
const bulletRight = rect1.x + rect1.width;
|
| 268 |
+
const bulletTop = rect1.y;
|
| 269 |
+
const bulletBottom = rect1.y + rect1.height;
|
| 270 |
+
|
| 271 |
+
// ์บ๋ฆญํฐ์ ๋ฒ์ (์บ๋ฆญํฐ ์ด๋ฏธ์ง ํฌ๊ธฐ ๊ธฐ์ค)
|
| 272 |
+
const playerLeft = rect2.x + 30; // ์บ๋ฆญํฐ ์ด๋ฏธ์ง ์ข์ธก
|
| 273 |
+
const playerRight = rect2.x + rect2.width - 30; // ์บ๋ฆญํฐ ์ด๋ฏธ์ง ์ฐ์ธก
|
| 274 |
+
const playerTop = rect2.y + 30; // ์บ๋ฆญํฐ ์ด๋ฏธ์ง ์๋จ
|
| 275 |
+
const playerBottom = rect2.y + rect2.height - 30; // ์บ๋ฆญํฐ ์ด๋ฏธ์ง ํ๋จ
|
| 276 |
+
|
| 277 |
+
// ์ถฉ๋ ์ฒดํฌ
|
| 278 |
+
return bulletRight > playerLeft && bulletLeft < playerRight &&
|
| 279 |
+
bulletBottom > playerTop && bulletTop < playerBottom;
|
| 280 |
}
|
| 281 |
|
| 282 |
function enemyShoot(enemy) {
|