-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.js
177 lines (152 loc) · 5.49 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
let board = document.getElementById('board')
let scoreCont = document.getElementById('score')
let maxScoreCont = document.getElementById('maxScoreCont');
let HeadEle;
// console.log(HeadEle);
let inputDir = { 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');
let speed = 5;
let lastPaintTime = 0;
let snakeArr = [
{ x: 13, y: 15 }
]
let food = {
x: 6, y: 7
};
// Game Functions
function main(ctime) {
window.requestAnimationFrame(main);
if ((ctime - lastPaintTime) / 1000 < (1 / speed)) {
return;
}
// console.log(ctime);
lastPaintTime = ctime;
gameEngine();
// console.log(ctime);
}
function isCollide(snake) {
// return false;
//if you into yourself
if (snake[0].x > 18 || snake[0].x < 0 || snake[0].y > 18 || snake[0].y < 0) {
return true;
}
}
function gameEngine() {
//part1: updating the snake array and food
if (isCollide(snakeArr)) {
gameOverSound.play();
musicSound.pause();
inputDir = { x: 0, y: 0 };
alert("Game over. Press any key to play again");
snakeArr = [{ x: 13, y: 15 }];
// musicSound.play();
}
//IF you have eaten the food, increment the score and regenerate the food
if (snakeArr[0].y === food.y && snakeArr[0].x === food.x) {
// console.log("food")
foodSound.play();
snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y });
// console.log(snakeArr)
let a = 2;
let b = 16;
food = { x: 2 + Math.round(a + (b - a) * Math.random()), y: Math.round(a + (b - a) * Math.random()) }
}
//Moving the snake
// console.log("-----")
// console.log(snakeArr.l)
for (let i = snakeArr.length - 2; i >= 0; i--) {
// const element = array[i];
// console.log("hello");
snakeArr[i + 1] = { ...snakeArr[i] };
// console.log(snakeArr[i + 1].x);
}
snakeArr[0].x += inputDir.x;
snakeArr[0].y += inputDir.y;
//part2: display the snake and food
//display the snake
board.innerHTML = "";
snakeArr.forEach((e, index) => {
snakeElement = document.createElement('div');
snakeElement.style.gridRowStart = e.y;
snakeElement.style.gridColumnStart = e.x;
if (index === 0) {
eyes = document.createElement('div');
eyes.classList.add('eyes');
eyes2 = document.createElement('div');
eyes2.classList.add('eyes');
snakeElement.classList.add('head');
// HeadEle = document.querySelectorAll('.head');
// console.log(e.x, e.y, typeof e.x, typeof e.y)
if (inputDir.x === 0 && inputDir.y === -1) {
snakeElement.style.setProperty('--top', '15%');
snakeElement.style.setProperty('--bottom', '75%');
snakeElement.style.setProperty('--left', '2%');
snakeElement.style.setProperty('--right', '2%');
snakeElement.style.setProperty('--direction', 'row');
}
else if (inputDir.x === 0 && inputDir.y === 1) {
snakeElement.style.setProperty('--top', '75%');
snakeElement.style.setProperty('--bottom', '15%');
snakeElement.style.setProperty('--left', '2%');
snakeElement.style.setProperty('--right', '2%');
snakeElement.style.setProperty('--direction', 'row');
}
else if (inputDir.x === -1 && inputDir.y === 0) {
snakeElement.style.setProperty('--top', '2%');
snakeElement.style.setProperty('--bottom', '2%');
snakeElement.style.setProperty('--left', '15%');
snakeElement.style.setProperty('--right', '75%');
snakeElement.style.setProperty('--direction', 'column');
}
else if (inputDir.x === 1 && inputDir.y === 0) {
snakeElement.style.setProperty('--top', '2%');
snakeElement.style.setProperty('--bottom', '2%');
snakeElement.style.setProperty('--left', '75%');
snakeElement.style.setProperty('--right', '15%');
snakeElement.style.setProperty('--direction', 'column');
}
board.appendChild(snakeElement);
snakeElement.appendChild(eyes);
snakeElement.appendChild(eyes2);
}
else {
snakeElement.classList.add('snake');
board.appendChild(snakeElement)
}
})
//part2: display the snake
foodElement = document.createElement('div');
foodElement.style.gridRowStart = food.y;
foodElement.style.gridColumnStart = food.x;
foodElement.classList.add('food');
board.appendChild(foodElement);
}
//Main logic starts here
window.requestAnimationFrame(main);
window.addEventListener('keydown', e => {
// inputDir = { x: 0, y: 1 } //start the game
moveSound.play();
switch (e.key) {
case "ArrowUp":
inputDir.x = 0;
inputDir.y = -1;
break;
case "ArrowDown":
inputDir.x = 0;
inputDir.y = 1;
break;
case "ArrowLeft":
inputDir.x = -1;
inputDir.y = 0;
break;
case "ArrowRight":
inputDir.x = 1;
inputDir.y = 0;
break;
default:
break;
}
});