-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
207 lines (180 loc) · 6.58 KB
/
index.js
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
let direction = {x: 0, y: 0};
const foodSound = new Audio('music_food.mp3');
const gameOverSound = new Audio('music_gameover.mp3');
const moveSound = new Audio('music_move.mp3');
const musicSound = new Audio('music_music.mp3');
musicSound.play(); // Play the music only once at the start
let speed = 7;
let score = 0;
let lastPaintTime = 0;
let snakeArr = [{x: 13, y: 15}]; // Initial snake
let food = {x: 18, y: 15}; // Initial food position
let inputDir = {x: 0, y: 0}; // Snake's movement direction
// High Score initialization
let highscoreval = localStorage.getItem("HighScoreBox"); // Fetch highscore from localStorage
if (highscoreval === null) {
highscoreval = 0;
localStorage.setItem("HighScoreBox", JSON.stringify(highscoreval)); // Set initial high score
} else {
highscoreval = JSON.parse(highscoreval);
}
// Display the high score on the screen
document.getElementById('HighScoreBox').innerHTML = "HighScore: " + highscoreval;
// Game loop function
function main(cTime) {
window.requestAnimationFrame(main);
if ((cTime - lastPaintTime) / 1000 < 1 / speed) {
return;
}
lastPaintTime = cTime;
gameEngine();
}
function isCollide(snakeArr) {
// If the snake hits itself
for (let i = 1; i < snakeArr.length; i++) {
if (snakeArr[i].x === snakeArr[0].x && snakeArr[i].y === snakeArr[0].y) {
return true;
}
}
// If the snake hits the wall (dynamically based on the board's grid size)
const gridSizeX = 40; // Adjust based on actual grid size
const gridSizeY = 30;
if (snakeArr[0].x >= gridSizeX || snakeArr[0].x <= 0 || snakeArr[0].y >= gridSizeY || snakeArr[0].y <= 0) {
return true;
}
return false;
}
function gameEngine() {
// Collision detection
if (isCollide(snakeArr)) {
gameOverSound.play();
musicSound.pause();
inputDir = {x: 0, y: 0};
alert("Game over. Press any key to play again!");
// Check if current score is greater than highscore and update it
if (score > highscoreval) {
highscoreval = score;
localStorage.setItem("HighScoreBox", JSON.stringify(highscoreval)); // Update high score in localStorage
document.getElementById('HighScoreBox').innerHTML = "HighScore: " + highscoreval; // Update high score display
}
// Reset game variables
snakeArr = [{x: 13, y: 15}]; // Reset the snake
musicSound.play();
score = 0; // Reset score
document.getElementById('scoreBox').innerHTML = "Score: " + score; // Reset score display
return; // Exit the game engine to stop execution
}
// If the snake eats the food
if (snakeArr[0].y === food.y && snakeArr[0].x === food.x) {
foodSound.play();
score += 1; // Increment score
document.getElementById('scoreBox').innerHTML = "Score: " + score; // Update score display
// Add new segment to the snake at the front (growth)
snakeArr.unshift({x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y});
let a = 2;
let b = 30;
// Generate new random position for food, ensuring it's not on the snake
let newFoodPosition;
do {
newFoodPosition = {
x: Math.round(a + (b - a) * Math.random()),
y: Math.round(a + (b - a) * Math.random())
};
} while (snakeArr.some(segment => segment.x === newFoodPosition.x && segment.y === newFoodPosition.y));
food = newFoodPosition;
}
// Move the snake's body
for (let i = snakeArr.length - 1; i > 0; i--) {
snakeArr[i] = { ...snakeArr[i - 1] }; // Move each segment to the position of the previous one
}
// Move the snake's head
snakeArr[0].x += inputDir.x;
snakeArr[0].y += inputDir.y;
// Clear the board
const board = document.getElementById('board');
board.innerHTML = "";
// Display the snake
snakeArr.forEach((e, index) => {
let snakeElement = document.createElement('div');
snakeElement.style.gridRowStart = e.y;
snakeElement.style.gridColumnStart = e.x;
if (index === 0) {
snakeElement.classList.add('head');
} else {
snakeElement.classList.add('snakeBody'); // Use 'snakeBody' class as per CSS
}
board.appendChild(snakeElement);
});
// Display the food
let foodElement = document.createElement('div');
foodElement.style.gridRowStart = food.y;
foodElement.style.gridColumnStart = food.x;
foodElement.classList.add('food'); // Add a class for the food element
board.appendChild(foodElement);
}
// Initialize game loop
window.requestAnimationFrame(main);
// Add event listener for keyboard input
window.addEventListener('keydown', e => {
switch (e.key) {
case "ArrowUp":
if (inputDir.y !== 1) { // Prevent snake from reversing
inputDir.x = 0;
inputDir.y = -1;
moveSound.play();
}
break;
case "ArrowDown":
if (inputDir.y !== -1) {
inputDir.x = 0;
inputDir.y = 1;
moveSound.play();
}
break;
case "ArrowLeft":
if (inputDir.x !== 1) {
inputDir.x = -1;
inputDir.y = 0;
moveSound.play();
}
break;
case "ArrowRight":
if (inputDir.x !== -1) {
inputDir.x = 1;
inputDir.y = 0;
moveSound.play();
}
break;
default:
break;
}
});
// Add event listeners for mobile buttons
document.getElementById('upButton').addEventListener('click', () => {
if (inputDir.y !== 1) {
inputDir.x = 0;
inputDir.y = -1;
moveSound.play();
}
});
document.getElementById('downButton').addEventListener('click', () => {
if (inputDir.y !== -1) {
inputDir.x = 0;
inputDir.y = 1;
moveSound.play();
}
});
document.getElementById('leftButton').addEventListener('click', () => {
if (inputDir.x !== 1) {
inputDir.x = -1;
inputDir.y = 0;
moveSound.play();
}
});
document.getElementById('rightButton').addEventListener('click', () => {
if (inputDir.x !== -1) {
inputDir.x = 1;
inputDir.y = 0;
moveSound.play();
}
});