-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathscript.js
More file actions
220 lines (183 loc) · 6.85 KB
/
script.js
File metadata and controls
220 lines (183 loc) · 6.85 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
const playBoard = document.querySelector(".play-board");
let foodX, foodY;
let snakeX = 5, snakeY = 10;
let snakeBody = [];
let velocityX = 0, velocityY = 0;
let score = 0;
let highScore = localStorage.getItem("highScore") || 0;
let gameInterval;
let GameOver = false;
let gameSpeed = 125; // Default game speed
let changingDirection = false;
// Function to change the food position to a random location
const changeFoodPosition = () => {
foodX = Math.floor(Math.random() * 30) + 1;
foodY = Math.floor(Math.random() * 30) + 1;
}
// Function to change the direction of the snake based on arrow keys
const changeDirection = (e) => {
// Prevent the snake from reversing direction
if (e.key === "ArrowUp" && velocityY !== 1 && changingDirection == false) {
velocityX = 0;
velocityY = -1;
changingDirection = true;
} else if (e.key === "ArrowDown" && velocityY !== -1 && changingDirection == false) {
velocityX = 0;
velocityY = 1;
changingDirection = true;
} else if (e.key === "ArrowRight" && velocityX !== -1 && changingDirection == false) {
velocityX = 1;
velocityY = 0;
changingDirection = true;
} else if (e.key === "ArrowLeft" && velocityX !== 1 && changingDirection == false) {
velocityX = -1;
velocityY = 0;
changingDirection = true;
}
}
// Function to display game over message and stop the game
const gameOver = () => {
clearInterval(gameInterval);
GameOver = true;
document.getElementById("finalScore").textContent = score;
document.getElementById("gameOverModal").style.display = "flex";
}
// Function to restart the game
const restartGame = () => {
// Reset game state
snakeX = 5;
snakeY = 10;
snakeBody = [];
velocityX = 0;
velocityY = 0;
score = 0;
GameOver = false;
// Update the score display
document.querySelector(".score").textContent = `Score: ${score}`;
document.querySelector(".highscore").textContent = `High Score: ${highScore}`;
// Hide the game over modal
document.getElementById("gameOverModal").style.display = "none";
// Show difficulty selection modal
document.getElementById("difficultyModal").style.display = "flex";
}
// Function to start the game based on selected difficulty
const startGame = (difficulty) => {
switch (difficulty) {
case 'easy':
gameSpeed = 125; // Slower speed for easy
break;
case 'medium':
gameSpeed = 100; // Medium speed
break;
case 'hard':
gameSpeed = 75; // Faster speed for hard
break;
case 'impossible':
gameSpeed = 30; // Faster speed for impossible
break;
}
// Hide the difficulty modal
document.getElementById("difficultyModal").style.display = "none";
// Show the mode modal
document.getElementById("wallsWrapModal").style.display = "block";
}
let gameWalls = false;
function changeModeWall() {
gameWalls = true;
console.log('wall');
document.getElementById("wallsWrapModal").style.display = "none";
// Initialize the food position
changeFoodPosition();
// Start the game with the selected speed
gameInterval = setInterval(initGame, gameSpeed);
}
function changeModeWrap() {
gameWalls = false;
console.log('wrap');
document.getElementById("wallsWrapModal").style.display = "none";
// Initialize the food position
changeFoodPosition();
// Start the game with the selected speed
gameInterval = setInterval(initGame, gameSpeed);
}
// Main function to update the game state
const initGame = () => {
// Save the current tail position
let tailX = snakeBody.length ? snakeBody[snakeBody.length - 1][0] : snakeX;
let tailY = snakeBody.length ? snakeBody[snakeBody.length - 1][1] : snakeY;
// Update snake's head position based on current velocity
snakeX += velocityX;
snakeY += velocityY;
// Check for border collision (game over condition)
if (gameWalls) {
if (snakeX < 1 || snakeX > 30 || snakeY < 1 || snakeY > 30) {
gameOver();
return;
}
} else {
if (snakeX < 1) {
snakeX = 30
velocityX = -1;
velocityY = 0;
}
if (snakeX > 30) {
snakeX = 1
velocityX = 1;
velocityY = 0;
}
if (snakeY < 1) {
snakeY = 30
velocityX = 0;
velocityY = -1;
}
if (snakeY > 30) {
snakeY = 1
velocityX = 0;
velocityY = 1;
}
}
// Check for self-collision (game over condition)
for (let i = 0; i < snakeBody.length; i++) {
if (snakeX === snakeBody[i][0] && snakeY === snakeBody[i][1]) {
gameOver();
return;
}
}
// Check if the snake has reached the food
if (snakeX === foodX && snakeY === foodY) {
changeFoodPosition(); // Move the food to a new position
// Add a new segment to the snake's body at the tail's previous position
snakeBody.push([tailX, tailY]);
score++;
if (score > highScore) {
highScore = score;
localStorage.setItem("highScore", highScore);
}
document.querySelector(".score").textContent = `Score: ${score}`;
document.querySelector(".highscore").textContent = `High Score: ${highScore}`;
}
// Move each part of the snake's body to follow the segment in front of it
for (let i = snakeBody.length - 1; i > 0; i--) {
snakeBody[i] = snakeBody[i - 1];
}
// Update the first segment of the snake's body to the previous head position
if (snakeBody.length > 0) {
snakeBody[0] = [snakeX - velocityX, snakeY - velocityY];
}
// Render the food and the snake's head on the grid
let htmlMarkup = `<div class="food" style='grid-area: ${foodY} / ${foodX}'></div>`;
htmlMarkup += `<div class="head" style='grid-area: ${snakeY} / ${snakeX}'></div>`;
// Render the snake's body on the grid
for (let i = 0; i < snakeBody.length; i++) {
htmlMarkup += `<div class="body" style='grid-area: ${snakeBody[i][1]} / ${snakeBody[i][0]}'></div>`;
}
changingDirection = false;
// Update the play board with the new positions
playBoard.innerHTML = htmlMarkup;
}
// Hide the mode modal on page load
document.getElementById("wallsWrapModal").style.display = "none";
// Show the difficulty selection modal on page load
document.getElementById("difficultyModal").style.display = "flex";
// Add event listener for keypress to change the snake's direction
document.addEventListener('keydown', changeDirection);