ProPerNounpYK commited on
Commit
be3e997
ยท
verified ยท
1 Parent(s): 9623bb6

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +121 -127
index.html CHANGED
@@ -132,145 +132,127 @@
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
- function updateGame() {
163
- ctx.clearRect(0, 0, canvas.width, canvas.height);
164
 
165
- player.direction = mouseX > player.x ? 1 : -1;
166
- player.image.src = player.direction === 1 ? player.imageRight : player.imageLeft;
167
- ctx.drawImage(player.image, player.x, player.y, player.width, player.height);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
- // ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๋ฐœ์‚ฌํ•œ ์ด์•Œ ์ฒ˜๋ฆฌ
170
- bullets.forEach((bullet, index) => {
171
- bullet.x += bullet.speed;
172
- ctx.fillStyle = 'yellow';
173
- ctx.fillRect(bullet.x, bullet.y, 10, 5);
174
- });
175
 
176
- // ์  ์ฒ˜๋ฆฌ
177
- enemies.forEach((enemy, enemyIndex) => {
178
- enemy.x += enemy.speed;
179
- ctx.drawImage(enemy.image, enemy.x, enemy.y, enemy.width, enemy.height);
180
 
181
- // ํ”Œ๋ ˆ์ด์–ด์™€ ์ ์ด ์ถฉ๋Œํ–ˆ์„ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
182
- if (checkCollision(player, enemy)) {
183
- player.health--;
184
- healthFill.style.width = (player.health / 5 * 100) + '%';
185
- player.image.src = player.direction === 1 ? player.imageHitRight : player.imageHitLeft;
186
- enemies.splice(enemyIndex, 1); // ์  ์ œ๊ฑฐ
187
- playerHitSound.play();
188
 
189
- if (player.health <= 0) {
190
- endGame("Game Over! Go back to the past and change the current result!");
191
- }
192
- }
193
 
194
- // ํ”Œ๋ ˆ์ด์–ด์˜ ์ด์•Œ์ด ์ ์„ ๋งž์ท„์„ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
195
- bullets.forEach((bullet, bulletIndex) => {
196
- if (checkCollision(bullet, enemy)) {
197
- // ์ด์•Œ์ด ์ ์„ ๋งž์ท„์„ ๋•Œ ์ฒ˜๋ฆฌ
198
- enemy.image.src = enemy.speed > 0 ? enemy.imageHitRight : enemy.imageHitLeft;
199
- setTimeout(() => {
200
  enemies.splice(enemyIndex, 1); // ์  ์ œ๊ฑฐ
201
- bullets.splice(bulletIndex, 1); // ์ด์•Œ ์ œ๊ฑฐ
202
- enemiesRemaining--;
203
- enemyCount.textContent = enemiesRemaining;
204
- }, 100);
205
- enemyHitSound.play();
206
 
207
- if (enemiesRemaining <= 0) {
208
- endGame("You have successfully protected New York. Return to the time machine.");
 
209
  }
210
- }
211
- });
212
 
213
- // ์ ์˜ ์ด์•Œ ๋ฐœ์‚ฌ
214
- enemyShoot(enemy);
215
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
 
217
- // ์ ์˜ ์ด์•Œ ์ฒ˜๋ฆฌ
218
- enemyBullets.forEach((bullet, index) => {
219
- bullet.x += bullet.speed;
220
- ctx.fillStyle = 'orange';
221
- ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
222
 
223
- // ์ด์•Œ์ด ํ”Œ๋ ˆ์ด์–ด์™€ ์ถฉ๋Œํ–ˆ์„ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
224
- if (
225
- bullet.x < player.x + player.width - 30 &&
226
- bullet.x + bullet.width > player.x + 30 &&
227
- bullet.y < player.y + player.height - 30 &&
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
- if (player.health <= 0) {
237
- endGame("Game Over! Go back to the past and change the current result!");
238
- }
239
- }
240
- });
 
 
 
 
 
 
 
241
 
242
- // ํ™”๋ฉด ๋ฐ–์œผ๋กœ ๋‚˜๊ฐ„ ์ด์•Œ ์ œ๊ฑฐ
243
- bullets = bullets.filter(bullet =>
244
- bullet.x >= 0 && bullet.x <= canvas.width
245
- );
246
- enemyBullets = enemyBullets.filter(bullet =>
247
- bullet.x >= 0 && bullet.x <= canvas.width
248
- );
249
 
250
- // ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜์ง€ ์•Š์œผ๋ฉด ๊ณ„์† ์—…๋ฐ์ดํŠธ
251
- if (!gameOver) {
252
- requestAnimationFrame(updateGame);
253
- }
254
- }
255
- setTimeout(() => {
256
- message.textContent = "Click on the character";
257
- setTimeout(() => {
258
- message.textContent = "You can move your character using the arrow keys.";
259
- setTimeout(() => {
260
- message.textContent = "The character's direction follows the mouse cursor.";
261
- setTimeout(() => {
262
- message.textContent = "Press the space bar to shoot a bullet.";
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
- return rect1.x < rect2.x + rect2.width &&
283
- rect1.x + rect1.width > rect2.x &&
284
- rect1.y < rect2.y + rect2.height &&
285
- rect1.y + rect1.height > rect2.y;
 
 
 
 
 
 
 
 
 
 
 
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) {