-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (114 loc) · 3.42 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
import createSnake from "./createSnake.js";
import createGrid from "./createGrid.js";
import removeSnake from "./removeSnake.js";
import generateApples from "./generateApples.js";
import modalFunction from "./modalFunction.js";
const score = document.querySelector("#score");
const leftButton = document.querySelector(".leftButton");
const rightButton = document.querySelector(".rightButton");
const upButton = document.querySelector(".upButton");
const downButton = document.querySelector(".downButton");
const grid = document.querySelector(".grid");
const startBtn = document.querySelector("#start");
let squares = [];
let currentSnake = [3, 2, 1, 0];
let direction = 1;
let snakeSpeed = 350;
const width = 15;
const numOfFields = width * width;
let appleIndex = 0;
const closeModal = document.querySelector(".closeModalWindow");
const modal = document.querySelector("#myModal");
let moving = setInterval(control, snakeSpeed);
createGrid(grid, squares, numOfFields);
createSnake(currentSnake, squares);
appleIndex = generateApples(squares);
function startGame() {
removeSnake(currentSnake, squares);
squares[appleIndex].classList.remove("apple");
clearInterval(moving);
currentSnake = [3, 2, 1, 0];
direction = 1;
snakeSpeed = 350;
score.textContent = 0;
createSnake(currentSnake, squares);
appleIndex = generateApples(squares);
moving = setInterval(control, snakeSpeed);
modal.style.display = "none";
}
function move() {
if (
(currentSnake[0] + width > numOfFields && direction === width) ||
(currentSnake[0] - width < 0 && direction === -width) ||
(currentSnake[0] % width === width - 1 && direction === 1) ||
(currentSnake[0] % width === 0 && direction === -1) ||
squares[currentSnake[0] + direction].classList.contains("snake")
) {
modalFunction(score.textContent);
return clearInterval(moving);
}
const tail = currentSnake.pop();
squares[tail].className = "square";
currentSnake.unshift(currentSnake[0] + direction);
createSnake(currentSnake, squares);
if (squares[currentSnake[0]].classList.contains("apple")) {
squares[appleIndex].classList.remove("apple");
squares[tail].classList.add("snake");
currentSnake.push(tail);
createSnake(currentSnake, squares);
appleIndex = generateApples(squares);
score.textContent = parseInt(score.textContent) + 1;
clearInterval(moving);
speedUpSnake();
}
}
function speedUpSnake() {
clearInterval(moving);
snakeSpeed > 150 && (snakeSpeed -= snakeSpeed / 10);
moving = setInterval(control, snakeSpeed);
}
function control(event) {
if (event) {
switch (event.key) {
case "ArrowRight":
direction = 1;
break;
case "ArrowLeft":
direction = -1;
break;
case "ArrowDown":
direction = width;
break;
case "ArrowUp":
direction = -width;
break;
default:
return;
}
} else {
move();
}
}
// For closing modal
closeModal.onclick = function () {
modal.style.display = "none";
};
startBtn.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
leftButton.onclick = function (event) {
direction = -1;
};
rightButton.onclick = function (event) {
direction = 1;
};
upButton.onclick = function (event) {
direction = -width;
};
downButton.onclick = function (event) {
direction = width;
};
document.addEventListener("keydown", control);
startBtn.addEventListener("click", startGame);