-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (85 loc) · 2.82 KB
/
Copy pathscript.js
File metadata and controls
102 lines (85 loc) · 2.82 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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const restartButton = document.getElementById('restartButton');
const scoreDisplay = document.getElementById('score');
const gridSize = 20;
const canvasSize = 400;
let snake, food, score, direction, gameInterval, speed;
function initGame() {
snake = [{ x: gridSize * 5, y: gridSize * 5 }];
direction = { x: 0, y: 0 };
score = 0;
speed = 100;
placeFood();
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed);
scoreDisplay.textContent = "00";
}
function placeFood() {
let foodOverlap = true;
while (foodOverlap) {
food = {
x: Math.floor(Math.random() * canvasSize / gridSize) * gridSize,
y: Math.floor(Math.random() * canvasSize / gridSize) * gridSize
};
foodOverlap = snakeCollision(food);
}
}
function gameLoop() {
update();
draw();
}
function update() {
const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
if (head.x === food.x && head.y === food.y) {
score++;
scoreDisplay.textContent = score.toString().padStart(2, '0');
snake.push({});
placeFood();
speed -= 5;
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed);
}
for (let i = snake.length - 1; i > 0; i--) {
snake[i] = { ...snake[i - 1] };
}
snake[0] = head;
if (head.x < 0 || head.x >= canvasSize || head.y < 0 || head.y >= canvasSize || snakeCollision(head)) {
initGame();
}
}
function draw() {
ctx.fillStyle = '#34495e';
ctx.fillRect(0, 0, canvasSize, canvasSize);
ctx.strokeStyle = '#2c3e50';
for (let i = 0; i < canvasSize / gridSize; i++) {
for (let j = 0; j < canvasSize / gridSize; j++) {
ctx.strokeRect(i * gridSize, j * gridSize, gridSize, gridSize);
}
}
ctx.fillStyle = '#ffffff';
snake.forEach(segment => ctx.fillRect(segment.x, segment.y, gridSize, gridSize));
ctx.fillStyle = '#e74c3c';
ctx.fillRect(food.x, food.y, gridSize, gridSize);
}
function snakeCollision(head) {
return snake.some((segment, index) => index !== 0 && segment.x === head.x && segment.y === head.y);
}
window.addEventListener('keydown', e => {
switch (e.key) {
case 'ArrowUp':
if (direction.y === 0) direction = { x: 0, y: -gridSize };
break;
case 'ArrowDown':
if (direction.y === 0) direction = { x: 0, y: gridSize };
break;
case 'ArrowLeft':
if (direction.x === 0) direction = { x: -gridSize, y: 0 };
break;
case 'ArrowRight':
if (direction.x === 0) direction = { x: gridSize, y: 0 };
break;
}
});
restartButton.addEventListener('click', initGame);
initGame();