-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
143 lines (114 loc) · 4 KB
/
script.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
// Getting the reference of dom
// elements
const foodSound = new Audio('https://firebasestorage.googleapis.com/v0/b/fire-and-earn.appspot.com/o/Abhinabfile%2FAUD-20230118-WA0005.mp3?alt=media&token=02ebe836-9e18-4981-9736-601c9d80512e');
const gameOverSound = new Audio('https://firebasestorage.googleapis.com/v0/b/fire-and-earn.appspot.com/o/Abhinabfile%2FAUD-20230118-WA0003.mp3?alt=media&token=f08fb475-e60f-4d5e-a603-f96af1642573');
const moveSound = new Audio('https://firebasestorage.googleapis.com/v0/b/fire-and-earn.appspot.com/o/Abhinabfile%2FAUD-20230118-WA0004.mp3?alt=media&token=16f76da3-6ca9-4810-936a-80e452556855');
const musicSound = new Audio('https://firebasestorage.googleapis.com/v0/b/fire-and-earn.appspot.com/o/Abhinabfile%2FAUD-20230118-WA0006.mp3?alt=media&token=a44ec0c0-fe82-4144-bde1-e91e069c49e6');
let score = 0;
const btnUp = document.querySelector('#UP');
const btnDown = document.querySelector('#DOWN');
const btnLeft = document.querySelector('#LEFT');
const btnRight = document.querySelector('#RIGHT');
const board = document.querySelector(".snake-board");
// Variables
let up = false;
let down = false;
let left = false;
let right = false;
let speed = 6;
let lastPaintTime = 0;
// Snake body array
let snakeArr = [{ x: 14, y: 3 }];
let food = { x: Math.round(Math.random() * (26 - 1) + 1), y: Math.round(Math.random() * (26 - 1) + 1) };
// Direction
let inputDir = { x: 0, y: 0 };
// Main function for game loop
function main(ctime) {
window.requestAnimationFrame(main);
if ((ctime - lastPaintTime) / 1000 < 1 / speed) {
return;
}
lastPaintTime = ctime;
gameEngine();
}
function gameEngine() {
// musicSound.play();
// Updating the snake array
if (isCollide(snakeArr)) {
inputDir = { x: 0, y: 0 };
alert("Game over! To play again press any key");
snakeArr = [{ x: 14, y: 3 }];
gameOverSound.play();
musicSound.pause();
score = 0;
}
if (snakeArr[0].x === food.x && snakeArr[0].y === food.y) {
foodSound.play();
score += 1;
scoreBox.innerHTML = "score" + score;
snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y });
let a = 2;
let b = 20;
let j = 1;
while (snakeArr[j].x === food.x && snakeArr[j].y === food.y) {
food = { x: Math.round(Math.random() * (b - a) + a), y: Math.round(Math.random() * (b - a) + a) };
j++;
}
}
for (let i = snakeArr.length - 2; i >= 0; i--) {
snakeArr[i + 1] = { ...snakeArr[i] };
}
snakeArr[0].x += inputDir.x;
snakeArr[0].y += inputDir.y;
// Display snake head and food
board.innerHTML = "";
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("snake");
}
board.appendChild(snakeElement);
});
let foodElement = document.createElement("div");
foodElement.style.gridRowStart = food.y;
foodElement.style.gridColumnStart = food.x;
foodElement.classList.add("food");
board.appendChild(foodElement);
}
function isCollide(snakeArr) {
for (let i = 1; i < snakeArr.length; i++) {
if (snakeArr[i].x === snakeArr[0].x && snakeArr[i].y === snakeArr[0].y) {
return true;
}
}
if (snakeArr[0].x >= 28 || snakeArr[0].x <= 0 || snakeArr[0].y >= 28 || snakeArr[0].y <= 0) {
return true;
}
return false;
}
window.requestAnimationFrame(main);
btnUp.addEventListener("click", () => {
moveSound.play();
inputDir.x = 0;
inputDir.y = -1;
});
btnDown.addEventListener("click", () => {
moveSound.play();
inputDir.x = 0;
inputDir.y = 1;
});
btnLeft.addEventListener("click", () => {
moveSound.play();
inputDir.x = -1;
inputDir.y = 0;
});
btnRight.addEventListener("click", () => {
moveSound.play();
inputDir.x = 1;
inputDir.y = 0;
});