cutechicken commited on
Commit
10242f9
·
verified ·
1 Parent(s): 895f9d2

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +73 -142
index.html CHANGED
@@ -112,7 +112,6 @@
112
  const bossButton = document.getElementById('bossButton');
113
  canvas.width = window.innerWidth;
114
  canvas.height = window.innerHeight;
115
-
116
  // Game state
117
  let currentRound = 1;
118
  let gameOver = false;
@@ -126,7 +125,6 @@
126
  let autoFire = false;
127
  let gold = 0;
128
  let isBossStage = false;
129
-
130
  // Load assets
131
  const backgroundImg = new Image();
132
  backgroundImg.src = 'city.png';
@@ -134,7 +132,6 @@
134
  playerImg.src = 'player.png';
135
  const enemyImg = new Image();
136
  enemyImg.src = 'enemy.png';
137
-
138
  // Audio setup
139
  const cannonSound = new Audio('firemn.ogg');
140
  const machinegunSound = new Audio('firemg.ogg');
@@ -143,7 +140,6 @@
143
  const countSound = new Audio('count.ogg');
144
  bgm.loop = true;
145
  enemyFireSound.volume = 0.5;
146
-
147
  const weapons = {
148
  cannon: {
149
  fireRate: 1000,
@@ -158,7 +154,6 @@
158
  sound: machinegunSound
159
  }
160
  };
161
-
162
  // Player setup
163
  const player = {
164
  x: canvas.width/2,
@@ -170,7 +165,6 @@
170
  health: 1000,
171
  maxHealth: 1000
172
  };
173
-
174
  function startCountdown() {
175
  isCountingDown = true;
176
  countdownTime = 3;
@@ -189,111 +183,72 @@
189
  countdownEl.textContent = countdownTime > 0 ? countdownTime : 'GO!';
190
  }, 1000);
191
  }
192
-
193
- class Enemy {
194
- constructor(isBoss = false) {
195
- this.x = Math.random() * canvas.width;
196
- this.y = Math.random() * canvas.height;
197
- this.health = isBoss ? 5000 : 1000;
198
- this.maxHealth = this.health;
199
- this.speed = isBoss ? 1 : 2;
200
- this.lastShot = 0;
201
- this.shootInterval = isBoss ? 1000 : 1000;
202
- this.angle = 0;
203
- this.width = 100;
204
- this.height = 45;
205
- this.moveTimer = 0;
206
- this.moveInterval = Math.random() * 2000 + 1000;
207
- this.moveAngle = Math.random() * Math.PI * 2;
208
- this.isBoss = isBoss;
209
- this.lastMachineGunShot = 0; // 기관총 발사 시점 추가
210
- if (isBoss) {
211
- this.enemyImg = new Image();
212
- this.enemyImg.src = 'boss.png';
213
- } else if (currentRound >= 7) {
214
- this.enemyImg = new Image();
215
- this.enemyImg.src = 'enemy3.png';
216
- } else if (currentRound >= 4) {
217
- this.enemyImg = new Image();
218
- this.enemyImg.src = 'enemy2.png';
219
- }
220
- }
221
-
222
- update() {
223
- if (isCountingDown) return;
224
- const now = Date.now();
225
-
226
- if (now - this.moveTimer > this.moveInterval) {
227
- this.moveAngle = Math.random() * Math.PI * 2;
228
- this.moveTimer = now;
229
- }
230
- this.x += Math.cos(this.moveAngle) * this.speed;
231
- this.y += Math.sin(this.moveAngle) * this.speed;
232
- this.x = Math.max(this.width / 2, Math.min(canvas.width - this.width / 2, this.x));
233
- this.y = Math.max(this.height / 2, Math.min(canvas.height - this.height / 2, this.y));
234
- this.angle = Math.atan2(player.y - this.y, player.x - this.x);
235
-
236
- if (now - this.lastShot > this.shootInterval && !isCountingDown) {
237
- this.shoot();
238
- this.lastShot = now;
239
- }
240
- }
241
-
242
- shoot() {
243
- const now = Date.now();
244
-
245
- // 기관총 발사 조건
246
- if (this.isBoss && now - this.lastMachineGunShot > 3000) { // 3초 간격
247
- this.lastMachineGunShot = now;
248
- this.fireMachineGun();
249
- } else {
250
- this.fireCannon();
251
- }
252
- }
253
-
254
- fireMachineGun() {
255
- const sound = machinegunSound;
256
- sound.play();
257
-
258
- for (let i = -2; i <= 2; i++) {
259
- const spreadAngle = this.angle + i * 0.1;
260
- bullets.push({
261
- x: this.x + Math.cos(spreadAngle) * 30,
262
- y: this.y + Math.sin(spreadAngle) * 30,
263
- angle: spreadAngle,
264
- speed: 8,
265
- isEnemy: true,
266
- size: 2,
267
- damage: 50,
268
- });
269
  }
270
- }
271
-
272
- fireCannon() {
273
- const sound = this.isBoss ? cannonSound : enemyFireSound.cloneNode();
274
- sound.play();
275
-
276
- bullets.push({
277
- x: this.x + Math.cos(this.angle) * 30,
278
- y: this.y + Math.sin(this.angle) * 30,
279
- angle: this.angle,
280
- speed: 5,
281
- isEnemy: true,
282
- size: this.isBoss ? 5 : 3,
283
- damage: this.isBoss ? 300 : 150,
284
- });
285
- }
286
- }
287
  function showShop() {
288
  document.getElementById('shop').style.display = 'block';
289
  }
290
-
291
  // 플레이어의 기본 상태를 저장
292
  const defaultPlayerStats = {
293
  maxHealth: 1000,
294
- speed: 5,
295
- width: 100,
296
- height: 45
297
  };
298
  function buyTank(tankImg, cost, tankId) {
299
  if (gold >= cost) {
@@ -301,24 +256,21 @@ function buyTank(tankImg, cost, tankId) {
301
  playerImg.src = tankImg;
302
  document.getElementById(tankId).style.display = 'none';
303
  document.getElementById('shop').style.display = 'none';
304
-
305
- if (tankId === 'tank1') { // PZ.IV
306
- player.maxHealth = 1500;
307
- player.speed = defaultPlayerStats.speed; // 기본 이동속도로 복구
308
- player.width = 100; // PZ.IV 크기 설정
309
- player.height = 55; // PZ.IV의 크기 설정
310
- }
311
- else if (tankId === 'tank2') { // TIGER
312
- player.maxHealth = 2000;
313
- player.speed = defaultPlayerStats.speed * 0.7;
314
- player.width = 100; // TIGER는 기본 크기 유지
315
- player.height = 45; // TIGER는 기본 크기 유지
316
  }
317
-
 
 
 
 
318
  player.health = player.maxHealth;
319
  }
320
  }
321
-
322
  function initRound() {
323
  enemies = [];
324
  for(let i = 0; i < 1 * currentRound; i++) {
@@ -329,7 +281,6 @@ function buyTank(tankImg, cost, tankId) {
329
  items = [];
330
  startCountdown();
331
  }
332
-
333
  function startBossStage() {
334
  isBossStage = true;
335
  enemies = [];
@@ -340,24 +291,21 @@ function buyTank(tankImg, cost, tankId) {
340
  document.getElementById('bossButton').style.display = 'none';
341
  startCountdown();
342
  }
343
-
344
  canvas.addEventListener('mousemove', (e) => {
345
  player.angle = Math.atan2(e.clientY - player.y, e.clientX - player.x);
346
  });
347
-
348
  const keys = {};
349
  document.addEventListener('keydown', e => {
350
  keys[e.key] = true;
351
  if(e.key.toLowerCase() === 'c') {
352
  currentWeapon = currentWeapon === 'cannon' ? 'machinegun' : 'cannon';
353
- weaponInfo.textContent = Current Weapon: ${currentWeapon.charAt(0).toUpperCase() + currentWeapon.slice(1)};
354
  } else if(e.key.toLowerCase() === 'r') {
355
  autoFire = !autoFire;
356
  }
357
  });
358
 
359
  document.addEventListener('keyup', e => keys[e.key] = false);
360
-
361
  function fireBullet() {
362
  if(isCountingDown) return;
363
  const weapon = weapons[currentWeapon];
@@ -376,7 +324,6 @@ function buyTank(tankImg, cost, tankId) {
376
  lastShot = now;
377
  }
378
  }
379
-
380
  function updateGame() {
381
  if(gameOver) return;
382
  if(!isCountingDown) {
@@ -388,9 +335,7 @@ function buyTank(tankImg, cost, tankId) {
388
  player.y = Math.max(player.height/2, Math.min(canvas.height - player.height/2, player.y));
389
  fireBullet();
390
  }
391
-
392
  enemies.forEach(enemy => enemy.update());
393
-
394
  if(!isCountingDown) {
395
  bullets = bullets.filter(bullet => {
396
  bullet.x += Math.cos(bullet.angle) * bullet.speed;
@@ -423,7 +368,6 @@ function buyTank(tankImg, cost, tankId) {
423
  return bullet.x >= 0 && bullet.x <= canvas.width &&
424
  bullet.y >= 0 && bullet.y <= canvas.height;
425
  });
426
-
427
  items = items.filter(item => {
428
  const dist = Math.hypot(item.x - player.x, item.y - player.y);
429
  if(dist < 30) {
@@ -453,30 +397,25 @@ function buyTank(tankImg, cost, tankId) {
453
  function spawnHealthItem(x, y) {
454
  items.push({x, y});
455
  }
456
-
457
  function drawHealthBar(x, y, health, maxHealth, width, height, color) {
458
  ctx.fillStyle = '#333';
459
  ctx.fillRect(x - width/2, y - height/2, width, height);
460
  ctx.fillStyle = color;
461
  ctx.fillRect(x - width/2, y - height/2, width * (health/maxHealth), height);
462
  }
463
-
464
  function drawGame() {
465
  ctx.clearRect(0, 0, canvas.width, canvas.height);
466
  const pattern = ctx.createPattern(backgroundImg, 'repeat');
467
  ctx.fillStyle = pattern;
468
  ctx.fillRect(0, 0, canvas.width, canvas.height);
469
-
470
  // 플레이어 그리기
471
  ctx.save();
472
  ctx.translate(player.x, player.y);
473
  ctx.rotate(player.angle);
474
  ctx.drawImage(playerImg, -player.width/2, -player.height/2, player.width, player.height);
475
  ctx.restore();
476
-
477
  // 체력바
478
  drawHealthBar(canvas.width/2, 30, player.health, player.maxHealth, 200, 20, 'green');
479
-
480
  // 적 그리기
481
  enemies.forEach(enemy => {
482
  ctx.save();
@@ -487,7 +426,6 @@ function buyTank(tankImg, cost, tankId) {
487
  ctx.restore();
488
  drawHealthBar(enemy.x, enemy.y - 40, enemy.health, enemy.maxHealth, 60, 5, 'red');
489
  });
490
-
491
  // 총알 그리기
492
  bullets.forEach(bullet => {
493
  ctx.beginPath();
@@ -495,7 +433,6 @@ function buyTank(tankImg, cost, tankId) {
495
  ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
496
  ctx.fill();
497
  });
498
-
499
  // 아이템 그리기
500
  items.forEach(item => {
501
  ctx.beginPath();
@@ -503,13 +440,11 @@ function buyTank(tankImg, cost, tankId) {
503
  ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
504
  ctx.fill();
505
  });
506
-
507
  // UI 그리기
508
  ctx.fillStyle = 'white';
509
  ctx.font = '24px Arial';
510
- ctx.fillText(Round ${currentRound}/10, 10, 30);
511
- ctx.fillText(Gold: ${gold}, 10, 60);
512
-
513
  if(isCountingDown) {
514
  ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
515
  ctx.fillRect(0, 0, canvas.width, canvas.height);
@@ -520,16 +455,13 @@ function gameLoop() {
520
  drawGame();
521
  requestAnimationFrame(gameLoop);
522
  }
523
-
524
  nextRoundBtn.addEventListener('click', () => {
525
  currentRound++;
526
  nextRoundBtn.style.display = 'none';
527
  document.getElementById('shop').style.display = 'none'; // 상점 창 닫기
528
  initRound();
529
  });
530
-
531
  bossButton.addEventListener('click', startBossStage);
532
-
533
  restartBtn.addEventListener('click', () => {
534
  currentRound = 1;
535
  gameOver = false;
@@ -552,7 +484,6 @@ restartBtn.addEventListener('click', () => {
552
  gameLoop();
553
  bgm.play(); // 게임 시작시 BGM 재생
554
  });
555
-
556
  window.addEventListener('resize', () => {
557
  canvas.width = window.innerWidth;
558
  canvas.height = window.innerHeight;
 
112
  const bossButton = document.getElementById('bossButton');
113
  canvas.width = window.innerWidth;
114
  canvas.height = window.innerHeight;
 
115
  // Game state
116
  let currentRound = 1;
117
  let gameOver = false;
 
125
  let autoFire = false;
126
  let gold = 0;
127
  let isBossStage = false;
 
128
  // Load assets
129
  const backgroundImg = new Image();
130
  backgroundImg.src = 'city.png';
 
132
  playerImg.src = 'player.png';
133
  const enemyImg = new Image();
134
  enemyImg.src = 'enemy.png';
 
135
  // Audio setup
136
  const cannonSound = new Audio('firemn.ogg');
137
  const machinegunSound = new Audio('firemg.ogg');
 
140
  const countSound = new Audio('count.ogg');
141
  bgm.loop = true;
142
  enemyFireSound.volume = 0.5;
 
143
  const weapons = {
144
  cannon: {
145
  fireRate: 1000,
 
154
  sound: machinegunSound
155
  }
156
  };
 
157
  // Player setup
158
  const player = {
159
  x: canvas.width/2,
 
165
  health: 1000,
166
  maxHealth: 1000
167
  };
 
168
  function startCountdown() {
169
  isCountingDown = true;
170
  countdownTime = 3;
 
183
  countdownEl.textContent = countdownTime > 0 ? countdownTime : 'GO!';
184
  }, 1000);
185
  }
186
+ class Enemy {
187
+ constructor(isBoss = false) {
188
+ this.x = Math.random() * canvas.width;
189
+ this.y = Math.random() * canvas.height;
190
+ this.health = isBoss ? 5000 : 1000;
191
+ this.maxHealth = this.health;
192
+ this.speed = isBoss ? 1 : 2;
193
+ this.lastShot = 0;
194
+ this.shootInterval = isBoss ? 1000 : 1000;
195
+ this.angle = 0;
196
+ this.width = 100;
197
+ this.height = 45;
198
+ this.moveTimer = 0;
199
+ this.moveInterval = Math.random() * 2000 + 1000;
200
+ this.moveAngle = Math.random() * Math.PI * 2;
201
+ this.isBoss = isBoss;
202
+ if (isBoss) {
203
+ this.enemyImg = new Image();
204
+ this.enemyImg.src = 'boss.png';
205
+ } else if (currentRound >= 7) {
206
+ this.enemyImg = new Image();
207
+ this.enemyImg.src = 'enemy3.png';
208
+ } else if (currentRound >= 4) {
209
+ this.enemyImg = new Image();
210
+ this.enemyImg.src = 'enemy2.png';
211
+ }
212
+ }
213
+ update() {
214
+ if(isCountingDown) return;
215
+ const now = Date.now();
216
+
217
+ if (now - this.moveTimer > this.moveInterval) {
218
+ this.moveAngle = Math.random() * Math.PI * 2;
219
+ this.moveTimer = now;
220
+ }
221
+ this.x += Math.cos(this.moveAngle) * this.speed;
222
+ this.y += Math.sin(this.moveAngle) * this.speed;
223
+ this.x = Math.max(this.width/2, Math.min(canvas.width - this.width/2, this.x));
224
+ this.y = Math.max(this.height/2, Math.min(canvas.height - this.height/2, this.y));
225
+ this.angle = Math.atan2(player.y - this.y, player.x - this.x);
226
+
227
+ if (now - this.lastShot > this.shootInterval && !isCountingDown) {
228
+ this.shoot();
229
+ this.lastShot = now;
230
+ }
231
+ }
232
+ shoot() {
233
+ enemyFireSound.cloneNode().play();
234
+ bullets.push({
235
+ x: this.x + Math.cos(this.angle) * 30,
236
+ y: this.y + Math.sin(this.angle) * 30,
237
+ angle: this.angle,
238
+ speed: 5,
239
+ isEnemy: true,
240
+ size: this.isBoss ? 5 : 3,
241
+ damage: this.isBoss ? 200 : 100
242
+ });
243
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  function showShop() {
246
  document.getElementById('shop').style.display = 'block';
247
  }
 
248
  // 플레이어의 기본 상태를 저장
249
  const defaultPlayerStats = {
250
  maxHealth: 1000,
251
+ speed: 5
 
 
252
  };
253
  function buyTank(tankImg, cost, tankId) {
254
  if (gold >= cost) {
 
256
  playerImg.src = tankImg;
257
  document.getElementById(tankId).style.display = 'none';
258
  document.getElementById('shop').style.display = 'none';
259
+ // 먼저 플레이어 스탯을 기본값으로 초기화
260
+ player.maxHealth = defaultPlayerStats.maxHealth;
261
+ player.speed = defaultPlayerStats.speed;
262
+ // 구매한 모든 전차의 효과를 한 번에 적용
263
+ if (document.getElementById('tank1').style.display === 'none') { // PZ.IV 구매 여부
264
+ player.maxHealth += 500;
 
 
 
 
 
 
265
  }
266
+ if (document.getElementById('tank2').style.display === 'none') { // TIGER 구매 여부
267
+ player.maxHealth += 1000;
268
+ player.speed *= 0.7;
269
+ }
270
+ // 현재 체력을 새로운 최대 체력으로 설정
271
  player.health = player.maxHealth;
272
  }
273
  }
 
274
  function initRound() {
275
  enemies = [];
276
  for(let i = 0; i < 1 * currentRound; i++) {
 
281
  items = [];
282
  startCountdown();
283
  }
 
284
  function startBossStage() {
285
  isBossStage = true;
286
  enemies = [];
 
291
  document.getElementById('bossButton').style.display = 'none';
292
  startCountdown();
293
  }
 
294
  canvas.addEventListener('mousemove', (e) => {
295
  player.angle = Math.atan2(e.clientY - player.y, e.clientX - player.x);
296
  });
 
297
  const keys = {};
298
  document.addEventListener('keydown', e => {
299
  keys[e.key] = true;
300
  if(e.key.toLowerCase() === 'c') {
301
  currentWeapon = currentWeapon === 'cannon' ? 'machinegun' : 'cannon';
302
+ weaponInfo.textContent = `Current Weapon: ${currentWeapon.charAt(0).toUpperCase() + currentWeapon.slice(1)}`;
303
  } else if(e.key.toLowerCase() === 'r') {
304
  autoFire = !autoFire;
305
  }
306
  });
307
 
308
  document.addEventListener('keyup', e => keys[e.key] = false);
 
309
  function fireBullet() {
310
  if(isCountingDown) return;
311
  const weapon = weapons[currentWeapon];
 
324
  lastShot = now;
325
  }
326
  }
 
327
  function updateGame() {
328
  if(gameOver) return;
329
  if(!isCountingDown) {
 
335
  player.y = Math.max(player.height/2, Math.min(canvas.height - player.height/2, player.y));
336
  fireBullet();
337
  }
 
338
  enemies.forEach(enemy => enemy.update());
 
339
  if(!isCountingDown) {
340
  bullets = bullets.filter(bullet => {
341
  bullet.x += Math.cos(bullet.angle) * bullet.speed;
 
368
  return bullet.x >= 0 && bullet.x <= canvas.width &&
369
  bullet.y >= 0 && bullet.y <= canvas.height;
370
  });
 
371
  items = items.filter(item => {
372
  const dist = Math.hypot(item.x - player.x, item.y - player.y);
373
  if(dist < 30) {
 
397
  function spawnHealthItem(x, y) {
398
  items.push({x, y});
399
  }
 
400
  function drawHealthBar(x, y, health, maxHealth, width, height, color) {
401
  ctx.fillStyle = '#333';
402
  ctx.fillRect(x - width/2, y - height/2, width, height);
403
  ctx.fillStyle = color;
404
  ctx.fillRect(x - width/2, y - height/2, width * (health/maxHealth), height);
405
  }
 
406
  function drawGame() {
407
  ctx.clearRect(0, 0, canvas.width, canvas.height);
408
  const pattern = ctx.createPattern(backgroundImg, 'repeat');
409
  ctx.fillStyle = pattern;
410
  ctx.fillRect(0, 0, canvas.width, canvas.height);
 
411
  // 플레이어 그리기
412
  ctx.save();
413
  ctx.translate(player.x, player.y);
414
  ctx.rotate(player.angle);
415
  ctx.drawImage(playerImg, -player.width/2, -player.height/2, player.width, player.height);
416
  ctx.restore();
 
417
  // 체력바
418
  drawHealthBar(canvas.width/2, 30, player.health, player.maxHealth, 200, 20, 'green');
 
419
  // 적 그리기
420
  enemies.forEach(enemy => {
421
  ctx.save();
 
426
  ctx.restore();
427
  drawHealthBar(enemy.x, enemy.y - 40, enemy.health, enemy.maxHealth, 60, 5, 'red');
428
  });
 
429
  // 총알 그리기
430
  bullets.forEach(bullet => {
431
  ctx.beginPath();
 
433
  ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
434
  ctx.fill();
435
  });
 
436
  // 아이템 그리기
437
  items.forEach(item => {
438
  ctx.beginPath();
 
440
  ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
441
  ctx.fill();
442
  });
 
443
  // UI 그리기
444
  ctx.fillStyle = 'white';
445
  ctx.font = '24px Arial';
446
+ ctx.fillText(`Round ${currentRound}/10`, 10, 30);
447
+ ctx.fillText(`Gold: ${gold}`, 10, 60);
 
448
  if(isCountingDown) {
449
  ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
450
  ctx.fillRect(0, 0, canvas.width, canvas.height);
 
455
  drawGame();
456
  requestAnimationFrame(gameLoop);
457
  }
 
458
  nextRoundBtn.addEventListener('click', () => {
459
  currentRound++;
460
  nextRoundBtn.style.display = 'none';
461
  document.getElementById('shop').style.display = 'none'; // 상점 창 닫기
462
  initRound();
463
  });
 
464
  bossButton.addEventListener('click', startBossStage);
 
465
  restartBtn.addEventListener('click', () => {
466
  currentRound = 1;
467
  gameOver = false;
 
484
  gameLoop();
485
  bgm.play(); // 게임 시작시 BGM 재생
486
  });
 
487
  window.addEventListener('resize', () => {
488
  canvas.width = window.innerWidth;
489
  canvas.height = window.innerHeight;