-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
323 lines (278 loc) · 10.4 KB
/
script.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const length = 5 // lenght at the start of a game
let inputs = ["up"] // starting direction
let newPoint = true
let points = 0
let gameRunning = false
let startTime
let upKey = 87,
leftKey = 65,
downKey = 83,
rightKey = 68
function updateKeybindings(event, keyName) {
let pressedChar = event.keyCode
let target = document.getElementById(keyName)
target.innerHTML = event.key
target.blur()
eval(keyName + ' = ' + pressedChar)
}
function sanitiseDirection(newDir) {
let oldDir = inputs[inputs.length - 1]
let out
if (newDir !== oldDir) {
if (newDir === "up" && oldDir !== "down") {
out = "up"
} else if (newDir === "left" && oldDir !== "right") {
out = "left"
} else if (newDir === "down" && oldDir !== "up") {
out = "down"
} else if (newDir === "right" && oldDir !== "left") {
out = "right"
}
}
if (inputs.length < 3 && out !== undefined) {
inputs.push(out)
}
}
function keyboardControl(event) {
let char = event.keyCode
let dir
if (char === upKey) {
dir = "up"
} else if (char === leftKey) {
dir = "left"
} else if (char === downKey) {
dir = "down"
} else if (char === rightKey) {
dir = "right"
}
sanitiseDirection(dir)
}
swipeControl("swipable", (el, dir) => {
sanitiseDirection(dir)
})
function randNumZeroToMax(maximum) {
return Math.floor(Math.random() * Math.floor(maximum))
}
function clearField() {
for (let element of document.getElementsByClassName("field")) {
element.classList.remove("point")
element.classList.remove("snakebody")
element.classList.remove("snakehead")
element.checked = false
}
}
function createSnakeGame(fieldsize = 15, speed = 120) {
for (let rowcount = 0; rowcount < fieldsize; rowcount++) {
let row = document.createElement("DIV");
row.classList.add("row")
row.id = "r-" + rowcount
document.getElementById("snek").appendChild(row);
for (let colcount = 0; colcount < fieldsize; colcount++) {
let check = document.createElement("INPUT")
check.type = "checkbox"
check.id = "r-" + rowcount + "ch-" + colcount
check.classList.add("field")
check.classList.add("colored")
check.onchange = function () {
if (!gameRunning) {
gameRunning = true
startTime = new Date().getTime()
}
document.getElementById("timedisplay").innerHTML = displayTime(startTime)
function displayTime(sTime, eTime = new Date().getTime(), precise = false) {
let difference = (eTime - sTime)
let minutes = Math.floor(difference / (1000 * 60))
let seconds = Math.floor((difference % (1000 * 60)) / 1000)
let decisec = Math.floor((difference % 1000) / 100)
let millisec = Math.floor(difference % 1000)
let elapsedTime
if (precise) {
elapsedTime = leftFillNum(minutes, 2) + ":" + leftFillNum(seconds, 2) + "." + millisec
} else {
elapsedTime = leftFillNum(minutes, 2) + ":" + leftFillNum(seconds, 2) + "." + decisec
}
return elapsedTime
}
function death() {
let t = displayTime(startTime, new Date().getTime(), true)
document.getElementById("timedisplay").innerHTML = t
gameRunning = false
clearField()
newPoint = true
displaySavedHighscore()
alert(`Game Over \n\nYou got ${points} Points in a time of ${t}`)
points = 0
document.getElementById("point-counter").innerHTML = "Points: " + points
}
//snake head
setTimeout(() => {
this.blur()
let noloop = document.getElementById("noloop").checked
let vert
let horiz
switch (inputs[0]) {
case "up":
if (noloop) {
vert = rowcount - 1
} else {
vert = fieldsize + rowcount - 1
}
horiz = colcount
break
case "left":
vert = rowcount
if (noloop) {
horiz = colcount - 1
} else {
horiz = fieldsize + colcount - 1
}
break
case "down":
vert = rowcount + 1
horiz = colcount
break
case "right":
vert = rowcount
horiz = colcount + 1
break
}
if (inputs.length > 1) {
inputs.shift()
}
this.classList.add("snakebody")
this.classList.remove("snakehead")
let nextHead = document.getElementById("r-" + (vert % fieldsize) + "ch-" + (horiz % fieldsize))
if (noloop) {
nextHead = document.getElementById("r-" + (vert) + "ch-" + (horiz))
}
if (nextHead === undefined || (nextHead.checked === true && !(nextHead.classList.contains("point")))) {
death()
return
} else if (nextHead.classList.contains("point")) {
nextHead.classList.remove("point")
nextHead.checked = false
newPoint = true
points++
document.getElementById("point-counter").innerHTML = "Points: " + points
}
nextHead.classList.add("snakehead")
nextHead.click()
}, speed)
//snake tail deletion
setTimeout(() => {
this.checked = false
this.classList.remove("snakebody")
}, speed * (points + length))
setTimeout(() => {
this.classList.remove("snakehead")
}, speed)
// point spawning
while (newPoint) {
let nextPoint = document.getElementById("r-" + randNumZeroToMax(fieldsize) + "ch-" + randNumZeroToMax(fieldsize))
if (!(nextPoint.classList.contains("snakebody") || nextPoint.classList.contains("snakehead"))) {
nextPoint.classList.add("point")
nextPoint.checked = true
newPoint = false
}
}
} // onchange end
document.getElementById("r-" + rowcount).appendChild(check)
}
}
}
//document.getElementById("r-" + Math.floor(fieldsize/2) + "ch-" + Math.floor(fieldsize/2)).click()
function leftFillNum(num, targetLength) {
return num.toString().padStart(targetLength, 0)
}
function restyle() {
for (let box of document.getElementsByClassName("field")) {
if (box.classList.contains("colored")) {
box.classList.remove("colored")
} else {
box.classList.add("colored")
}
}
}
let autoclose
function openOptions() {
clearTimeout(autoclose)
document.getElementById("options").style = "height: fit-content;"
autoclose = setTimeout(closeOptions, 15000)
}
function closeOptions() {
clearTimeout(autoclose)
document.getElementById("options").style = "height: 0; padding: 0 5px;"
}
function displaySavedHighscore() {
if (typeof (Storage) !== "undefined") {
if (localStorage.getItem("highscore") === null) {
localStorage.setItem("highscore", 0)
}
if (points > localStorage.getItem("highscore")) {
localStorage.setItem("highscore", points)
}
document.getElementById("high").innerHTML = "Highscore: " + localStorage.getItem("highscore")
} else {
let nostoragehigh
if (points > nostoragehigh) {
nostoragehigh = points
}
document.getElementById("high").innerHTML = "Highscore: " + nostoragehigh
}
}
// Tune deltaMin according to your needs. Near 0 it will almost
// always trigger, with a big value it can never trigger.
function swipeControl(id, func, deltaMin = 30) {
const swipe_det = {
sX: 0,
sY: 0,
eX: 0,
eY: 0
}
// Directions enumeration
const directions = Object.freeze({
UP: "up",
DOWN: "down",
RIGHT: "right",
LEFT: "left"
})
let direction = null
const el = document.getElementById(id)
el.addEventListener(
"touchstart",
function (e) {
const t = e.touches[0]
swipe_det.sX = t.screenX
swipe_det.sY = t.screenY
},
false
)
el.addEventListener(
"touchmove",
function (e) {
// Prevent default will stop user from scrolling, use with care
e.preventDefault()
const t = e.touches[0]
swipe_det.eX = t.screenX
swipe_det.eY = t.screenY
},
false
)
el.addEventListener(
"touchend",
function (e) {
const deltaX = swipe_det.eX - swipe_det.sX
const deltaY = swipe_det.eY - swipe_det.sY
// Min swipe distance
if (deltaX ** 2 + deltaY ** 2 < deltaMin ** 2) return
// horizontal
if (deltaY === 0 || Math.abs(deltaX / deltaY) > 1)
direction = deltaX > 0 ? directions.RIGHT : directions.LEFT
// vertical
else direction = deltaY > 0 ? directions.DOWN : directions.UP
if (direction && typeof func === "function") func(el, direction)
direction = null
},
false
)
}