Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
984 lines (927 loc) · 27 KB
/
Copy pathindex.html
File metadata and controls
984 lines (927 loc) · 27 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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-129166857-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-129166857-1');
</script>
<meta charset="UTF-8">
<title>Canvas snake</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<style>
@keyframes paused{
0%{
border:3px solid rgba(255,255,255,.4);
}
100%{
border: 3px solid rgba(0,0,0,.5);
}
}
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body{
margin: 0;
padding: 0;
overflow: hidden;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
height: 100vh;
background-image: radial-gradient(ellipse farthest-corner at center, #1FC3FF 0%, #376770 100%);
}
canvas{
padding: 0;
}
#canv{
position: relative;
display: block;
background: #282923;
margin-right: 10px;
}
#canv2{
position: relative;
display: block;
background: #282923;
/*top: 570px;*/
}
.scores{
position: absolute;
left: 5px;
font-size: 1.5em;
padding: 0;
margin: 0;
}
.sidebar{
position: relative;
flex-direction: column;
width: 500px;
height: 90vh;
padding: 8px;
background: #282923;
border-radius: 30px;
overflow-y: auto;
}
.sidebar li.snake{
/*width: 100%;*/
height: 40px;
border-radius: 30px;
display: flex;
justify-content: flex-end;
-ms-align-items: center;
align-items: center;
background: #cccccc;
margin-bottom: 8px;
border:3px solid rgba(255,255,255,.55);
}
.sidebar li.controls{
display: flex;
justify-content: flex-end;
}
.sidebar li.controls .btnAddSnake{
height: 40px;
width: 40px;
align-self: flex-end;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
border-radius: 30px;
font-size: 30px;
/*border:3px solid rgba(255,255,255,.55);*/
background-color: rgba(255,255,255,.4);
}
.sidebar li.controls .btnAddSnake:hover{
cursor: pointer;
/*border:3px solid rgba(0, 0, 0, 0.2);*/
/*border:3px solid rgba(255, 255, 255, 0.6);*/
}
.sidebar li.snake input{
width: 20px;
height: 20px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-size: 20px;
line-height: 20px;
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
-ms-align-items: center;
align-items: center;
justify-content: center;
position: relative;
padding: 0;
margin-right: 10px;
}
input{
background-color: rgba(255,255,255,.4);
border:3px solid rgba(0,0,0,.5);
min-height: 30px;
min-width: 30px;
line-height: 25px;
border-radius: 6px;
/*color: rgba(255, 255, 255, 0.4);*/
color: #000;
text-align: center;
/*text-transform: uppercase;*/
}
input[type='text']::-webkit-input-placeholder {
font-size: 25px;
/*color: rgba(255, 255, 255, 0.4);*/
color: #000;
text-align: center;
text-transform: uppercase;
vertical-align: middle;
}
input[type='color']{
background-color: rgba(255,255,255,.4);
border:3px solid rgba(0,0,0,.5);
min-height: 30px;
min-width: 30px;
/*line-height: 25px;*/
border-radius: 6px;
}
input[type='number']{
min-width: 20px;
width: 50px;
color: transparent;
text-shadow: 0 0 0 black;
}
input[type='number']::-webkit-inner-spin-button{
/*background: transparent;*/
/*height: 50%;*/
display: block;
}
input:hover{
border:3px solid rgba(255, 255, 255, 0.6);
cursor: pointer;
}
input:focus{
outline: none !important;
}
.score{
position: absolute;
left: 30px;
font-family: sans-serif;
font-size: 18px;
}
.sidebar li.controls > *{
margin-left: 15px;
}
input.playBtn{
color: #fff;
flex-grow: 100;
margin-left: 0 !important;
display: block;
border-radius: 30px;
font-family: sans-serif;
font-size: 20px;
}
.btnMute{
width: 40px;
height: 40px;
display: flex;
justify-content: center;
font-size: 20px;
color: #fff;
border-radius: 30px;
background-color: rgba(255,255,255,.4);
border:3px solid rgba(0,0,0,.5);
}
.btnMute:hover{
border:3px solid rgba(255, 255, 255, 0.6);
cursor: pointer;
}
.btnMute:focus{
outline: none !important;
}
.playBtn.blink{
animation-name: paused;
animation-duration: 1s;
animation-iteration-count: infinite;
}
.sidebar li.controls .appleCtrl{
border-radius: 30px;
border:3px solid rgba(255,255,255,.55);
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
-ms-align-items: center;
align-items: center;
justify-content: space-between;
height: 40px;
position: relative;
overflow: hidden;
z-index: 1;
}
.appleCtrl span{
padding: 0 48px;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(255,255,255,.55);
height: 100%;
color: #fff;
font-family: sans-serif;
font-size: 20px;
z-index: 1;
}
.appleCtrl input{
border-radius: 30px;
width: 40px;
height: 40px;
position: absolute;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
border-radius: 30px;
font-size: 30px;
background-color: transparent;
border-color: transparent;
z-index: 2;
}
.appleCtrl input:hover{
background-color: rgba(255,255,255,.4);
border:3px solid rgba(0, 0, 0, 0.2);
cursor: pointer;
}
input.decrease{
left: -2px;
}
input.increase{
right: -2px;
}
/*
::-webkit-input-placeholder {
text-align: center;
}
:-moz-placeholder { /* Firefox 18- *
text-align: center;
}
::-moz-placeholder { /* Firefox 19+ *
text-align: center;
}
:-ms-input-placeholder {
text-align: center;
}*/
</style>
</head>
<body>
<ul class="scores">
<li class="snake1Score"></li>
<li class="snake2Score"></li>
<li class="snake3Score"></li>
</ul>
<audio id="turn">
<source src="sounds/5.mp3" type="audio/ogg">
Your browser does not support the audio element.
</audio>
<audio id="eat">
<source src="sounds/4.mp3" type="audio/ogg">
Your browser does not support the audio element.
</audio>
<audio id="fail">
<source src="sounds/1.mp3" type="audio/ogg">
Your browser does not support the audio element.
</audio>
<canvas id='canv'>Update your browser</canvas>
<ul class="sidebar">
<!-- <li class="snake">
<strong class="score">Score: <span class="value">10</span></strong>
<input type="color">
<input type="text" placeholder="⇐">
<input type="text" placeholder="⇑">
<input type="text" placeholder="⇒">
<input type="text" placeholder="⇓">
<input type="button" value="✕">
</li> -->
<li class="controls" id="controls">
<input type="button" class="playBtn blink" autofocus id="playBtn" value="Start">
<button class="btnMute" autofocus id="btnMute" ><i class="fas fa-volume-up" id="vol"></i></button>
<div class="appleCtrl" id="appleCtrl">
<input type="button" title="Delete apple" value="-" class="decrease" id="decr">
<span class="appleNum" id="aNum">1</span>
<input type="button" title="Add apple" value="+" class="increase" id="incr">
</div>
<input type="button" title="Add snake" class="btnAddSnake" id="btnAddSnake" value="+">
</li>
</ul>
<!-- <canvas id='canv2'>Update your browser</canvas> -->
<script>
'use strict'
//alert(JSON.stringify({up: 38,dn: 40,lt: 37,rt: 39}) === JSON.stringify({up: 38,dn: 40,lt: 37,rt: 39}));
class Canvas{
constructor(selector, pxSize, pxNum = 36 * 2){
this._selector = selector;
//this.size = ~~size;
this.pxNum = ~~pxNum;
this.pxSize = ~~pxSize;
//this.pxSize = this.size / this.pxNum;
this.size = this.pxNum * this.pxSize;
this.appleNum = 1;
this.appleMaxNum = 20;
this.playBtn = document.getElementById("playBtn");
this.btnMute = document.getElementById("btnMute");
this.snakes = [];
this.apples = [];
this.init();
//this.drawGrid();
}
get elem() {
return document.querySelector(this._selector);
}
get ctx() {
return this.elem.getContext('2d');
}
get test() {
return "test";
}
init() {
const canv = this.elem;
const ctx = this.ctx;
ctx.fillStyle = 'Black';
ctx.strokeStyle = 'Black';
canv.width = canv.height = this.size;
this.addSnake();
//this.snakes.forEach((snake)=>{
//snake.init();
//});
this.playBtn.addEventListener("click",(e) => {
if(this.playBtn.value == "Start"){//e.keyCode == 13 && !this.gameStarted
this.gameStarted = true;
this.appleSpawn();
this.applesDraw();
this.snakes.forEach((snake)=>{
snake.start();
});
e.target.classList.remove("blink");
this.playBtn.value = "Pause";
this.gamePlayPause();
}
});
this.appleControl();
this.btnAddSnake("btnAddSnake");
this.muteCtrl();
}
muteCtrl(){
this.btnMute.addEventListener("click", (e) => {
//alert(String.fromCharCode(10005));
//alert((5535756586).toString(32));
let icon = document.getElementById("vol");
/*let off = document.createElement('li');
off.innterH*/
if(this.muted){
icon.classList.remove("fa-volume-off");
icon.classList.add("fa-volume-up");
this.muted = false;
}else{
icon.classList.remove("fa-volume-up");
icon.classList.add("fa-volume-mute");
this.muted = true;
}
});
}
btnAddSnake(id){
const btnAddSnake = document.getElementById(id);
btnAddSnake.addEventListener("click",()=>{
this.addSnake();
//this.appleAdd();
});
}
gamePlayPause(){
this.playBtn.addEventListener("click",(e) => {
if(e.target.value == "Pause"){
this.snakes.forEach((snake,i)=>{
snake.pause();
});
e.target.classList.add("blink");
e.target.value = "Resume";
}else if(e.target.value == "Resume"){
this.snakes.forEach((snake,i)=>{
snake.resume();
});
e.target.classList.remove("blink");
e.target.value = "Pause";
}
});
}
drawLine(x1, y1, x2, y2, color = 'grey') {
const canv = this.elem;
const ctx = this.ctx;
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineJoin = 'mitter';
ctx.lineWidth = 1;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
drawGrid(){
let w = this.size,
h = this.size,
//p = this.pxSize,//w / Canvas._pxSize,
xStep = this.pxSize,
yStep = this.pxSize;
for(let x = 0; x < w; x += xStep){
this.drawLine(x, 0, x, h);
}
for(let y = 0; y < h; y += yStep){
this.drawLine(0, y, w, y);
}
}
appleAdd(){
++this.appleNum;
let apl = new Apple(this,"blue");
this.apples.push(apl);
apl.randCord();
apl.spawn();
}
appleRemove(){
--this.appleNum;
let i = this.appleNum;
let apl = this.apples[i];
apl.delete();
this.apples = this.apples.slice(0,i);
}
appleSpawn(){
for(let i = 0; i < this.appleNum; ++i){
this.appleAdd();
--this.appleNum;//appleApp() already increases appleNum
}
return this.apples;
}
applesDraw(){
this.apples.every((elem,i)=>{
elem.spawn();
});
}
addSnake(){
const snake = new Snake(this, 20, {up: 38, dn: 40, lt: 37, rt: 39, forse: false});
//{up: 73, dn: 75, lt: 74, rt: 76, forse: 16 }
}
appleControl(){
const ctx = this.ctx;
//const appleControl = document.getElementById("appleCtrl");
const appleIncrease = document.getElementById("incr");
const appleDecrease = document.getElementById("decr");
const appleNumOut = document.getElementById("aNum");
appleNumOut.innerHTML = this.appleNum;
appleIncrease.addEventListener("click",(e)=>{
//alert(e.target.value);
//alert("old: "+this.appleNum+"\nnew: "+e.target.value);
if(this.appleNum + 1 <= this.appleMaxNum){
this.appleAdd();
appleNumOut.innerHTML = this.appleNum;
}
});
appleDecrease.addEventListener("click",(e)=>{
if(this.appleNum - 1 >= 1){
this.appleRemove();
appleNumOut.innerHTML = this.appleNum;
}
});
}
}
class Apple {
constructor(parent,color = "blue"){
this.color = color;
this.parent = parent;
}
randCord(){
const parent = this.parent;
this.x = ~~(Math.random() * (parent.size / parent.pxSize)) * parent.pxSize,
this.y = ~~(Math.random() * (parent.size / parent.pxSize)) * parent.pxSize;
this.parent.snakes.some((snake)=>{
snake.tail.some((t)=>{
if(~~this.x == ~~t.x && ~~this.y == ~~t.y){
this.randCord();
}
});
});
}
spawn(){
const parent = this.parent;
const ctx = parent.ctx;
ctx.fillStyle = this.color;
parent.gameStarted ? ctx.fillRect(this.x, this.y,parent.pxSize,parent.pxSize) : false;
}
respawn(){
this.delete();
this.randCord();
this.spawn();
}
delete(){
const parent = this.parent;
const ctx = parent.ctx;
ctx.clearRect(this.x, this.y,parent.pxSize,parent.pxSize);
}
}
class Snake {
constructor(
parent,
size = 5,
key = {up: 38,dn: 40,lt: 37,rt: 39,forse:32}||{up: 87,dn: 83,lt: 65,rt: 68,forse:32},
teleport = true,
selfCollision = true,
multiplayer = true
){
this.size = this.size_basic = size;//tail size
this.tail = []; //tail array
this.tail_o = []; //previous frame tail array
this.spd = this.spd_basic = 5; // absloute spead
this.spX = 0; //X axis spead
this.spY = 0; //Y axis spead
this.teleport = teleport;
this.selfCollision = true;//selfCollision;
this.parent = parent; //parent canvas
//this.color = color; //snake color
this.color = "#"+ Math.floor(Math.random()*16777215).toString(16);
this.headColor = "#"+ Math.floor(Math.random()*16777215).toString(16);
this.score_table;
this.key = key; //snake keys array
this.gameStarted = false;
this.forseSpead = true;
this.score = 0;//score
this.multiplayer = multiplayer;
this.paused = false;
this.parent.snakes.push(this);
this.init();
this.interval = 1000/(this.spd*2.5);
}
init() {
const parent = this.parent;
this.settings(".sidebar",parent);
++parent.snakes_num;
if(parent.gameStarted){
this.start();
}
}
start(){
const parent = this.parent;
const headX = ~~(Math.random() * (parent.size / parent.pxSize)) * parent.pxSize,
headY = ~~(Math.random() * (parent.size / parent.pxSize)) * parent.pxSize;
//addEventListener("keypress",(e) => {
if(parent.gameStarted){
//for(let parent of this.parents){
//console.log("started");
this.place(parent, headX, headY);
const ctx = parent.ctx;
ctx.fillStyle = 'Black';
//ctx.strokeStyle = 'Black';
const pxSize = parent.pxSize;
this.gameStarted = true;
this.move( parent, headX, headY );
//}
}
//});
}
pause(){
this.paused = true;
this.spX = this.spY = 0;
const snake = this;
//document.removeEventListener("keydown",snake.changeDir(event));
//document.removeEventListener("keyup",snake.forseSpead(event));
}
resume(){
this.paused = false;
//this.spX = this.spY = 0;
}
drawTail(ctx,pxSize){
if(!this.deleted){
this.tail.forEach((elem,i)=>{
ctx.fillStyle = elem.color;
if(i == 0 && !this.failed){ctx.fillStyle = this.headColor;}
ctx.fillRect(elem.x, elem.y, pxSize, pxSize);
});
}
}
clearTail(ctx,pxSize){
if(!this.deleted){
let c = 0.2;
this.tail_o.forEach((elem,i)=>{
//ctx.clearRect(elem.x - 0.45, elem.y - 0.45, pxSize + 0.9, pxSize + 0.9);
ctx.clearRect(elem.x - c, elem.y - c, pxSize + 2*c, pxSize + 2*c);
});
this.tail_o = this.tail;
}
//this.parent.drawGrid();
}
place(parent, headX, headY) {
const ctx = parent.ctx;
const pxSize = parent.pxSize;
//ctx.strokeStyle = this.color;
ctx.lineJoin = 'mitter';
ctx.lineWidth = 1;
ctx.fillStyle = this.color;
//ctx.beginPath();
let head = ctx.fillRect(headX, headY, pxSize, pxSize);//ctx.arc(headX, headY, pxSize / 2,0, Math.PI * 2);
//ctx.fill()
//ctx.closePath();
//console.log(this.appleCheck(parent,headX,headY));
}
remove(ctx,pxSize){
//this.score_table.removeChild(this.panel);
this.panel.style.display = "none";
this.failed = true;
//headX -= this.spX;
//headY -= this.spY;
this.size -= 1;
this.spX = 0;
this.spY = 0;
this.score = 0;
this.failed = true;
this.deleted = true;
this.tail.forEach((elem,i)=>{
ctx.clearRect(elem.x - 0.45, elem.y - 0.45, pxSize + 0.9, pxSize + 0.9);
});
//this.tail = [NaN];
let snakes = this.parent.snakes;
let index = this.parent.snakes.indexOf(this);
snakes.splice(index,1);
//console.log(snakes.length);
ctx.clearRect(this.headX - 0.45, this.headY - 0.45, pxSize + 0.9, pxSize + 0.9);
}
sound(id,parent){
if(!parent.muted){
document.getElementById(id).pause();
document.getElementById(id).currentTime = 0;
document.getElementById(id).play();
}
}
settings(elem,parent){
const ctx = parent.ctx;
const pxSize = parent.pxSize;
let panel = document.createElement("li"),
score_table = document.createElement("strong"),
scoreTxt = document.createTextNode("Score: "),
scoreVal = document.createElement("span"),
color = document.createElement("input"),
btnDel = document.createElement("input");
panel.classList.add("snake");
panel.style.backgroundColor = this.color;
score_table.classList.add("score");
score_table.appendChild(scoreTxt);
score_table.appendChild(scoreVal);
color.setAttribute("type","color");
color.value = this.color;
btnDel.setAttribute("type","button");
btnDel.value = String.fromCharCode(10005);
panel.appendChild(score_table);
panel.appendChild(color);
let keys = {
lt : String.fromCharCode(8656),//"⇐",
up : String.fromCharCode(8657),//"⇑",
rt : String.fromCharCode(8658),//"⇒",
dn : String.fromCharCode(8659),//"⇓",
}
const Sidebar = document.querySelector(elem);
const btnAddSnake = document.getElementById("controls");
panel = Sidebar.insertBefore(panel, btnAddSnake);
for (let key in keys) {
let k = document.createElement("input");
k.classList.add("key");
k.setAttribute("type","text");
k.setAttribute("placeholder",keys[key]);
k.setAttribute("maxlength","1");
keys[key] = k;
panel.appendChild(k);
}
panel.appendChild(btnDel);
for(let k in keys){
keys[k].addEventListener("keyup",(e)=>{
//keys[k].style.border = "2px solid red";
this.key[k] = e.keyCode;
//console.log(this.key);
});
}
color.addEventListener("change",()=>{
this.color = color.value;
panel.style.backgroundColor = this.color;
});
btnDel.addEventListener("click",()=>{
this.remove(ctx,pxSize);
});
this.score_table = score_table;
this.panel = panel;
this.scoreVal = scoreVal;
this.scoreVal.innerHTML = this.score;
}
appleCheck(parent,x,y){
//for(let i = 0; i < parent.appleNum; ++i){
parent.apples.some((elem,i)=>{
let appleX = parent.apples[i].x,
appleY = parent.apples[i].y;
let headX = x,
headY = y;
if(
(Math.abs(headX - appleX) < 2)&&
(Math.abs(headY - appleY) < 2)
){
++this.size;
this.sound("eat",parent);
parent.apples[i].respawn();
this.spd += 0.0125;
++this.score;
this.interval = 1000/(this.spd*2.5);
//console.log(this.spd);
return true;
}
});
}
score_out(selector){
this.scoreVal.innerHTML = this.score;
}
move(parent,headX,headY){
const ctx = parent.ctx;
const pxSize = parent.pxSize;
let spd_o = this.spd;
//const changeDir = this.changeDir(e);
//const forseSpead = this.forseSpead(e);
document.addEventListener("keydown", e => {
if(!this.paused){
//alert("not paused");
if (e.keyCode == this.key.up && !this.spY) {
this.spY = -parent.pxSize; this.spX = 0;
this.sound("turn",parent);
this.failed=false;
}// up
else if (e.keyCode == this.key.dn && !this.spY) {
this.spY = parent.pxSize; this.spX = 0;
this.sound("turn",parent);
this.failed=false;
}// down
else if (e.keyCode == this.key.lt && !this.spX) {
this.spX = -parent.pxSize; this.spY = 0;
this.sound("turn",parent);
this.failed=false;
}// left
else if (e.keyCode == this.key.rt && !this.spX) {
this.spX = parent.pxSize; this.spY = 0;
this.sound("turn",parent);
this.failed=false;
}// right
else{
//console.log(e.keyCode);
}
if (e.keyCode == (this.key.forse) && this.gameStarted && this.forseSpead){
this.spd += 15;
this.interval = 1000/(this.spd*2.5);
}
}
});
document.addEventListener("keyup" ,e => {
//this.forseSpead(e);
if(!this.paused){
if (e.keyCode == (this.key.forse) && this.gameStarted && this.forseSpead){
this.spd = spd_o;
this.interval = 1000/(this.spd*2.5);
}
}
});
//}
//console.log(this.paused);
let loop = () => {
this.headX = headX;
this.headY = headY;
let head = {x:headX,y:headY,color:ctx.fillStyle};
ctx.fillStyle = this.color;
//head moving
headX += this.spX;
headY += this.spY;
head = {x:headX,y:headY,color:ctx.fillStyle};
//teleport (strict after head moving)
if(this.teleport){
let start = 0,
end = parent.size;
if( Math.ceil(headX) >= end ){headX = start; }
if( Math.ceil(headX) < start ){headX = end - pxSize; }
if( Math.ceil(headY) < start ){headY = end - pxSize; }
if( Math.ceil(headY) >= end ){headY = start; }
}
//self collision check
if(this.selfCollision && this.score > 0 && !this.failed && !this.paused){
this.tail.some((elem,i)=>{
if(elem.x == headX && elem.y == headY && i <= this.size - 1){
this.sound("fail",parent);
//--this.size;
//--this.score;
//this.spd -= 0.0125;
if(i + 1 > this.size_basic){
//alert(this.size+"\n"+i + 1);
//console.error("rest tail: " + i);
//this.spd -= (this.spd_basic / this.size_basic) * (this.size - (i + 1));
this.spd -= (this.size - i - 1) * 0.0125;
this.score -= (this.size - i - 1);
//alert("bind 1");
this.size = i;
//alert(this.size);
}else{
//this.size = this.size_basic;
//this.spd = this.spd_basic;
//this.score = 0;
//alert("bind 2");
--this.size;
--this.score;
this.spd -= 0.0125;
}
/*//////////
if(i >= this.tail.length / 2){
if(this.size - i >= this.size_basic){
this.size = i;
this.spd -= (this.spd_basic / this.size_basic) * i;
}else{
this.size = this.size_basic;
this.spd = this.spd_basic;
}
//this.spd -= (this.spd / this.size);
//this.score -= i + this.size_basic;//this.size - this.tail.length + i;
this.score -= i;
}else{
this.size -= 1;
this.spd -= this.spd_basic / this.size_basic;//0.05;
//alert(this.spd - (this.spd / this.size));
--this.score;
}*/
/////
this.tail = this.tail.slice(0,this.size);
this.interval= 1000/(this.spd*2.5);
//console.warn(this.spd);
return true;
}
});
}
//other snake collision check
if(this.multiplayer && this.gameStarted && !this.failed){
parent.snakes.some((snake)=>{
if(snake !== this && !this.failed){
//this.color == "grey" ? console.log(this.tail) : false;
//snake.tail.some((t,i)=>{
let t = snake.tail;
for(let i = 0; i < t.length; ++i){
if(~~t[i].x == ~~headX && ~~t[i].y == ~~headY && !this.failed){
this.sound("fail",parent);
headX -= this.spX;
headY -= this.spY;
this.spX = 0;
this.spY = 0;
if(this.score){
this.size -= 1;
--this.score;
this.failed = true;
this.interval = 1000/(this.spd*2.5);
}
break;
}
}
}
});
}
//clear old tail
//if(!this.failed){
this.clearTail(ctx,pxSize);
//}
//generating new tail by pushing "new head" to the begining of the tail
if(!this.deleted){
this.tail.unshift({x:headX,y:headY,color:ctx.fillStyle});
}
//limiter
if( this.tail.length > this.size ){ this.tail.pop(); }
//draw new tail
this.drawTail(ctx,pxSize);
this.appleCheck(parent,headX,headY);
this.score_out(this.score_table);
setTimeout(loop,this.interval);
}
loop();
}
}
const canvas1 = new Canvas('#canv' , 10,72);
//const canvas2 = new Canvas('#canv2', 550, 15);
//console.log(canvas1.snakes);
//const snake1 = new Snake(canvas1, 20, "lime", {up: 73, dn: 75, lt: 74, rt: 76, forse: 16 });
//snake1.score_table = ".snake1Score";
//const snake2 = new Snake(canvas1, 20, "#00ffff",{up: 87, dn: 83, lt: 65, rt: 68, forse: 16 });
//snake2.score_table = ".snake2Score";
//const snake3 = new Snake(canvas1, 20, "#cccccc", {up: 104,dn: 101,lt: 100,rt: 102, forse: 32});
//snake3.score_table = ".snake3Score";
/*const addSnake = document.getElementById("btnAddSnake");
addSnake.addEventListener("click",()=>{
canvas1.addSnake();
//canvas1.appleAdd();
});*/
</script>
</body>
</html>