-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (142 loc) · 3.93 KB
/
script.js
File metadata and controls
157 lines (142 loc) · 3.93 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
let squares = Array.from(document.querySelectorAll(".grid div"));
const scoreDisplay = document.querySelector("#score");
const highScoreDisplay = document.querySelector("#highScore");
const startPosition = 250;
let snakeArray = [startPosition, 249, 248];
let direction = "";
let mousePosition = 258;
let score = 0;
let highScoreString = localStorage.getItem("highScore");
let highScore = JSON.parse(highScoreString) ?? 0; //sets to 0 if null (first run through)
let snakeLength = snakeArray.length;
let timeoutID;
let gameOverIndicator = 1; //will be set to 0 if game should end
let timer = 250; //time in ms
let snakeSpeed = $("#speed").val();
$("#speed").change(function () {
snakeSpeed = $(this).val();
if (snakeSpeed === "medium") {
timer = 250;
} else if (snakeSpeed === "slow") {
timer = 500;
} else if (snakeSpeed === "fast") {
timer = 125;
}
});
function draw() {
for (let i = 0; i < snakeArray.length; i++) {
squares[snakeArray[i]].classList.add("snake");
}
}
function undraw() {
for (let i = 0; i < snakeArray.length; i++) {
squares[snakeArray[i]].classList.remove("snake");
}
}
function drawMouse() {
squares[mousePosition].classList.add("mouse");
}
function undrawMouse() {
squares[mousePosition].classList.remove("mouse");
}
function eatMouse() {
if (snakeArray[0] === mousePosition) {
undrawMouse();
mousePosition = Math.floor(Math.random() * squares.length);
while (
squares[mousePosition].classList.contains("border") ||
squares[mousePosition].classList.contains("snake")
) {
mousePosition = Math.floor(Math.random() * squares.length);
}
drawMouse();
score += 10;
scoreDisplay.innerHTML = score;
let snakeTailCompare =
snakeArray[snakeLength - 2] - snakeArray[snakeLength - 1];
if (snakeTailCompare === 1) {
snakeArray.push(snakeArray[snakeLength - 1] - 1);
} else if (snakeTailCompare === -1) {
snakeArray.push(snakeArray[snakeLength - 1] + 1);
} else if (snakeTailCompare === 22) {
snakeArray.push(snakeArray[snakeLength - 1] - 22);
} else if (snakeTailCompare === -22) {
snakeArray.push(snakeArray[snakeLength - 1] + 22);
}
}
}
function moveRight() {
undraw();
snakeArray.unshift(snakeArray[0] + 1);
snakeArray.pop();
draw();
}
function moveUp() {
undraw();
snakeArray.unshift(snakeArray[0] - 22);
snakeArray.pop();
draw();
}
function moveDown() {
undraw();
snakeArray.unshift(snakeArray[0] + 22);
snakeArray.pop();
draw();
}
function moveLeft() {
undraw();
snakeArray.unshift(snakeArray[0] - 1);
snakeArray.pop();
draw();
}
document.addEventListener("keydown", control);
function control(e) {
if (e.keyCode === 37) {
direction = "left";
} else if (e.keyCode === 38) {
direction = "up";
} else if (e.keyCode === 39) {
direction = "right";
} else if (e.keyCode === 40) {
direction = "down";
}
}
function gameOver() {
for (let i = 1; i < snakeArray.length; i++) {
if (snakeArray[0] === snakeArray[i]) {
scoreDisplay.innerHTML = score + " Game Over!";
gameOverIndicator = 0;
if (score > highScore) {
localStorage.setItem("highScore", score);
highScoreDisplay.innerHTML = "New High Score! " + score;
}
} else if (squares[snakeArray[0]].classList.contains("border")) {
scoreDisplay.innerHTML = score + " Game Over!";
gameOverIndicator = 0;
if (score > highScore) {
localStorage.setItem("highScore", score);
highScoreDisplay.innerHTML = "New High Score! " + score;
}
}
}
}
function move() {
if (direction === "left") {
moveLeft();
} else if (direction === "up") {
moveUp();
} else if (direction === "right") {
moveRight();
} else if (direction === "down") {
moveDown();
}
eatMouse();
gameOver();
if (gameOverIndicator !== 0) {
timeoutID = setTimeout(move, timer);
}
}
highScoreDisplay.innerHTML = "High Score: " + highScore;
draw();
drawMouse();
move();