Spaces:
Running
Running
File size: 40,444 Bytes
da78402 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>保卫萝卜塔防游戏</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Ma+Shan+Zheng&display=swap');
body {
font-family: 'Ma Shan Zheng', cursive;
background-color: #f0f8ff;
user-select: none;
}
.game-container {
position: relative;
width: 100%;
max-width: 800px;
margin: 0 auto;
}
#gameCanvas {
background-color: #e6f7ff;
border: 3px solid #4a90e2;
border-radius: 10px;
box-shadow: 0 0 20px rgba(74, 144, 226, 0.3);
}
.tower-option {
transition: all 0.2s ease;
}
.tower-option:hover {
transform: scale(1.05);
box-shadow: 0 0 15px rgba(255, 215, 0, 0.5);
}
.tower-option:active {
transform: scale(0.95);
}
.enemy-health-bar {
height: 4px;
background: linear-gradient(to right, #ff0000, #ff9900);
position: absolute;
top: -8px;
left: 0;
}
.wave-button {
background: linear-gradient(to bottom, #ff9966, #ff5e62);
color: white;
border: none;
border-radius: 50px;
font-weight: bold;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: all 0.2s;
}
.wave-button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}
.wave-button:active {
transform: translateY(1px);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.tower-range {
position: absolute;
border-radius: 50%;
background-color: rgba(100, 200, 255, 0.2);
border: 1px dashed rgba(100, 150, 255, 0.5);
pointer-events: none;
}
.projectile {
position: absolute;
border-radius: 50%;
background-color: gold;
box-shadow: 0 0 5px yellow;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.pulse {
animation: pulse 1s infinite;
}
</style>
</head>
<body class="bg-blue-50 min-h-screen py-8">
<div class="container mx-auto px-4">
<h1 class="text-4xl md:text-5xl font-bold text-center text-blue-800 mb-2">保卫萝卜</h1>
<p class="text-center text-blue-600 mb-8">建造防御塔保护你的萝卜免受怪物侵袭!</p>
<div class="game-container">
<!-- 游戏状态栏 -->
<div class="flex justify-between items-center mb-4 bg-blue-100 p-3 rounded-lg shadow">
<div class="flex items-center">
<div class="bg-yellow-100 border-2 border-yellow-300 rounded-full p-2 mr-3">
<span class="text-yellow-700 font-bold text-xl" id="money">100</span>
</div>
<div class="bg-red-100 border-2 border-red-300 rounded-full p-2">
<span class="text-red-700 font-bold text-xl" id="lives">10</span>
</div>
</div>
<div class="text-center">
<div class="text-blue-700 font-bold">波数: <span id="wave">0</span>/10</div>
<div class="text-sm text-gray-600">下一波: <span id="next-wave-timer">5</span>秒</div>
</div>
<button id="start-wave" class="wave-button px-6 py-2 text-lg">
开始波次
</button>
</div>
<!-- 游戏画布 -->
<canvas id="gameCanvas" width="800" height="500" class="w-full"></canvas>
<!-- 塔选项 -->
<div class="flex justify-between mt-4 bg-blue-100 p-3 rounded-lg shadow">
<div class="tower-option flex flex-col items-center p-2 bg-white rounded-lg cursor-pointer border-2 border-transparent hover:border-yellow-400" data-tower="arrow">
<div class="w-12 h-12 bg-blue-200 rounded-full flex items-center justify-center mb-1">
<div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
<div class="w-4 h-4 bg-blue-800 rounded-full"></div>
</div>
</div>
<span class="font-bold text-blue-800">箭塔</span>
<span class="text-sm text-yellow-700">$50</span>
</div>
<div class="tower-option flex flex-col items-center p-2 bg-white rounded-lg cursor-pointer border-2 border-transparent hover:border-yellow-400" data-tower="cannon">
<div class="w-12 h-12 bg-red-200 rounded-full flex items-center justify-center mb-1">
<div class="w-8 h-8 bg-red-500 rounded-full"></div>
</div>
<span class="font-bold text-red-800">炮塔</span>
<span class="text-sm text-yellow-700">$80</span>
</div>
<div class="tower-option flex flex-col items-center p-2 bg-white rounded-lg cursor-pointer border-2 border-transparent hover:border-yellow-400" data-tower="magic">
<div class="w-12 h-12 bg-purple-200 rounded-full flex items-center justify-center mb-1">
<div class="w-8 h-8 bg-purple-500 rounded-full flex items-center justify-center">
<div class="w-4 h-4 bg-white rounded-full"></div>
</div>
</div>
<span class="font-bold text-purple-800">魔法塔</span>
<span class="text-sm text-yellow-700">$120</span>
</div>
<div class="tower-option flex flex-col items-center p-2 bg-white rounded-lg cursor-pointer border-2 border-transparent hover:border-yellow-400" data-tower="slow">
<div class="w-12 h-12 bg-green-200 rounded-full flex items-center justify-center mb-1">
<div class="w-8 h-8 bg-green-500 rounded-full flex items-center justify-center">
<div class="w-6 h-6 bg-white rounded-full opacity-50"></div>
</div>
</div>
<span class="font-bold text-green-800">减速塔</span>
<span class="text-sm text-yellow-700">$90</span>
</div>
</div>
<!-- 游戏说明 -->
<div class="mt-6 bg-white p-4 rounded-lg shadow">
<h2 class="text-xl font-bold text-blue-800 mb-2">游戏说明</h2>
<ul class="list-disc pl-5 text-gray-700">
<li class="mb-1">点击下方的防御塔,然后在地图上放置</li>
<li class="mb-1">每波敌人来袭前有5秒准备时间</li>
<li class="mb-1">敌人到达终点会减少你的生命值</li>
<li class="mb-1">击败敌人获得金币,用于建造更多防御塔</li>
<li class="mb-1">成功防御10波敌人获胜!</li>
</ul>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// 游戏常量
const TOWER_TYPES = {
arrow: { cost: 50, damage: 10, range: 150, fireRate: 800, color: '#3b82f6', projectileSpeed: 8 },
cannon: { cost: 80, damage: 25, range: 120, fireRate: 1500, color: '#ef4444', projectileSpeed: 6 },
magic: { cost: 120, damage: 15, range: 180, fireRate: 1000, color: '#8b5cf6', projectileSpeed: 10 },
slow: { cost: 90, damage: 5, range: 160, fireRate: 2000, color: '#10b981', projectileSpeed: 5, slowEffect: 0.5 }
};
const ENEMY_TYPES = [
{ health: 30, speed: 1.5, color: '#f59e0b', reward: 10 },
{ health: 60, speed: 1.0, color: '#ef4444', reward: 20 },
{ health: 100, speed: 0.8, color: '#7c3aed', reward: 35 },
{ health: 150, speed: 0.6, color: '#1e293b', reward: 50 }
];
// 游戏状态
let gameState = {
money: 100,
lives: 10,
wave: 0,
maxWave: 10,
waveInProgress: false,
nextWaveTimer: 5,
selectedTower: null,
towers: [],
enemies: [],
projectiles: [],
path: [],
carrotPosition: { x: 750, y: 250 },
gameOver: false,
gameWon: false
};
// DOM元素
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const moneyDisplay = document.getElementById('money');
const livesDisplay = document.getElementById('lives');
const waveDisplay = document.getElementById('wave');
const nextWaveTimerDisplay = document.getElementById('next-wave-timer');
const startWaveButton = document.getElementById('start-wave');
const towerOptions = document.querySelectorAll('.tower-option');
// 初始化游戏路径
function initPath() {
gameState.path = [
{ x: 0, y: 100 },
{ x: 200, y: 100 },
{ x: 200, y: 400 },
{ x: 400, y: 400 },
{ x: 400, y: 200 },
{ x: 600, y: 200 },
{ x: 600, y: 300 },
{ x: gameState.carrotPosition.x, y: gameState.carrotPosition.y }
];
}
// 绘制游戏
function draw() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制背景网格
drawGrid();
// 绘制路径
drawPath();
// 绘制萝卜
drawCarrot();
// 绘制防御塔
gameState.towers.forEach(tower => {
drawTower(tower);
});
// 绘制敌人
gameState.enemies.forEach(enemy => {
drawEnemy(enemy);
});
// 绘制投射物
gameState.projectiles.forEach(projectile => {
drawProjectile(projectile);
});
// 如果游戏结束,显示游戏结束画面
if (gameState.gameOver) {
drawGameOver();
}
// 如果游戏胜利,显示胜利画面
if (gameState.gameWon) {
drawGameWon();
}
}
// 绘制背景网格
function drawGrid() {
ctx.strokeStyle = '#dbeafe';
ctx.lineWidth = 1;
for (let x = 0; x < canvas.width; x += 50) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
for (let y = 0; y < canvas.height; y += 50) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
}
// 绘制路径
function drawPath() {
ctx.strokeStyle = '#a5b4fc';
ctx.lineWidth = 40;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.beginPath();
ctx.moveTo(gameState.path[0].x, gameState.path[0].y);
for (let i = 1; i < gameState.path.length; i++) {
ctx.lineTo(gameState.path[i].x, gameState.path[i].y);
}
ctx.stroke();
// 绘制路径边缘
ctx.strokeStyle = '#818cf8';
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(gameState.path[0].x, gameState.path[0].y);
for (let i = 1; i < gameState.path.length; i++) {
ctx.lineTo(gameState.path[i].x, gameState.path[i].y);
}
ctx.stroke();
}
// 绘制萝卜
function drawCarrot() {
// 萝卜身体
ctx.fillStyle = '#f97316';
ctx.beginPath();
ctx.arc(gameState.carrotPosition.x, gameState.carrotPosition.y, 30, 0, Math.PI * 2);
ctx.fill();
// 萝卜叶子
ctx.fillStyle = '#22c55e';
ctx.beginPath();
ctx.moveTo(gameState.carrotPosition.x - 15, gameState.carrotPosition.y - 30);
ctx.lineTo(gameState.carrotPosition.x, gameState.carrotPosition.y - 50);
ctx.lineTo(gameState.carrotPosition.x + 15, gameState.carrotPosition.y - 30);
ctx.closePath();
ctx.fill();
// 萝卜眼睛
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(gameState.carrotPosition.x - 10, gameState.carrotPosition.y - 5, 5, 0, Math.PI * 2);
ctx.arc(gameState.carrotPosition.x + 10, gameState.carrotPosition.y - 5, 5, 0, Math.PI * 2);
ctx.fill();
// 萝卜瞳孔
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(gameState.carrotPosition.x - 10, gameState.carrotPosition.y - 5, 2, 0, Math.PI * 2);
ctx.arc(gameState.carrotPosition.x + 10, gameState.carrotPosition.y - 5, 2, 0, Math.PI * 2);
ctx.fill();
// 萝卜嘴巴
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(gameState.carrotPosition.x, gameState.carrotPosition.y + 5, 10, 0, Math.PI);
ctx.stroke();
}
// 绘制防御塔
function drawTower(tower) {
// 塔基
ctx.fillStyle = '#d1d5db';
ctx.beginPath();
ctx.arc(tower.x, tower.y, 20, 0, Math.PI * 2);
ctx.fill();
// 塔身
ctx.fillStyle = tower.color;
ctx.beginPath();
ctx.arc(tower.x, tower.y, 15, 0, Math.PI * 2);
ctx.fill();
// 塔顶
ctx.fillStyle = '#facc15';
ctx.beginPath();
ctx.arc(tower.x, tower.y, 8, 0, Math.PI * 2);
ctx.fill();
// 如果塔正在攻击,绘制攻击方向线
if (tower.target) {
ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(tower.x, tower.y);
ctx.lineTo(tower.target.x, tower.target.y);
ctx.stroke();
}
}
// 绘制敌人
function drawEnemy(enemy) {
// 敌人身体
ctx.fillStyle = enemy.color;
ctx.beginPath();
ctx.arc(enemy.x, enemy.y, 15, 0, Math.PI * 2);
ctx.fill();
// 敌人眼睛
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(enemy.x - 5, enemy.y - 5, 3, 0, Math.PI * 2);
ctx.arc(enemy.x + 5, enemy.y - 5, 3, 0, Math.PI * 2);
ctx.fill();
// 敌人嘴巴
ctx.fillStyle = '#7f1d1d';
ctx.beginPath();
ctx.arc(enemy.x, enemy.y + 5, 5, 0, Math.PI);
ctx.fill();
// 绘制血条
const healthBarWidth = 30;
const healthPercentage = enemy.health / enemy.maxHealth;
ctx.fillStyle = '#dc2626';
ctx.fillRect(enemy.x - healthBarWidth/2, enemy.y - 25, healthBarWidth, 3);
ctx.fillStyle = '#16a34a';
ctx.fillRect(enemy.x - healthBarWidth/2, enemy.y - 25, healthBarWidth * healthPercentage, 3);
}
// 绘制投射物
function drawProjectile(projectile) {
ctx.fillStyle = projectile.color;
ctx.beginPath();
ctx.arc(projectile.x, projectile.y, projectile.radius, 0, Math.PI * 2);
ctx.fill();
// 添加发光效果
ctx.shadowColor = projectile.color;
ctx.shadowBlur = 10;
ctx.fill();
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
}
// 绘制游戏结束画面
function drawGameOver() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.font = 'bold 48px "Ma Shan Zheng", cursive';
ctx.textAlign = 'center';
ctx.fillText('游戏结束', canvas.width/2, canvas.height/2 - 30);
ctx.font = '24px "Ma Shan Zheng", cursive';
ctx.fillText('你的萝卜被吃掉了!', canvas.width/2, canvas.height/2 + 20);
ctx.fillStyle = '#f59e0b';
ctx.font = 'bold 28px "Ma Shan Zheng", cursive';
ctx.fillText('点击重新开始', canvas.width/2, canvas.height/2 + 80);
}
// 绘制游戏胜利画面
function drawGameWon() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#f59e0b';
ctx.font = 'bold 48px "Ma Shan Zheng", cursive';
ctx.textAlign = 'center';
ctx.fillText('胜利!', canvas.width/2, canvas.height/2 - 30);
ctx.fillStyle = 'white';
ctx.font = '24px "Ma Shan Zheng", cursive';
ctx.fillText('你成功保护了萝卜!', canvas.width/2, canvas.height/2 + 20);
ctx.fillStyle = '#f59e0b';
ctx.font = 'bold 28px "Ma Shan Zheng", cursive';
ctx.fillText('点击重新开始', canvas.width/2, canvas.height/2 + 80);
}
// 更新游戏状态
function update() {
if (gameState.gameOver || gameState.gameWon) return;
// 更新敌人
updateEnemies();
// 更新投射物
updateProjectiles();
// 更新防御塔
updateTowers();
// 更新波次计时器
if (!gameState.waveInProgress && gameState.wave < gameState.maxWave) {
updateWaveTimer();
}
// 检查游戏是否结束
if (gameState.lives <= 0) {
gameState.gameOver = true;
}
// 检查游戏是否胜利
if (gameState.wave >= gameState.maxWave && gameState.enemies.length === 0) {
gameState.gameWon = true;
}
// 更新UI
updateUI();
}
// 更新敌人
function updateEnemies() {
for (let i = gameState.enemies.length - 1; i >= 0; i--) {
const enemy = gameState.enemies[i];
// 移动敌人
if (enemy.pathIndex < gameState.path.length - 1) {
const target = gameState.path[enemy.pathIndex + 1];
const dx = target.x - enemy.x;
const dy = target.y - enemy.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 1) {
enemy.pathIndex++;
} else {
enemy.x += (dx / distance) * enemy.speed * (enemy.slowTimer > 0 ? 0.5 : 1);
enemy.y += (dy / distance) * enemy.speed * (enemy.slowTimer > 0 ? 0.5 : 1);
}
}
// 检查敌人是否到达终点
const dx = gameState.carrotPosition.x - enemy.x;
const dy = gameState.carrotPosition.y - enemy.y;
const distanceToCarrot = Math.sqrt(dx * dx + dy * dy);
if (distanceToCarrot < 30) {
gameState.lives--;
gameState.enemies.splice(i, 1);
continue;
}
// 更新减速计时器
if (enemy.slowTimer > 0) {
enemy.slowTimer--;
}
}
}
// 更新投射物
function updateProjectiles() {
for (let i = gameState.projectiles.length - 1; i >= 0; i--) {
const projectile = gameState.projectiles[i];
// 移动投射物
const dx = projectile.target.x - projectile.x;
const dy = projectile.target.y - projectile.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 5) {
// 投射物击中目标
const enemy = gameState.enemies.find(e => e === projectile.target);
if (enemy) {
enemy.health -= projectile.damage;
// 如果是减速塔的投射物,应用减速效果
if (projectile.slowEffect) {
enemy.slowTimer = 120; // 2秒减速效果
}
// 检查敌人是否被击败
if (enemy.health <= 0) {
gameState.money += enemy.reward;
const index = gameState.enemies.indexOf(enemy);
if (index !== -1) {
gameState.enemies.splice(index, 1);
}
}
}
gameState.projectiles.splice(i, 1);
} else {
projectile.x += (dx / distance) * projectile.speed;
projectile.y += (dy / distance) * projectile.speed;
}
}
}
// 更新防御塔
function updateTowers() {
gameState.towers.forEach(tower => {
// 重置目标
tower.target = null;
// 查找范围内的敌人
let closestEnemy = null;
let minDistance = Infinity;
gameState.enemies.forEach(enemy => {
const dx = enemy.x - tower.x;
const dy = enemy.y - tower.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < tower.range && distance < minDistance) {
closestEnemy = enemy;
minDistance = distance;
}
});
// 如果找到敌人且冷却结束,则攻击
if (closestEnemy && Date.now() - tower.lastShot > tower.fireRate) {
tower.target = closestEnemy;
tower.lastShot = Date.now();
// 创建投射物
gameState.projectiles.push({
x: tower.x,
y: tower.y,
target: closestEnemy,
damage: tower.damage,
speed: tower.projectileSpeed,
radius: 5,
color: tower.color,
slowEffect: tower.slowEffect
});
}
});
}
// 更新波次计时器
function updateWaveTimer() {
if (gameState.nextWaveTimer > 0) {
gameState.nextWaveTimer -= 1/60; // 假设60FPS
if (gameState.nextWaveTimer <= 0) {
startWave();
}
}
}
// 更新UI
function updateUI() {
moneyDisplay.textContent = gameState.money;
livesDisplay.textContent = gameState.lives;
waveDisplay.textContent = gameState.wave;
if (!gameState.waveInProgress && gameState.wave < gameState.maxWave) {
nextWaveTimerDisplay.textContent = Math.ceil(gameState.nextWaveTimer);
} else {
nextWaveTimerDisplay.textContent = '战斗中';
}
// 更新开始波次按钮状态
if (gameState.waveInProgress || gameState.wave >= gameState.maxWave) {
startWaveButton.disabled = true;
startWaveButton.classList.remove('hover:transform', 'hover:-translate-y-2', 'hover:shadow-md');
startWaveButton.classList.add('opacity-50');
} else {
startWaveButton.disabled = false;
startWaveButton.classList.add('hover:transform', 'hover:-translate-y-2', 'hover:shadow-md');
startWaveButton.classList.remove('opacity-50');
}
// 更新塔选项的可购买状态
towerOptions.forEach(option => {
const towerType = option.dataset.tower;
const cost = TOWER_TYPES[towerType].cost;
if (gameState.money < cost) {
option.classList.add('opacity-50');
option.classList.remove('cursor-pointer', 'hover:border-yellow-400');
option.classList.add('cursor-not-allowed');
} else {
option.classList.remove('opacity-50');
option.classList.add('cursor-pointer', 'hover:border-yellow-400');
option.classList.remove('cursor-not-allowed');
}
});
}
// 开始一波敌人
function startWave() {
if (gameState.waveInProgress || gameState.wave >= gameState.maxWave) return;
gameState.wave++;
gameState.waveInProgress = true;
gameState.nextWaveTimer = 5;
// 根据波数生成敌人
const enemyCount = 5 + gameState.wave * 2;
const enemyTypes = [];
if (gameState.wave < 3) {
enemyTypes.push(0); // 只生成第一种敌人
} else if (gameState.wave < 6) {
enemyTypes.push(0, 1); // 前两种敌人
} else if (gameState.wave < 9) {
enemyTypes.push(0, 1, 2); // 前三种敌人
} else {
enemyTypes.push(0, 1, 2, 3); // 所有敌人
}
for (let i = 0; i < enemyCount; i++) {
const typeIndex = enemyTypes[Math.floor(Math.random() * enemyTypes.length)];
const type = ENEMY_TYPES[typeIndex];
// 随着波数增加,敌人变得更强大
const healthMultiplier = 1 + (gameState.wave - 1) * 0.1;
const rewardMultiplier = 1 + (gameState.wave - 1) * 0.05;
setTimeout(() => {
gameState.enemies.push({
x: gameState.path[0].x,
y: gameState.path[0].y,
health: type.health * healthMultiplier,
maxHealth: type.health * healthMultiplier,
speed: type.speed,
color: type.color,
reward: Math.round(type.reward * rewardMultiplier),
pathIndex: 0,
slowTimer: 0
});
}, i * 1000); // 每秒生成一个敌人
}
// 检查波次是否结束
const checkWaveEnd = setInterval(() => {
if (gameState.enemies.length === 0) {
gameState.waveInProgress = false;
clearInterval(checkWaveEnd);
}
}, 1000);
}
// 游戏循环
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// 初始化游戏
function initGame() {
gameState = {
money: 100,
lives: 10,
wave: 0,
maxWave: 10,
waveInProgress: false,
nextWaveTimer: 5,
selectedTower: null,
towers: [],
enemies: [],
projectiles: [],
path: [],
carrotPosition: { x: 750, y: 250 },
gameOver: false,
gameWon: false
};
initPath();
startWaveButton.disabled = false;
startWaveButton.classList.remove('opacity-50');
}
// 事件监听器
canvas.addEventListener('click', (e) => {
if (gameState.gameOver || gameState.gameWon) {
initGame();
return;
}
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// 检查是否点击了路径
const isOnPath = isPointOnPath(x, y);
if (gameState.selectedTower && !isOnPath) {
// 检查是否有足够的金钱
const towerType = TOWER_TYPES[gameState.selectedTower];
if (gameState.money >= towerType.cost) {
// 检查是否与其他塔重叠
const overlapping = gameState.towers.some(tower => {
const dx = tower.x - x;
const dy = tower.y - y;
return Math.sqrt(dx * dx + dy * dy) < 40;
});
if (!overlapping) {
// 放置防御塔
gameState.towers.push({
x: x,
y: y,
type: gameState.selectedTower,
damage: towerType.damage,
range: towerType.range,
fireRate: towerType.fireRate,
color: towerType.color,
projectileSpeed: towerType.projectileSpeed,
lastShot: 0,
target: null,
slowEffect: towerType.slowEffect
});
gameState.money -= towerType.cost;
gameState.selectedTower = null;
// 移除所有范围指示器
document.querySelectorAll('.tower-range').forEach(el => el.remove());
}
}
}
});
// 检查点是否在路径上
function isPointOnPath(x, y) {
// 简化检查:只需检查是否靠近路径点
for (let i = 0; i < gameState.path.length; i++) {
const point = gameState.path[i];
const dx = point.x - x;
const dy = point.y - y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 30) {
return true;
}
}
// 检查是否在路径线段上
for (let i = 0; i < gameState.path.length - 1; i++) {
const p1 = gameState.path[i];
const p2 = gameState.path[i + 1];
// 计算点到线段的距离
const distance = pointToLineDistance(x, y, p1.x, p1.y, p2.x, p2.y);
if (distance < 25) {
return true;
}
}
return false;
}
// 计算点到线段的距离
function pointToLineDistance(x, y, x1, y1, x2, y2) {
const A = x - x1;
const B = y - y1;
const C = x2 - x1;
const D = y2 - y1;
const dot = A * C + B * D;
const len_sq = C * C + D * D;
let param = -1;
if (len_sq !== 0) {
param = dot / len_sq;
}
let xx, yy;
if (param < 0) {
xx = x1;
yy = y1;
} else if (param > 1) {
xx = x2;
yy = y2;
} else {
xx = x1 + param * C;
yy = y1 + param * D;
}
const dx = x - xx;
const dy = y - yy;
return Math.sqrt(dx * dx + dy * dy);
}
// 塔选项点击事件
towerOptions.forEach(option => {
option.addEventListener('click', () => {
const towerType = option.dataset.tower;
const cost = TOWER_TYPES[towerType].cost;
if (gameState.money >= cost) {
gameState.selectedTower = towerType;
// 移除之前的所有范围指示器
document.querySelectorAll('.tower-range').forEach(el => el.remove());
// 创建新的范围指示器
const rangeIndicator = document.createElement('div');
rangeIndicator.className = 'tower-range';
rangeIndicator.style.width = `${TOWER_TYPES[towerType].range * 2}px`;
rangeIndicator.style.height = `${TOWER_TYPES[towerType].range * 2}px`;
rangeIndicator.style.left = `0px`;
rangeIndicator.style.top = `0px`;
rangeIndicator.style.transform = 'translate(-50%, -50%)';
document.body.appendChild(rangeIndicator);
// 更新范围指示器位置
const updateRangeIndicator = (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
rangeIndicator.style.left = `${x + rect.left}px`;
rangeIndicator.style.top = `${y + rect.top}px`;
};
canvas.addEventListener('mousemove', updateRangeIndicator);
// 当取消选择或放置塔时移除事件监听器
const cancelSelection = () => {
canvas.removeEventListener('mousemove', updateRangeIndicator);
rangeIndicator.remove();
};
canvas.addEventListener('click', cancelSelection, { once: true });
}
});
});
// 开始波次按钮点击事件
startWaveButton.addEventListener('click', startWave);
// 初始化并开始游戏
initPath();
gameLoop();
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=bozhong/carrotprotection" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html> |