-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
809 lines (671 loc) · 26.7 KB
/
Copy pathscript.js
File metadata and controls
809 lines (671 loc) · 26.7 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
window.addEventListener('load', () => {
window.scrollTo(0, 0);
});
const audio = new Audio('asset/theme.mp3');
audio.loop = true;
if (/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)) {
document.write('<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, orientation=portrait">');
}
const closeBtn = document.querySelector('.close-btn');
closeBtn.addEventListener('click', () => {
// Memeriksa apakah browser merupakan perangkat mobile
if (window.innerWidth <= 768) {
alert('Gunakan desktop untuk pengalaman lebih baik.');
return;
}
});
closeBtn.addEventListener('click', () => {
const element = document.documentElement;
/*
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
} */
window.scrollTo(0, 0);
});
function showPopup() {
document.getElementById("welcome-popup").style.display = "flex";
}
function closePopup() {
document.getElementById("welcome-popup").style.display = "none";
}
function updateBg() {
// Mendapatkan elemen body
const body = document.querySelector('body');
// Mendapatkan waktu saat ini
// Mendapatkan elemen HTML dengan id "clock"
const clock = document.querySelector('#clock');
// Mendapatkan waktu dari elemen HTML tersebut
const currentTime = parseInt(clock.textContent);
// Menentukan gambar latar belakang berdasarkan waktu
let backgroundImage;
if (currentTime >= 6 && currentTime < 12) {
backgroundImage = "asset/background1.jpg";
} else if (currentTime >= 12 && currentTime < 18) {
backgroundImage = "asset/background2.jpg";
} else {
backgroundImage = "asset/background3.jpg";
}
// Mengubah background-image pada body
body.style.backgroundImage = `url(${backgroundImage})`;
}
setInterval(updateBg, 10);
/*//--------------------------------------------------------------------------------------
var hours = localStorage.getItem("hours") || new Date().getHours();
var minutes = localStorage.getItem("minutes") || new Date().getMinutes();
var level = 1;
function updateTime() {
if (new Date().getSeconds() % 1 === 0) {
minutes++;
}
if (minutes % 60 === 0 && new Date().getSeconds() % 1 === 0) {
hours++;
}
var timeString = padNumber(hours % 24) + ":" + padNumber(minutes % 60);
document.getElementById("clock").innerHTML = timeString;
var myInputValue = localStorage.getItem('myInputValue');
document.getElementById("greetpopup").innerHTML = myInputValue + ', Selamat Datang, Jaga peliharaanmu agar tetap hidup untuk menang!';
if(hours % 24 >=23){
document.getElementById("greeting").innerHTML = 'Waktunya Tidur, ' + myInputValue;
}else if(hours % 24>=18){
document.getElementById("greeting").innerHTML = 'Selamat Malam, ' + myInputValue;
}else if(hours % 24>=15){
document.getElementById("greeting").innerHTML = 'Selamat Sore, ' + myInputValue;
}else if(hours % 24>=11){
document.getElementById("greeting").innerHTML ='Selamat Siang, ' + myInputValue;
}else if(hours % 24>=6){
document.getElementById("greeting").innerHTML = 'Selamat Pagi, ' + myInputValue;
}else{
document.getElementById("greeting").innerHTML ='Waktunya Tidur, ' + myInputValue;
}
if(hours > 24){
var level = 3;
}else if(hours > 16){
var level = 2;
}else{
var level = 1;
}
localStorage.setItem("level", level);
localStorage.setItem("hours", hours);
localStorage.setItem("minutes", minutes);
}
function padNumber(number) {
return ("0" + number).slice(-2);
}
setInterval(updateTime, 1000);
*/
var sumberGambar = ["asset/baby_spongebob.png", "asset/baby_patrick.png", "asset/baby_squidward.png"];
var indeksGambar = 0;
var elemenGambar = document.getElementById("character");
function gantiGambar(jumlah) {
indeksGambar = (indeksGambar + jumlah + sumberGambar.length) % sumberGambar.length;
elemenGambar.src = sumberGambar[indeksGambar];}
function moveInput() {
const input = document.getElementById('petnameid');
localStorage.setItem('myInputValue', input.value);
localStorage.setItem("chartype", indeksGambar);
window.location.href = './main.html';
}
// Check if we are on file2.html
if (window.location.href.includes('./main.html')) {
// Get the input value from localStorage
const inputValue = localStorage.getItem('myInputValue');
localStorage.removeItem("hours");
localStorage.removeItem("minutes");
localStorage.removeItem("hoursplayed");
localStorage.removeItem("gamemins");
localStorage.removeItem("countgame");
// Set the text content of the container to the saved input value
const inputContainer = document.getElementById('myInputContainer');
inputContainer.textContent = inputValue;
}
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
var hours = localStorage.getItem("hours") || new Date().getHours();
var minutes = localStorage.getItem("minutes") || new Date().getMinutes();
var gamemins = localStorage.getItem("gamemins") || 0;
var hoursplayed = localStorage.getItem("hoursplayed") || 0;
var level = 1;
function updateTime() {
// Menambahkan 1 menit setelah 1 detik berganti
if (new Date().getSeconds() % 1 === 0) {
minutes++;
}
// Menambahkan 1 jam setelah 59 menit berganti
if (minutes % 60 === 0 && new Date().getSeconds() % 1 === 0) {
hours++;
}
gamemins++;
if(gamemins % 60 === 0){
hoursplayed++;
}
// Menampilkan waktu pada halaman
var timeString = padNumber(hours % 24) + ":" + padNumber(minutes % 60);
document.getElementById("clock").innerHTML = timeString;
var myInputValue = localStorage.getItem('myInputValue');
document.getElementById("greetpopup").innerHTML = myInputValue + ', Selamat Datang, Jaga peliharaanmu agar tetap hidup untuk menang!';
if(hours % 24 >=23){
document.getElementById("greeting").innerHTML = 'Waktunya Tidur, ' + myInputValue;
}else if(hours % 24>=18){
document.getElementById("greeting").innerHTML = 'Selamat Malam, ' + myInputValue;
}else if(hours % 24>=15){
document.getElementById("greeting").innerHTML = 'Selamat Sore, ' + myInputValue;
}else if(hours % 24>=11){
document.getElementById("greeting").innerHTML ='Selamat Siang, ' + myInputValue;
}else if(hours % 24>=6){
document.getElementById("greeting").innerHTML = 'Selamat Pagi, ' + myInputValue;
}else{
document.getElementById("greeting").innerHTML ='Waktunya Tidur, ' + myInputValue;
}
localStorage.setItem("hours", hours);
localStorage.setItem("minutes", minutes);
if(hoursplayed > 16){
var level = 3;
}else if(hoursplayed > 8){
var level = 2;
}else{
var level = 1;
}
localStorage.setItem("level", level);
document.getElementById("levell").innerHTML = "Level : " + level;
localStorage.setItem("hoursplayed", hoursplayed);
localStorage.setItem("gamemins", gamemins);
// Menyimpan nilai hours dan minutes ke localStorage
}
function padNumber(number) {
return ("0" + number).slice(-2);
}
setInterval(updateTime, 1000); //10000
//--------------------------------------------------------------------------------------
let food = 5;
let sleep = 5;
let health = 5;
let play = 5;
let intervalId;
function startchar(){
var typechar = localStorage.getItem("chartype");
if (typechar == 0){
document.getElementById("char").innerHTML = "<img src='asset/baby_spongebob.png'>";
} else if (typechar == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_patrick.png'>";
} else{
document.getElementById("char").innerHTML = "<img src='asset/baby_squidward.png'>";
}
}
startchar();
document.getElementById("atas").addEventListener("click", function(){
var event = new KeyboardEvent('keydown', {'keyCode':38});
document.dispatchEvent(event);
});
document.getElementById("bawah").addEventListener("click", function(){
var event = new KeyboardEvent('keydown', {'keyCode':40});
document.dispatchEvent(event);
});
document.getElementById("kiri").addEventListener("click", function(){
var event = new KeyboardEvent('keydown', {'keyCode':37});
document.dispatchEvent(event);
});
document.getElementById("kanan").addEventListener("click", function(){
var event = new KeyboardEvent('keydown', {'keyCode':39});
document.dispatchEvent(event);
});
function updateFoodBar() {
const bar = document.getElementById("bar-food");
bar.style.width = (food * 10) + "%";
if (food <= 3) {
bar.classList.add("low");
} else {
bar.classList.remove("low");
}
}
function updateSleepBar() {
const bar = document.getElementById("bar-sleep");
bar.style.width = (sleep * 10) + "%";
if (sleep <= 3) {
bar.classList.add("low");
} else {
bar.classList.remove("low");
}
}
function updateHealthBar() {
const bar = document.getElementById("bar-health");
bar.style.width = (health * 10) + "%";
if (health <= 3) {
bar.classList.add("low");
} else {
bar.classList.remove("low");
}
}
function updatePlayBar() {
const bar = document.getElementById("bar-play");
bar.style.width = (play * 10) + "%";
if (play <= 3) {
bar.classList.add("low");
} else {
bar.classList.remove("low");
}
}
function reduceSleep() {
sleep--;
if (sleep <= 0) {
sleep=1;
}
updateSleepBar();
}
function reduceFood() {
food--;
if (food <= 0) {
food=1;
}
updateFoodBar();
}
function reduceHealth() {
if(food === 1 || sleep === 1 || play === 1){
health--;
}
if (health <= 0) {
alert("Game Over");
localStorage.clear();
window.location.href = './welcome.html';
}
updateHealthBar();
}
function reducePlay() {
play--;
if (play <= 0) {
play=1;
}
updatePlayBar();
}
function startTimer() {
intervalId = setInterval(reduceFood, 17000); //bbalikin ke 7000
intervalId = setInterval(reduceSleep, 20000); //60000
intervalId = setInterval(reduceHealth, 30000);//15000
intervalId = setInterval(reducePlay, 45000);//15000
}
document.getElementById("health-btn").addEventListener("click", function() {
var typechar = localStorage.getItem("chartype");
var currlevel = localStorage.getItem("level");
//spongebob
if(typechar == 0){
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob1 harusnya heal
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_spongebob.png'>"; //spongebob1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob2eat
}
}, 3000);
}
//patrick
else if(typechar == 1){
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick1 harusnya heal
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_patrick.png'>"; //patrick1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick3eat
}
}, 3000);
}
//squidward
else{
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward1 harusnya heal
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_squidward.png'>"; //squidward1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward3eat
}
}, 3000);
}
health++;
if (health > 10) {
health = 10;
}
updateHealthBar();
});
//charlv1 = 0
//charlv2 = 1
//charlv3 = 2
document.getElementById("food-btn").addEventListener("click", function() {
var typechar = localStorage.getItem("chartype");
var currlevel = localStorage.getItem("level");
//spongebob
if(typechar == 0){
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen-eat.gif'>"; //spongebob1
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen-eat.gif'>"; //spongebob2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen-eat.gif'>"; //spongebob3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_spongebob.png'>"; //spongebob1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-adult.gif'>"; //spongebob2eat
}
}, 3000);
}
//patrick
else if(typechar == 1){
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-teen-eat.gif'>"; //patrick1
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-teen-eat.gif'>"; //patrick2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-teen-eat.gif'>"; //patrick3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_patrick.png'>"; //patrick1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick3eat
}
}, 3000);
}
//squidward
else{
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-eat.gif'>"; //squidward1
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-eat.gif'>"; //squidward2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-eat.gif'>"; //squidward3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_squidward.png'>"; //squidward1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward3eat
}
}, 3000);
}
food++;
if (food > 10) {
food = 10;
}
updateFoodBar();
});
document.getElementById("sleep-btn").addEventListener("click", function() {
var typechar = localStorage.getItem("chartype");
var currlevel = localStorage.getItem("level");
//spongebob
if(typechar == 0){
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen-sleep.gif'>"; //spongebob1
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen-sleep.gif'>"; //spongebob2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen-sleep.gif'>"; //spongebob3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_spongebob.png'>"; //spongebob1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-spongebob-teen.gif'>"; //spongebob2eat
}
}, 3000);
}
//patrick
else if(typechar == 1){
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-teen-sleep.gif'>"; //patrick1
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-teen-sleep.gif'>"; //patrick2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-teen-sleep.gif'>"; //patrick3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_patrick.png'>"; //patrick1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-patrick-play.gif'>"; //patrick3eat
}
}, 3000);
}
//squidward
else{
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward1 hrsnya sleep
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward2
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward3
}
setTimeout(function() {
if (currlevel == 1){
document.getElementById("char").innerHTML = "<img src='asset/baby_squidward.png'>"; //squidward1eat
}else if(currlevel == 2){
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward2eat
}else{
document.getElementById("char").innerHTML = "<img src='asset/gif-squidward-play.gif'>"; //squidward3eat
}
}, 3000);
}
sleep++;
if (sleep > 10) {
sleep = 10;
}
updateSleepBar();
});
startTimer();
//-------------------------------------------------------------------------------------------------------------
let count = 0;
document.getElementById("play-btn").addEventListener("click", function() {
if (sleep <=3 || food <= 3){
var myInputValue = localStorage.getItem('myInputValue');
alert(myInputValue + " kekurangan energi untuk bermain. beri dia makan atau beri dia waktu untuk tidur!");
}else{
if (count == 0 ){
document.getElementById("score").style.display = "block";
document.getElementById("table").style.display = "block";
document.getElementById("char").style.display = "none";
document.getElementById("buttoncont").style.display = "none";
document.getElementById("greetingcontainer").style.display = "none";
document.getElementById("controller").style.display = "block";
count++;
minigame();
} else {
document.getElementById("score").style.display = "block";
document.getElementById("table").style.display = "block";
document.getElementById("char").style.display = "none";
document.getElementById("buttoncont").style.display = "none";
document.getElementById("greetingcontainer").style.display = "none";
document.getElementById("controller").style.display = "block";
}
}
}
);
//--------------------------------------------------------------------------------------------------------------------
function minigame(){
function reset(){
clearInterval(gameLoop);
snake = [{ row: 4, col: 4 }];
foods = [];
for (let i = 0; i < 3; i++) {
foods.push({ row: Math.floor(Math.random() * ROWS), col: Math.floor(Math.random() * COLS) });
}
dx = 1;
dy = 0;
score = 0;
document.getElementById("score").innerText = "Score: " + score; // Menambahkan baris ini
clearTable();
drawFoods();
drawSnake();
gameLoop = setInterval(update, 401);
}
const ROWS = 7;
const COLS = 15;
const table = document.getElementById("table");
let snake = [{ row: 4, col: 4 }];
let foods = [];
for (let i = 0; i < 3; i++) {
foods.push({ row: Math.floor(Math.random() * ROWS), col: Math.floor(Math.random() * COLS) });
}
let dx = 1;
let dy = 0;
let score = 0;
function drawTable() {
let html = "";
for (let row = 0; row < ROWS; row++) {
html += "<tr>";
for (let col = 0; col < COLS; col++) {
html += `<td id="cell-${row}-${col}"></td>`;
}
html += "</tr>";
}
table.innerHTML = html;
}
function drawSnake() {
snake.forEach((part) => {
const cell = document.getElementById(`cell-${part.row}-${part.col}`);
cell.classList.add("snake");
});
}
function drawFoods() {
foods.forEach((food) => {
const cell = document.getElementById(`cell-${food.row}-${food.col}`);
cell.classList.add("food");
});
}
function addFood() {
const newFood = { row: Math.floor(Math.random() * ROWS), col: Math.floor(Math.random() * COLS) };
foods.push(newFood);
}
function moveSnake() {
const head = { row: snake[0].row + dy, col: snake[0].col + dx };
if (head.col < 0) {
head.col = COLS - 1;
} else if (head.col >= COLS) {
head.col = 0;
}
if (head.row < 0) {
head.row = ROWS - 1;
} else if (head.row >= ROWS) {
head.row = 0;
}
foods.forEach((food, index) => {
if (head.row === food.row && head.col === food.col) {
foods.splice(index, 1);
score++;
document.getElementById("score").innerText = "Score: " + score;
}
});
snake.pop();
snake.unshift(head);
if (foods.length === 0) {
addFood();
}
const headCell = document.getElementById(`cell-${head.row}-${head.col}`);
if (headCell.classList.contains("food")) {
headCell.classList.remove("food");
}
}
function clearTable() {
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const cell = document.getElementById(`cell-${row}-${col}`);
cell.classList.remove("snake", "food");
}
}
}
function update() {
clearTable();
drawFoods();
moveSnake();
drawSnake();
if (score === 5) {
clearInterval(gameLoop); // stop the game loop
alert("Selamat!");
document.getElementById("score").style.display = "none"; // hide the score
document.getElementById("table").style.display = "none"; // hide the table
document.getElementById("char").style.display = "block";
document.getElementById("buttoncont").style.display = "block";
document.getElementById("greetingcontainer").style.display = "none";
document.getElementById("controller").style.display = "none";
play++;
play++;
play++;
play++;
play++;
play++;
if (play > 10) {
play = 10;
}
updatePlayBar();
reset();
}
}
function onKeyDown(event) {
if (event.keyCode === 37 && dx === 0) {
dx = -1;
dy = 0;
} else if (event.keyCode === 38 && dy === 0) {
dx = 0;
dy = -1;
} else if (event.keyCode === 39 && dx === 0) {
dx = 1;
dy = 0;
} else if (event.keyCode === 40 && dy === 0) {
dx = 0;
dy = 1;
}
}
drawTable();
document.addEventListener("keydown", onKeyDown);
const gameLoop = setInterval(update, 400);
function removeFood() {
if (foods.length > 0) {
const removedFood = foods.shift();
const cell = document.getElementById(`cell-${removedFood.row}-${removedFood.col}`);
cell.classList.remove("food");
}
}
function updateFoods() {
if (foods.length === 0) {
addFood();
}
removeFood();
}
setInterval(updateFoods, 5500);
setInterval(clearFood, 4000);
}