1
1
// ---- temp only for development ----
2
- let temp = '!!temp42!!' ;
2
+ let temp ;
3
+ temp = '!!temp42!!' ;
3
4
// ---- JS ----
4
5
let board ;
5
6
let blockSize = 25 % screen . height ;
@@ -15,9 +16,11 @@ let speedY = 0;
15
16
let snakeBody = [ ] ;
16
17
let gameOver = false ;
17
18
let gameRunning = false ;
18
- let RGArr = [ ] ;
19
19
let recentGame = [ ] ;
20
- let NowUsedRGArr = RGArr ;
20
+ let RGArr = [ ] ;
21
+ // let HSArr = [0,0,0,0];
22
+ // let newHighScoreWritten = false;
23
+
21
24
22
25
// ----- from/for HTML ----
23
26
let liveScore = document . getElementById ( "score" ) ;
@@ -40,10 +43,11 @@ let LiveHighScore = document.getElementById("highScore");
40
43
let LiveHighSeconds = document . getElementById ( "highSeconds" ) ;
41
44
let LiveHighSnakeLength = document . getElementById ( "highBodyLength" ) ;
42
45
let LiveHighFootEaten = document . getElementById ( "highColected" ) ;
43
- let highScore ;
44
- let highSeconds ;
45
- let highLength ;
46
- let highColected ;
46
+ let highScore = undefined ;
47
+ let highSeconds = undefined ;
48
+ let highLength = undefined ;
49
+ let highColected = undefined ;
50
+ let LSClearButton = document . getElementById ( "clearLSButton" ) ;
47
51
48
52
// --------------------------- End of Variables -------------------------------------------------------------
49
53
@@ -78,6 +82,11 @@ function restart() {
78
82
}
79
83
80
84
function start ( ) {
85
+ localStorageReader ( ) ;
86
+ updateHighScore ( ) ;
87
+ for ( let i = 0 ; i < RGArr . length ; i ++ ) {
88
+ recentGamesTableWriter ( i ) ;
89
+ }
81
90
document . getElementById ( "deathModal" ) . style . display = 'none' ;
82
91
document . getElementById ( "ModalButton" ) . hidden = false ;
83
92
board = document . getElementById ( "board" ) ;
@@ -89,7 +98,9 @@ function start () {
89
98
setInterval ( updates , 100 ) ;
90
99
setInterval ( secondsCounter , 1000 ) ;
91
100
setInterval ( sendToHTML , 1 ) ;
92
- setInterval ( sendToHTMLgameOver , 100 ) ;
101
+ setInterval ( updateHighScore , 1000 ) ;
102
+ setInterval ( hide , 1000 ) ;
103
+ setInterval ( localStorageWriter , 10000 ) ;
93
104
}
94
105
95
106
@@ -216,6 +227,7 @@ function moveUpdate() {
216
227
document . getElementById ( "ModalButton" ) . hidden = false ;
217
228
document . getElementById ( "deathModal" ) . style . display = 'block' ;
218
229
deleteRGTable ( ) ;
230
+ sendToHTMLgameOver ( ) ;
219
231
addRecentGame ( newScore , secondsCounted , bodyLength , collectedFood ) ;
220
232
for ( let i = 0 ; i < RGArr . length ; i ++ ) {
221
233
recentGamesTableWriter ( i ) ;
@@ -241,6 +253,7 @@ function sendToHTML() {
241
253
}
242
254
243
255
function sendToHTMLgameOver ( ) {
256
+ updateHighScore ( ) ;
244
257
score = newScore ;
245
258
seconds = secondsCounted ;
246
259
length = bodyLength ;
@@ -249,38 +262,56 @@ function sendToHTMLgameOver() {
249
262
finalScore . innerHTML = score ;
250
263
finalBodyLength . innerHTML = length ;
251
264
finalFood . innerHTML = food ;
252
- updateHighScore ( ) ;
253
265
}
254
266
255
267
function updateHighScore ( ) {
256
- if ( newScore + secondsCounted + bodyLength + collectedFood > highScore + highSeconds + highLength + highColected ) {
257
- highScore = newScore
258
- highSeconds = secondsCounted
259
- highLength = bodyLength
260
- highColected = collectedFood
261
- } else if ( RGArr . length === 2 ) {
262
- highScore = newScore
268
+ // if (HSArr.length < 4) {
269
+ // writeHighScore();
270
+ // }
271
+ if ( newScore + secondsCounted + bodyLength + collectedFood > highScore + highSeconds + highLength + highColected ) {
272
+ highScore = newScore ;
273
+ highSeconds = secondsCounted ;
274
+ highLength = bodyLength ;
275
+ highColected = collectedFood ;
276
+ } else if ( RGArr === [ 0 , 0 , 0 , 0 ] ) {
277
+ highScore = newScore ;
278
+ highSeconds = secondsCounted ;
279
+ highLength = bodyLength ;
280
+ highColected = collectedFood ;
281
+ }
282
+ if ( highScore === undefined ) {
283
+ highScore = newScore ;
263
284
highSeconds = secondsCounted
264
- highLength = bodyLength
265
- highColected = collectedFood
285
+ highLength = bodyLength ;
286
+ highColected = collectedFood ;
266
287
}
267
- LiveHighScore . innerHTML = highScore
268
- LiveHighSeconds . innerHTML = highSeconds
269
- LiveHighSnakeLength . innerHTML = highLength
270
- LiveHighFootEaten . innerHTML = highColected
288
+ LiveHighScore . innerHTML = highScore ;
289
+ LiveHighSeconds . innerHTML = highSeconds ;
290
+ LiveHighSnakeLength . innerHTML = highLength ;
291
+ LiveHighFootEaten . innerHTML = highColected ;
271
292
}
272
293
294
+ // function writeHighScore() {
295
+ // HSArr = JSON.parse(localStorage.getItem("LHSArr"));
296
+ // alert(HSArr)
297
+ // highScore = HSArr[0];
298
+ // highSeconds = HSArr[1];
299
+ // highLength = HSArr[2];
300
+ // highColected = HSArr[3];
301
+ //
302
+ // }
303
+
273
304
function recentGamesTableWriter ( i ) {
274
305
let table = document . getElementById ( "recentGamesTable" ) ;
275
306
let row = table . insertRow ( 1 ) ;
276
307
let cell1 = row . insertCell ( 0 ) ;
277
308
let cell2 = row . insertCell ( 1 ) ;
278
309
let cell3 = row . insertCell ( 2 ) ;
279
310
let cell4 = row . insertCell ( 3 ) ;
280
- cell1 . innerHTML = NowUsedRGArr [ i ] [ 0 ] . toString ( ) ;
281
- cell2 . innerHTML = NowUsedRGArr [ i ] [ 1 ] . toString ( ) ;
282
- cell3 . innerHTML = NowUsedRGArr [ i ] [ 2 ] . toString ( ) ;
283
- cell4 . innerHTML = NowUsedRGArr [ i ] [ 3 ] . toString ( ) ;
311
+ cell1 . innerHTML = RGArr [ i ] [ 0 ] . toString ( ) ;
312
+ cell2 . innerHTML = RGArr [ i ] [ 1 ] . toString ( ) ;
313
+ cell3 . innerHTML = RGArr [ i ] [ 2 ] . toString ( ) ;
314
+ cell4 . innerHTML = RGArr [ i ] [ 3 ] . toString ( ) ;
284
315
}
285
316
286
317
function addRecentGame ( score , seconds , length , colected ) {
@@ -294,4 +325,34 @@ function deleteRGTable () {
294
325
for ( let i = RGTable . rows . length ; i > 1 ; i -- ) {
295
326
RGTable . deleteRow ( - 1 )
296
327
}
328
+ }
329
+
330
+ function localStorageWriter ( ) {
331
+ localStorage . deleteArray ( RGArr ) ;
332
+ // localStorage.deleteArray(HSArr);
333
+ localStorage . setItem ( "LRGArr" , JSON . stringify ( RGArr ) ) ;
334
+ // localStorage.setItem("LHSArr", JSON.stringify(HSArr));
335
+ }
336
+ function localStorageReader ( ) {
337
+ RGArr = JSON . parse ( localStorage . getItem ( "LRGArr" ) ) ;
338
+ }
339
+
340
+ //---------------
341
+ Storage . prototype . deleteArray = function ( arrayName ) {
342
+ this . removeItem ( arrayName ) ;
343
+ }
344
+ //--------------
345
+
346
+
347
+ function hide ( ) {
348
+ if ( gameRunning ) {
349
+ LSClearButton . style . display = "none" ;
350
+ } else {
351
+ LSClearButton . style . display = "block" ;
352
+ }
353
+ }
354
+ LSClearButton . onclick = function ( ) {
355
+ localStorage . clear ( ) ;
356
+ deleteRGTable ( ) ;
357
+ RGArr = [ ] ;
297
358
}
0 commit comments