bozhong commited on
Commit
da78402
·
verified ·
1 Parent(s): bb762dc

请帮我实现一个塔防类小游戏(例如,保卫萝卜) - Initial Deployment

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