-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
112 lines (95 loc) · 3.1 KB
/
main.js
File metadata and controls
112 lines (95 loc) · 3.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
document.addEventListener("DOMContentLoaded", function () {
const canvas = document.getElementById("snakeCanvas");
const ctx = canvas.getContext("2d");
const scoreElement = document.getElementById("score");
const gameOverElement = document.getElementById("gameOver");
const box = 20;
let snake = [{ x: 0, y: 0 }];
let food = { x: 0, y: 0 };
let direction = "right";
let score = 0;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background
ctx.fillStyle = "#2c3e50";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
snake.forEach((segment) => {
ctx.fillStyle = "#27ae60";
ctx.fillRect(segment.x, segment.y, box, box);
ctx.strokeStyle = "#ecf0f1";
ctx.strokeRect(segment.x, segment.y, box, box);
});
// Draw food
ctx.fillStyle = "#e74c3c";
ctx.fillRect(food.x, food.y, box, box);
ctx.strokeStyle = "#ecf0f1";
ctx.strokeRect(food.x, food.y, box, box);
// Draw score
scoreElement.textContent = "Score: " + score;
// Move the snake
let newHead = { x: snake[0].x, y: snake[0].y };
if (direction === "right") newHead.x += box;
if (direction === "left") newHead.x -= box;
if (direction === "up") newHead.y -= box;
if (direction === "down") newHead.y += box;
snake.unshift(newHead);
// Check for collision with food
if (newHead.x === food.x && newHead.y === food.y) {
// Generate new food
food = {
x: Math.floor(Math.random() * (canvas.width / box)) * box,
y: Math.floor(Math.random() * (canvas.height / box)) * box,
};
// Increase the score
score++;
} else {
// Remove the last segment of the snake
snake.pop();
}
// Check for collision with walls or itself
if (
newHead.x < 0 ||
newHead.y < 0 ||
newHead.x >= canvas.width ||
newHead.y >= canvas.height ||
collision(newHead, snake.slice(1))
) {
clearInterval(game);
gameOverElement.style.display = "block";
}
}
function collision(head, array) {
return array.some(
(segment) => head.x === segment.x && head.y === segment.y
);
}
function changeDirection(event) {
const key = event.keyCode;
if (key === 37 && direction !== "right") {
direction = "left";
} else if (key === 38 && direction !== "down") {
direction = "up";
} else if (key === 39 && direction !== "left") {
direction = "right";
} else if (key === 40 && direction !== "up") {
direction = "down";
} else if (key === 32 && gameOverElement.style.display === "block") {
// Restart the game on Space key press after game over
resetGame();
}
}
function resetGame() {
snake = [{ x: 0, y: 0 }];
food = {
x: Math.floor(Math.random() * (canvas.width / box)) * box,
y: Math.floor(Math.random() * (canvas.height / box)) * box,
};
direction = "right";
score = 0;
gameOverElement.style.display = "none";
game = setInterval(draw, 150);
}
document.addEventListener("keydown", changeDirection);
let game = setInterval(draw, 150);
});