-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
469 lines (408 loc) · 13.5 KB
/
script.js
File metadata and controls
469 lines (408 loc) · 13.5 KB
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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const highScoreElement = document.getElementById('highScore');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
// Kích thước ô
const gridSize = 30;
const tileCount = canvas.width / gridSize;
// Biến trò chơi
let snake = [{ x: 10, y: 10 }];
let food = {};
let dx = 0;
let dy = 0;
let score = 0;
let highScore = localStorage.getItem('snakeHighScore') || 0;
let gameRunning = false;
let gamePaused = false;
let gameSpeed = 150;
let animationId;
// Biến cho chuyển động mượt
let snakePositions = [];
let animationProgress = 0;
let lastRenderTime = 0;
const frameTime = 1000 / 60; // 60 FPS
// Hiển thị điểm cao
highScoreElement.textContent = highScore;
// Tạo mồi ngẫu nhiên
function randomFood() {
do {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
} while (snake.some(segment => segment.x === food.x && segment.y === food.y));
}
// Tính toán vị trí nội suy cho chuyển động mượt
function interpolatePosition(current, target, progress) {
return {
x: current.x + (target.x - current.x) * progress,
y: current.y + (target.y - current.y) * progress
};
}
// Vẽ trò chơi với animation mượt
function drawGame() {
// Xóa canvas
ctx.fillStyle = 'rgba(0, 0, 0, 0.9)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Vẽ lưới (tùy chọn)
ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
ctx.lineWidth = 1;
for (let i = 0; i < tileCount; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize, 0);
ctx.lineTo(i * gridSize, canvas.height);
ctx.moveTo(0, i * gridSize);
ctx.lineTo(canvas.width, i * gridSize);
ctx.stroke();
}
// Vẽ rắn với animation mượt
snake.forEach((segment, index) => {
let drawX = segment.x * gridSize;
let drawY = segment.y * gridSize;
// Áp dụng animation mượt
if (snakePositions[index] && animationProgress < 1) {
const interpolated = interpolatePosition(
snakePositions[index],
{ x: drawX, y: drawY },
animationProgress
);
drawX = interpolated.x;
drawY = interpolated.y;
}
if (index === 0) {
// Đầu rắn với gradient
const gradient = ctx.createLinearGradient(drawX, drawY, drawX + gridSize, drawY + gridSize);
gradient.addColorStop(0, '#ff6b6b');
gradient.addColorStop(1, '#ff5252');
ctx.fillStyle = gradient;
// Vẽ đầu rắn bo tròn
ctx.beginPath();
ctx.roundRect(drawX + 2, drawY + 2, gridSize - 4, gridSize - 4, 4);
ctx.fill();
// Vẽ mắt với hiệu ứng
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(drawX + 6, drawY + 6, 2, 0, 2 * Math.PI);
ctx.arc(drawX + 14, drawY + 6, 2, 0, 2 * Math.PI);
ctx.fill();
// Vẽ pupil
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(drawX + 6, drawY + 6, 1, 0, 2 * Math.PI);
ctx.arc(drawX + 14, drawY + 6, 1, 0, 2 * Math.PI);
ctx.fill();
// Vẽ highlight mắt
ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';
ctx.beginPath();
ctx.arc(drawX + 6.5, drawY + 5.5, 0.5, 0, 2 * Math.PI);
ctx.arc(drawX + 14.5, drawY + 5.5, 0.5, 0, 2 * Math.PI);
ctx.fill();
} else {
// Thân rắn với gradient động
const hue = (180 + index * 10 + Date.now() * 0.1) % 360;
const gradient = ctx.createLinearGradient(drawX, drawY, drawX + gridSize, drawY + gridSize);
gradient.addColorStop(0, `hsl(${hue}, 70%, 60%)`);
gradient.addColorStop(1, `hsl(${hue}, 70%, 40%)`);
ctx.fillStyle = gradient;
// Vẽ thân bo tròn
ctx.beginPath();
ctx.roundRect(drawX + 2, drawY + 2, gridSize - 4, gridSize - 4, 3);
ctx.fill();
// Vẽ highlight
ctx.fillStyle = `hsla(${hue}, 70%, 80%, 0.3)`;
ctx.beginPath();
ctx.roundRect(drawX + 3, drawY + 3, gridSize - 6, gridSize - 6, 2);
ctx.fill();
}
});
// Vẽ mồi với hiệu ứng nhấp nháy
const foodPulse = Math.sin(Date.now() * 0.008) * 0.1 + 0.9;
const foodSize = (gridSize / 2 - 2) * foodPulse;
// Vẽ shadow cho mồi
ctx.fillStyle = 'rgba(255, 107, 107, 0.3)';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2 + 1,
food.y * gridSize + gridSize / 2 + 1,
foodSize + 2,
0,
2 * Math.PI
);
ctx.fill();
// Vẽ mồi chính
const foodGradient = ctx.createRadialGradient(
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
0,
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
foodSize
);
foodGradient.addColorStop(0, '#ff6b6b');
foodGradient.addColorStop(0.7, '#ff5252');
foodGradient.addColorStop(1, '#d32f2f');
ctx.fillStyle = foodGradient;
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
foodSize,
0,
2 * Math.PI
);
ctx.fill();
// Vẽ highlight cho mồi
ctx.fillStyle = 'rgba(255, 255, 255, 0.4)';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2 - 2,
food.y * gridSize + gridSize / 2 - 2,
foodSize * 0.3,
0,
2 * Math.PI
);
ctx.fill();
}
// Cập nhật vị trí cho animation mượt
function updateSnakePositions() {
snakePositions = snake.map(segment => ({
x: segment.x * gridSize,
y: segment.y * gridSize
}));
}
// Di chuyển rắn
function moveSnake() {
if (gamePaused || (dx === 0 && dy === 0)) return;
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
// Kiểm tra va chạm với tường
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
return;
}
// Kiểm tra va chạm với thân
for (let segment of snake) {
if (head.x === segment.x && head.y === segment.y) {
gameOver();
return;
}
}
// Lưu vị trí cũ cho animation
updateSnakePositions();
snake.unshift(head);
// Kiểm tra ăn mồi
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score;
// Hiệu ứng tăng điểm
scoreElement.parentElement.classList.add('score-animate');
setTimeout(() => {
scoreElement.parentElement.classList.remove('score-animate');
}, 300);
randomFood();
// Tăng tốc độ dần
if (gameSpeed > 80) {
gameSpeed = Math.max(80, gameSpeed - 2);
}
// Kiểm tra điểm cao
if (score > highScore) {
highScore = score;
highScoreElement.textContent = highScore;
localStorage.setItem('snakeHighScore', highScore);
}
} else {
snake.pop();
}
// Reset animation progress
animationProgress = 0;
}
// Game over
function gameOver() {
gameRunning = false;
finalScoreElement.textContent = score;
gameOverScreen.style.display = 'block';
// Hiệu ứng rung canvas
canvas.style.animation = 'shake 0.5s ease-in-out';
setTimeout(() => {
canvas.style.animation = '';
}, 500);
}
// Khởi động lại game
function restartGame() {
snake = [{ x: 10, y: 10 }];
dx = 0;
dy = 0;
score = 0;
gameSpeed = 150;
scoreElement.textContent = score;
randomFood();
gameOverScreen.style.display = 'none';
gameRunning = true;
gamePaused = false;
animationProgress = 0;
snakePositions = [];
updateSnakePositions();
// Reset nút pause
document.querySelector('.pause-btn').textContent = '⏸️ Tạm dừng';
}
// Tạm dừng/tiếp tục
function togglePause() {
if (gameRunning) {
gamePaused = !gamePaused;
document.querySelector('.pause-btn').textContent = gamePaused ? '▶️ Tiếp tục' : '⏸️ Tạm dừng';
}
}
// Vòng lặp game với animation mượt
function gameLoop(currentTime) {
// Tính toán thời gian delta
const deltaTime = currentTime - lastRenderTime;
if (deltaTime >= frameTime) {
if (gameRunning) {
// Cập nhật animation progress
animationProgress = Math.min(1, animationProgress + deltaTime / gameSpeed);
// Di chuyển rắn khi animation hoàn thành
if (animationProgress >= 1) {
moveSnake();
}
drawGame();
}
lastRenderTime = currentTime;
}
animationId = requestAnimationFrame(gameLoop);
}
// Điều khiển bàn phím
document.addEventListener('keydown', (e) => {
if (!gameRunning) return;
// Ngăn chặn cuộn trang
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(e.key)) {
e.preventDefault();
}
// Nếu rắn chưa bắt đầu di chuyển, cho phép di chuyển đầu tiên
if (dx === 0 && dy === 0) {
switch(e.key) {
case 'ArrowUp':
dx = 0;
dy = -1;
break;
case 'ArrowDown':
dx = 0;
dy = 1;
break;
case 'ArrowLeft':
dx = -1;
dy = 0;
break;
case 'ArrowRight':
dx = 1;
dy = 0;
break;
}
return;
}
if (gamePaused) return;
switch(e.key) {
case 'ArrowUp':
if (dy !== 1) {
dx = 0;
dy = -1;
}
break;
case 'ArrowDown':
if (dy !== -1) {
dx = 0;
dy = 1;
}
break;
case 'ArrowLeft':
if (dx !== 1) {
dx = -1;
dy = 0;
}
break;
case 'ArrowRight':
if (dx !== -1) {
dx = 1;
dy = 0;
}
break;
case ' ':
togglePause();
break;
}
});
// Điều khiển cảm ứng cho mobile
let touchStartX = 0;
let touchStartY = 0;
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
});
canvas.addEventListener('touchend', (e) => {
e.preventDefault();
if (!gameRunning || gamePaused) return;
const touchEndX = e.changedTouches[0].clientX;
const touchEndY = e.changedTouches[0].clientY;
const diffX = touchStartX - touchEndX;
const diffY = touchStartY - touchEndY;
// Kiểm tra ngưỡng vuốt tối thiểu
if (Math.abs(diffX) < 10 && Math.abs(diffY) < 10) return;
// Nếu rắn chưa di chuyển
if (dx === 0 && dy === 0) {
if (Math.abs(diffX) > Math.abs(diffY)) {
dx = diffX > 0 ? -1 : 1;
dy = 0;
} else {
dx = 0;
dy = diffY > 0 ? -1 : 1;
}
return;
}
if (Math.abs(diffX) > Math.abs(diffY)) {
// Vuốt ngang
if (diffX > 0 && dx !== 1) {
dx = -1;
dy = 0;
} else if (diffX < 0 && dx !== -1) {
dx = 1;
dy = 0;
}
} else {
// Vuốt dọc
if (diffY > 0 && dy !== 1) {
dx = 0;
dy = -1;
} else if (diffY < 0 && dy !== -1) {
dx = 0;
dy = 1;
}
}
});
// Thêm fallback cho roundRect nếu không được hỗ trợ
if (!CanvasRenderingContext2D.prototype.roundRect) {
CanvasRenderingContext2D.prototype.roundRect = function(x, y, width, height, radius) {
this.beginPath();
this.moveTo(x + radius, y);
this.lineTo(x + width - radius, y);
this.arcTo(x + width, y, x + width, y + radius, radius);
this.lineTo(x + width, y + height - radius);
this.arcTo(x + width, y + height, x + width - radius, y + height, radius);
this.lineTo(x + radius, y + height);
this.arcTo(x, y + height, x, y + height - radius, radius);
this.lineTo(x, y + radius);
this.arcTo(x, y, x + radius, y, radius);
this.closePath();
};
}
// Khởi tạo game
function initGame() {
randomFood();
updateSnakePositions();
drawGame();
gameRunning = true;
gamePaused = false;
requestAnimationFrame(gameLoop);
}
// Bắt đầu game
initGame();