-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
187 lines (162 loc) · 4.69 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
// Canvas Specs
const block = 25;
const rows = 15;
const columns = 15;
var board;
var ctx;
// Snake Position
var snekX = Math.floor(Math.random() * columns) * block;
var snekY = Math.floor(Math.random() * rows) * block;
// Default Snake Velocity
var velocityX = 0;
var velocityY = 0;
// Misc
var snekBody = [];
var score = 0;
var alive = true;
window.onload = function () {
snekGame = document.getElementById("snekGame");
scoreTxt = document.getElementById("scoreTxt");
toggleViewModeBtn = document.getElementById("toggleDarkMode");
resizeCanvas();
ctx = snekGame.getContext("2d");
randomFud();
document.addEventListener("keydown", changeDir);
window.addEventListener("resize", resizeCanvas);
toggleViewModeBtn.addEventListener("click", toggleDarkMode);
setInterval(redraw, 100);
}
// Toggle dark mode
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
}
// Scale based on screensize
function resizeCanvas() {
snekGame.width = Math.min(window.innerWidth, columns * block);
snekGame.height = Math.min(window.innerHeight, rows * block);
}
function redraw() {
if (!alive) return;
ctx.clearRect(0, 0, snekGame.width, snekGame.height);
ctx.fillStyle = "red";
ctx.fillRect(fudX, fudY, block, block);
// Scoring
if (snekX == fudX && snekY == fudY) {
snekBody.push([fudX, fudY]);
score++;
scoreTxt.innerText = "Score: " + score;
randomFud();
}
// Snek movement & generation
for (let i = snekBody.length - 1; i > 0; i--) {
snekBody[i] = snekBody[i - 1];
}
if (snekBody.length) {
snekBody[0] = [snekX, snekY];
}
ctx.fillStyle = "green";
snekX += velocityX * block;
snekY += velocityY * block;
ctx.fillRect(snekX, snekY, block, block);
for (let i = 0; i < snekBody.length; i++) {
ctx.fillStyle = "lime";
ctx.fillRect(snekBody[i][0], snekBody[i][1], block, block);
}
// Death by out of bounds
if (snekX < 0 || snekX >= columns * block || snekY < 0 || snekY >= rows * block) {
alive = false;
displayGameOver();
return;
}
// Death by crashing into self
for (let i = 0; i < snekBody.length; i++) {
if (snekX == snekBody[i][0] && snekY == snekBody[i][1]) {
alive = false;
displayGameOver();
return;
}
}
}
// The popup for when the player dies
function displayGameOver() {
// Game over text
ctx.fillStyle = "black";
ctx.font = "50px AtkinsonHyperlegible";
var text = "Game Over";
var textWidth = ctx.measureText(text).width;
var x = (snekGame.width - textWidth) / 2;
var y = snekGame.height / 2;
ctx.fillText(text, x, y);
// Reset Button
if (!document.querySelector(".reset-button")) {
var resetButton = document.createElement("button");
resetButton.innerText = "Reset";
resetButton.className = "button reset-button";
document.body.appendChild(resetButton);
resetButton.addEventListener("click", resetGame);
}
}
function resetGame() {
// Reset specs
snekX = Math.floor(Math.random() * columns) * block;
snekY = Math.floor(Math.random() * rows) * block;
velocityX = 0;
velocityY = 0;
snekBody = [];
score = 0;
scoreTxt.innerText = "Score: " + score;
alive = true;
// Hide button
var resetButton = document.querySelector(".reset-button");
if (resetButton) {
resetButton.remove();
}
// Restart the game
randomFud();
redraw();
}
// Takes inputs and changes snek direction based on it
function changeDir(e) {
switch (e.code) {
case "ArrowUp":
if (velocityY != 1) {
velocityY = -1;
velocityX = 0;
}
break;
case "ArrowDown":
if (velocityY != -1) {
velocityY = 1;
velocityX = 0;
}
break;
case "ArrowLeft":
if (velocityX != 1) {
velocityX = -1;
velocityY = 0;
}
break;
case "ArrowRight":
if (velocityX != -1) {
velocityX = 1;
velocityY = 0;
}
break;
}
}
// Randomly positions the fud on the canvas
function randomFud() {
do {
fudX = Math.floor(Math.random() * columns) * block;
fudY = Math.floor(Math.random() * rows) * block;
} while (inSnek(fudX, fudY));
}
// Prevents food being spawned under the snek
function inSnek(x, y) {
for (let i = 0; i < snekBody.length; i++) {
if (snekBody[i][0] === x && snekBody[i][1] === y) {
return true;
}
}
return false;
}