-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
192 lines (179 loc) · 6.06 KB
/
Copy pathindex.js
File metadata and controls
192 lines (179 loc) · 6.06 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
const grid = document.querySelector(".grid");
const startButton = document.getElementById("start");
const scoreDisplay = document.getElementById("score");
const width = 10;
let squares = [];
let currentSnake = [2, 1, 0];
let direction = 1;
let appleIndex = 0;
let score = 0;
let intervalTime = 500;
let speed = 0.9;
let timerId = 0;
function createGrid() {
//create 100 of these elements with a for loop
for (let i = 0; i < width * width; i++) {
//create element
const square = document.createElement("div");
//add styling to the element
square.classList.add("square");
//put the element into our grid
grid.appendChild(square);
//push it into a new squares array
squares.push(square);
}
}
createGrid();
currentSnake.forEach((index) => squares[index].classList.add("snake"));
squares[currentSnake[0]].classList.add("head-right");
// squares[currentSnake[currentSnake.length - 1]].classList.add("tail-right");
function startGame() {
//remove the snake
currentSnake.forEach((index) => squares[index].classList.remove("snake"));
//remove the apple
squares[appleIndex].classList.remove("apple");
squares[appleIndex].innerHTML = "";
clearInterval(timerId);
currentSnake = [2, 1, 0];
score = 0;
//re add new score to browser
scoreDisplay.textContent = score;
direction = 1;
generateApple();
//readd the class of snake to our new currentSnake
currentSnake.forEach((index) => squares[index].classList.add("snake"));
timerId = setInterval(move, intervalTime);
}
function move() {
if (
(currentSnake[0] + width >= width * width && direction === width) || //if snake has hit bottom
(currentSnake[0] % width === width - 1 && direction === 1) || //if snake has hit right wall
(currentSnake[0] % width === 0 && direction === -1) || //if snake has hit left wall
(currentSnake[0] - width < 0 && direction === -width) || //if snake has hit top
squares[currentSnake[0] + direction].classList.contains("snake")
)
return clearInterval(timerId);
// removeSnakeTail();
//remove last element from our currentSnake array
const tail = currentSnake.pop();
//remove styling from last element
squares[tail].classList.remove("snake");
removeSnakeHead();
//add square in direction we are heading
currentSnake.unshift(currentSnake[0] + direction);
//deal with snake head gets apple
if (squares[currentSnake[0]].classList.contains("apple")) {
//remove the class of apple
squares[currentSnake[0]].classList.remove("apple");
// remove the apple
squares[currentSnake[0]].innerHTML = "";
//grow our snake by adding class of snake to it
squares[tail].classList.add("snake");
console.log(tail);
//grow our snake array
currentSnake.push(tail);
console.log(currentSnake);
//generate new apple
generateApple();
//add one to the score
score++;
//display our score
scoreDisplay.textContent = score;
//speed up our snake
clearInterval(timerId);
console.log(intervalTime);
intervalTime = intervalTime * speed;
console.log(intervalTime);
timerId = setInterval(move, intervalTime);
}
//add styling so we can see it
squares[currentSnake[0]].classList.add("snake");
calculateSnakeHead();
// calculateSnakeTail();
}
function generateApple() {
do {
appleIndex = Math.floor(Math.random() * squares.length);
} while (
squares[appleIndex].classList.contains("snake") ||
squares[appleIndex].classList.contains("apple")
);
squares[appleIndex].classList.add("apple");
squares[appleIndex].innerHTML = "🍎";
}
generateApple();
function control(e) {
if (e.keyCode === 39) {
// if snake heading left return true
if (direction == -1) return true;
console.log("right pressed");
direction = 1;
squares[currentSnake[0]].classList.add("turn-right");
} else if (e.keyCode === 38) {
// if snake heading down return true
if (direction == +width) return true;
console.log("up pressed");
direction = -width;
squares[currentSnake[0]].classList.add("turn-up");
} else if (e.keyCode === 37) {
// if snake heading left return true
if (direction == +1) return true;
console.log("right pressed");
direction = -1;
squares[currentSnake[0]].classList.add("turn-left");
} else if (e.keyCode === 40) {
// if snake heading up return true
if (direction == -width) return true;
console.log("down pressed");
direction = +width;
squares[currentSnake[0]].classList.add("turn-down");
}
}
function calculateSnakeHead() {
if (direction === -width) {
squares[currentSnake[0]].classList.add("head-up");
}
if (direction === +1) {
squares[currentSnake[0]].classList.add("head-right");
}
if (direction === -1) {
squares[currentSnake[0]].classList.add("head-left");
}
if (direction === +width) {
squares[currentSnake[0]].classList.add("head-down");
}
}
function removeSnakeHead() {
squares[currentSnake[0]].classList.remove("head-left");
squares[currentSnake[0]].classList.remove("head-right");
squares[currentSnake[0]].classList.remove("head-down");
squares[currentSnake[0]].classList.remove("head-up");
}
// function calculateSnakeTail() {
// const tail = squares[currentSnake[currentSnake.length - 1]];
// if (tail.classList.contains("turn-left")) {
// tail.classList.remove("turn-left");
// tail.classList.add("tail-left");
// }
// if (tail.classList.contains("turn-right")) {
// tail.classList.remove("turn-right");
// tail.classList.add("tail-right");
// }
// if (tail.classList.contains("turn-up")) {
// tail.classList.remove("turn-up");
// tail.classList.add("tail-up");
// }
// if (tail.classList.contains("turn-down")) {
// tail.classList.remove("turn-down");
// tail.classList.add("tail-down");
// }
// }
// function removeSnakeTail() {
// const tail = squares[currentSnake[currentSnake.length - 1]];
// tail.classList.remove("tail-left");
// tail.classList.remove("tail-right");
// tail.classList.remove("tail-down");
// tail.classList.remove("tail-up");
// }
document.addEventListener("keyup", control);
startButton.addEventListener("click", startGame);