-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
373 lines (322 loc) · 10.1 KB
/
script.js
File metadata and controls
373 lines (322 loc) · 10.1 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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const highScoreElement = document.getElementById('highScore');
const gameOverElement = document.getElementById('gameOver');
const startMessageElement = document.getElementById('startMessage');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
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 = true;
let gameStarted = false;
let particles = [];
// Initialize high score display
highScoreElement.textContent = highScore;
// Particle system for visual effects
function createParticle(x, y, color = '#00ff88') {
return {
x: x,
y: y,
vx: (Math.random() - 0.5) * 4,
vy: (Math.random() - 0.5) * 4,
life: 30,
maxLife: 30,
color: color
};
}
function updateParticles() {
particles = particles.filter(particle => {
particle.x += particle.vx;
particle.y += particle.vy;
particle.life--;
return particle.life > 0;
});
}
function drawParticles() {
particles.forEach(particle => {
const alpha = particle.life / particle.maxLife;
ctx.save();
ctx.globalAlpha = alpha;
ctx.fillStyle = particle.color;
ctx.fillRect(particle.x, particle.y, 3, 3);
ctx.restore();
});
}
// Create floating particles in background
function createBackgroundParticles() {
const particleContainer = document.getElementById('particles');
for (let i = 0; i < 20; i++) {
const particle = document.createElement('div');
particle.className = 'particle';
particle.style.left = Math.random() * 100 + '%';
particle.style.animationDelay = Math.random() * 6 + 's';
particle.style.animationDuration = (Math.random() * 3 + 4) + 's';
particleContainer.appendChild(particle);
}
}
// Generate random food position
function randomFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// Make sure food doesn't spawn on snake
for (let part of snake) {
if (food.x === part.x && food.y === part.y) {
randomFood();
return;
}
}
}
// Draw game elements with enhanced graphics
function drawGame() {
// Clear canvas with gradient
const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, '#000000');
gradient.addColorStop(0.5, '#0a0a0a');
gradient.addColorStop(1, '#000000');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw grid pattern
ctx.strokeStyle = 'rgba(0, 255, 136, 0.1)';
ctx.lineWidth = 0.5;
for (let i = 0; i <= tileCount; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize, 0);
ctx.lineTo(i * gridSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * gridSize);
ctx.lineTo(canvas.width, i * gridSize);
ctx.stroke();
}
// Draw snake with gradient and glow effect
snake.forEach((part, index) => {
const isHead = index === 0;
const size = gridSize - 2;
const x = part.x * gridSize + 1;
const y = part.y * gridSize + 1;
// Create gradient for snake body
const snakeGradient = ctx.createRadialGradient(
x + size/2, y + size/2, 0,
x + size/2, y + size/2, size/2
);
if (isHead) {
snakeGradient.addColorStop(0, '#00ff88');
snakeGradient.addColorStop(1, '#008844');
// Draw eyes for the head
ctx.fillStyle = snakeGradient;
ctx.fillRect(x, y, size, size);
// Eyes
ctx.fillStyle = '#ffffff';
const eyeSize = 3;
ctx.fillRect(x + 4, y + 4, eyeSize, eyeSize);
ctx.fillRect(x + size - 7, y + 4, eyeSize, eyeSize);
ctx.fillStyle = '#000000';
ctx.fillRect(x + 5, y + 5, 1, 1);
ctx.fillRect(x + size - 6, y + 5, 1, 1);
} else {
const intensity = 1 - (index / snake.length) * 0.5;
snakeGradient.addColorStop(0, `rgba(0, 255, 136, ${intensity})`);
snakeGradient.addColorStop(1, `rgba(0, 136, 68, ${intensity})`);
ctx.fillStyle = snakeGradient;
ctx.fillRect(x, y, size, size);
}
// Add glow effect
ctx.shadowColor = '#00ff88';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
});
// Reset shadow
ctx.shadowBlur = 0;
// Draw food with pulsing effect
const time = Date.now() * 0.005;
const pulseSize = Math.sin(time) * 2 + (gridSize - 4);
const foodX = food.x * gridSize + (gridSize - pulseSize) / 2;
const foodY = food.y * gridSize + (gridSize - pulseSize) / 2;
// Food gradient
const foodGradient = ctx.createRadialGradient(
foodX + pulseSize/2, foodY + pulseSize/2, 0,
foodX + pulseSize/2, foodY + pulseSize/2, pulseSize/2
);
foodGradient.addColorStop(0, '#ff4444');
foodGradient.addColorStop(1, '#aa0000');
ctx.fillStyle = foodGradient;
ctx.fillRect(foodX, foodY, pulseSize, pulseSize);
// Food glow
ctx.shadowColor = '#ff4444';
ctx.shadowBlur = 15;
ctx.fillRect(foodX, foodY, pulseSize, pulseSize);
ctx.shadowBlur = 0;
// Draw and update particles
updateParticles();
drawParticles();
}
// Move snake
function moveSnake() {
if (!gameRunning || !gameStarted) return;
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
// Check wall collision
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
return;
}
// Check self collision
for (let part of snake) {
if (head.x === part.x && head.y === part.y) {
gameOver();
return;
}
}
snake.unshift(head);
// Check food collision
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score;
// Update high score
if (score > highScore) {
highScore = score;
highScoreElement.textContent = highScore;
localStorage.setItem('snakeHighScore', highScore);
// Create celebration particles
for (let i = 0; i < 10; i++) {
particles.push(createParticle(
food.x * gridSize + Math.random() * gridSize,
food.y * gridSize + Math.random() * gridSize,
'#ffff00'
));
}
}
// Create eating particles
for (let i = 0; i < 5; i++) {
particles.push(createParticle(
food.x * gridSize + Math.random() * gridSize,
food.y * gridSize + Math.random() * gridSize,
'#ff4444'
));
}
randomFood();
} else {
snake.pop();
}
}
// Game over with screen shake effect
function gameOver() {
gameRunning = false;
gameStarted = false;
gameOverElement.style.display = 'block';
// Create explosion particles
for (let i = 0; i < 20; i++) {
particles.push(createParticle(
snake[0].x * gridSize + Math.random() * gridSize,
snake[0].y * gridSize + Math.random() * gridSize,
'#ff0000'
));
}
// Screen shake effect
canvas.style.animation = 'shake 0.5s ease-in-out';
setTimeout(() => {
canvas.style.animation = '';
}, 500);
}
// Restart game
function restartGame() {
snake = [{x: 10, y: 10}];
dx = 0;
dy = 0;
score = 0;
particles = [];
scoreElement.textContent = score;
gameRunning = true;
gameStarted = false;
gameOverElement.style.display = 'none';
startMessageElement.style.display = 'block';
randomFood();
}
// Start game
function startGame() {
if (!gameStarted && gameRunning) {
gameStarted = true;
startMessageElement.style.display = 'none';
}
}
// Keyboard controls with visual feedback
document.addEventListener('keydown', (e) => {
e.preventDefault();
if (!gameRunning && e.key.toLowerCase() === 'r') {
restartGame();
return;
}
if (!gameRunning) return;
const key = e.key.toLowerCase();
// Add visual feedback for key presses
const keyElements = document.querySelectorAll('.key');
keyElements.forEach(keyEl => {
if (keyEl.textContent.toLowerCase() === key) {
keyEl.style.transform = 'translateY(-2px) scale(1.1)';
keyEl.style.boxShadow = '0 6px 12px rgba(0, 255, 136, 0.5)';
setTimeout(() => {
keyEl.style.transform = '';
keyEl.style.boxShadow = '';
}, 200);
}
});
// Prevent reverse direction and start game
switch(key) {
case 'w':
if (dy !== 1) {
dx = 0;
dy = -1;
startGame();
}
break;
case 's':
if (dy !== -1) {
dx = 0;
dy = 1;
startGame();
}
break;
case 'a':
if (dx !== 1) {
dx = -1;
dy = 0;
startGame();
}
break;
case 'd':
if (dx !== -1) {
dx = 1;
dy = 0;
startGame();
}
break;
}
});
// Ensure focus on the document for keyboard events
document.addEventListener('DOMContentLoaded', () => {
document.body.focus();
createBackgroundParticles();
});
// Click to focus (fallback)
document.addEventListener('click', () => {
document.body.focus();
});
// Game loop
function gameLoop() {
moveSnake();
drawGame();
}
// Initialize game
randomFood();
drawGame();
createBackgroundParticles();
setInterval(gameLoop, 150);