-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
848 lines (713 loc) · 25.2 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
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
// overall user interface varibles
var onePlayerBtn = document.querySelector('#onePlayer');
var twoPlayerBtn = document.querySelector('#twoPlayer');
var highScoreBtn = document.querySelector('#highScoreMode');
var survivalBtn = document.querySelector('#survivalMode');
var firstPlayerCanvas = document.querySelector('#myCanvas');
var secondPlayerCanvas = document.querySelector('#dualCanvas');
var twoPlayerCanvas = document.querySelector('#survivalCanvas');
var startBtn = document.querySelector('#btn');
var startSurvivalBtn = document.querySelector('#survivalBtn');
var backBtn = document.querySelector('#backBtn');
var controlBtnP1 = document.querySelector('#controlBtnP1');
var controlBtnP2 = document.querySelector('#controlBtnP2');
var onePlayerControl = document.querySelector('#onePlayerControl');
var twoPlayerControl = document.querySelector('#twoPlayerControl');
// one player game
function startOnePlayer() {
onePlayerBtn.style.display = "none";
twoPlayerBtn.style.display = "none";
highScoreBtn.style.display = "none";
survivalBtn.style.display = "none";
firstPlayerCanvas.style.display = "inline-block";
secondPlayerCanvas.style.display = "none";
twoPlayerCanvas.style.display = "none";
startBtn.style.display = "inline-block";
startBtn.style.marginTop = "10px";
startSurvivalBtn.style.display = "none";
backBtn.style.display = "inline-block";
backBtn.style.marginTop = "10px";
controlBtnP1.style.display = "inline-block";
controlBtnP2.style.display = "none";
}
// two players mode selection = "high score" || "survival"
function twoPlayerMode() {
onePlayerBtn.style.display = "none";
twoPlayerBtn.style.display = "none";
highScoreBtn.style.display = "inline-block";
survivalBtn.style.display = "inline-block";
firstPlayerCanvas.style.display = "none";
secondPlayerCanvas.style.display = "none";
twoPlayerCanvas.style.display = "none";
startBtn.style.display = "none";
startSurvivalBtn.style.display = "none";
backBtn.style.display = "block";
backBtn.style.marginLeft = "auto";
backBtn.style.marginRight = "auto";
backBtn.style.marginTop = "10px";
controlBtnP1.style.display = "none";
controlBtnP2.style.display = "none";
}
// two players high score mode
function startTwoPlayerHighScore() {
onePlayerBtn.style.display = "none";
twoPlayerBtn.style.display = "none";
highScoreBtn.style.display = "none";
survivalBtn.style.display = "none";
firstPlayerCanvas.style.display = "inline-block";
secondPlayerCanvas.style.display = "inline-block";
twoPlayerCanvas.style.display = "none";
startBtn.style.display = "inline-block";
startBtn.style.marginTop = "10px";
startSurvivalBtn.style.display = "none";
backBtn.style.display = "inline-block";
backBtn.style.marginTop = "10px";
controlBtnP1.style.display = "none";
controlBtnP2.style.display = "inline-block";
}
// two players survival mode
function startTwoPlayerSurvival () {
onePlayerBtn.style.display = "none";
twoPlayerBtn.style.display = "none";
highScoreBtn.style.display = "none";
survivalBtn.style.display = "none";
firstPlayerCanvas.style.display = "none";
secondPlayerCanvas.style.display = "none";
twoPlayerCanvas.style.display = "block";
startBtn.style.display = "none";
startSurvivalBtn.style.display = "inline-block";
startSurvivalBtn.style.marginTop = "10px";
backBtn.style.display = "inline-block";
backBtn.style.marginTop = "10px";
controlBtnP1.style.display = "none";
controlBtnP2.style.display = "inline-block";
}
// return back to main menu
function mainMenu() {
onePlayerBtn.style.display = "inline-block";
twoPlayerBtn.style.display = "inline-block";
highScoreBtn.style.display = "none";
survivalBtn.style.display = "none";
firstPlayerCanvas.style.display = "none";
secondPlayerCanvas.style.display = "none";
twoPlayerCanvas.style.display = "none";
startBtn.style.display = "none";
startSurvivalBtn.style.display = "none";
backBtn.style.display = "none";
controlBtnP1.style.display = "none";
controlBtnP2.style.display = "none";
}
// to display & remove instructions to play game
function appearOnePlayerControl() {
onePlayerControl.style.display = "block";
}
function removeOnePlayerControl() {
onePlayerControl.style.display = "none";
}
function appearTwoPlayerControl() {
twoPlayerControl.style.display = "block";
}
function removeTwoPlayerControl() {
twoPlayerControl.style.display = "none";
}
// linking 1 Player button to startOnePlayer function
onePlayerBtn.addEventListener("click", startOnePlayer);
// linking 2 Player button to mode selection
twoPlayerBtn.addEventListener("click", twoPlayerMode);
// linking highScoreBtn to high score mode
highScoreBtn.addEventListener("click", startTwoPlayerHighScore);
// linking survivalBtn to survival mode
survivalBtn.addEventListener("click", startTwoPlayerSurvival);
// linking back button to home page
backBtn.addEventListener("click", mainMenu);
// button to display control keys for Player 1
controlBtnP1.addEventListener("mouseover", appearOnePlayerControl);
controlBtnP1.addEventListener("mouseout", removeOnePlayerControl);
// button to display control keys for Player 2
controlBtnP2.addEventListener("mouseover", appearTwoPlayerControl);
controlBtnP2.addEventListener("mouseout", removeTwoPlayerControl);
// linking to P1 myCanvas html
var mycanvas = document.getElementById('myCanvas');
var ctx = myCanvas.getContext("2d");
// linking to P2 dualCanvas html
var dualCanvas = document.getElementById('dualCanvas');
var ctxB = dualCanvas.getContext("2d");
// linking to 2 players survivalCanvas html
var survivalCanvas = document.getElementById('survivalCanvas');
var ctxC = survivalCanvas.getContext("2d");
// components of the P1 snake game
var snakeSize = 10;
var w = 350;
var h = 350;
var score = 0;
var snake;
var food;
// components of the P2 snake game
var snakeSizeB = 10;
var wB = 350;
var hB = 350;
var scoreB = 0;
var snakeB;
var foodB;
// P1 game module
var drawModule = (function () {
var bodySnake = function(x, y) {
// this is the single square of the snake
ctx.fillStyle = 'yellow';
ctx.fillRect(x*snakeSize, y*snakeSize, snakeSize, snakeSize);
// this is the border of the snake
ctx.strokeStyle = 'darkgreen';
ctx.strokeRect(x*snakeSize, y*snakeSize, snakeSize, snakeSize);
}
var bait = function(x, y) {
// this is the single square of the food
ctx.fillStyle = 'red';
ctx.fillRect(x*snakeSize+1, y*snakeSize+1, snakeSize-2, snakeSize-2);
// this is the border of the food
ctx.strokeStyle = 'white';
ctx.strokeRect(x*snakeSize, y*snakeSize, snakeSize, snakeSize);
}
var scoreText = function() {
// this is to keep track of the score aka food eaten
var score_text = "Score: " + score;
ctx.fillStyle = 'blue';
ctx.fillText(score_text, 145, h-5);
}
// create snake
var drawSnake = function() {
// snake length will be 5 square long at the start
var length = 4;
snake = [];
// for loop will push 5 square into the snake element
// every element will have x = 0 and the y will take the value of the index.
for (var i = length; i>=0; i--) {
snake.push({x:i, y:0});
}
}
// create food
var createFood = function() {
food = {
// food will be at random position
x: Math.floor((Math.random() * 30) + 1),
y: Math.floor((Math.random() * 30) + 1)
}
// look at the position of the snake's body.
for (var i=0; i>snake.length; i++) {
var snakeX = snake[i].x;
var snakeY = snake[i].y;
// if food is in the position of the snake, food will randomise again
if (food.x === snakeX || food.y === snakeY || food.y === snakeY && food.x===snakeX) {
food.x = Math.floor((Math.random() * 30) + 1);
food.y = Math.floor((Math.random() * 30) + 1);
}
}
}
// check for collision with body
var checkCollision = function(x, y, array) {
for(var i=0; i<array.length; i++) {
if(array[i].x === x && array[i].y === y)
return true;
}
return false;
}
var paint = function () {
// the area within the canvas in which the snake will move
ctx.fillStyle = 'lightgrey';
ctx.fillRect(0, 0, w, h);
// the border of the play area
ctx.strokeStyle = 'black';
ctx.strokeRect(0, 0, w, h);
// start game button will be disable during game play
btn.setAttribute('disabled', true);
var snakeX = snake[0].x;
var snakeY = snake[0].y;
// using keyboard arrow keys to control the snake
// pop out the last element of the array and shift it on the top as first element
if (direction === 'right') {
snakeX++;
} else if (direction === 'left') {
snakeX--;
} else if (direction === 'up') {
snakeY--;
} else if (direction === 'down') {
snakeY++;
}
// game over when snake collide the wall
// game over when snake conllide it's own body (checkCollision function)
if (snakeX == -1 || snakeX == w / snakeSize || snakeY == -1 || snakeY == h / snakeSize || checkCollision(snakeX, snakeY, snake)) {
// stop the game
// start button enabled again
btn.removeAttribute('disabled', true);
// reset score
score = 0;
// clean up the canvas
ctx.clearRect(0, 0, w, h);
gameloop = clearInterval(gameloop);
return;
}
// snake eat food will grow in length
if (snakeX == food.x && snakeY == food.y) {
// add a new square at the end of the array instead of moving the tail to the front
var tail = {
x: snakeX,
y: snakeY
};
// snake eat food will score point
score++;
// create new food.
createFood();
} else {
// pop out the last cell
var tail = snake.pop();
tail.x = snakeX;
tail.y = snakeY;
}
// pop out the tail and shift it to the front of the array
snake.unshift(tail);
// creat snake using bodySnake function
for (var i = 0; i < snake.length; i++) {
bodySnake(snake[i].x, snake[i].y);
}
// create food using bait function
bait(food.x, food.y);
// input scoreText using scoreText function
scoreText();
}
var init = function () {
direction = "down";
drawSnake();
createFood();
gameloop = setInterval(paint, 80);
}
// to expose the init function
return {
init: init
};
// end of P1 drawModule
}());
// P2 game module
var drawModuleB = (function () {
var bodySnakeB = function(x, y) {
// this is the single square of the snake
ctxB.fillStyle = 'green';
ctxB.fillRect(x*snakeSizeB, y*snakeSizeB, snakeSizeB, snakeSizeB);
// this is the border of the snake
ctxB.strokeStyle = 'darkblue';
ctxB.strokeRect(x*snakeSizeB, y*snakeSizeB, snakeSizeB, snakeSizeB);
}
var baitB = function(x, y) {
// this is the single square of the food
ctxB.fillStyle = 'salmon';
ctxB.fillRect(x*snakeSizeB+1, y*snakeSizeB+1, snakeSizeB-2, snakeSizeB-2);
// this is the border of the food
ctxB.strokeStyle = 'white';
ctxB.strokeRect(x*snakeSizeB, y*snakeSizeB, snakeSizeB, snakeSizeB);
}
var scoreTextB = function() {
// this is to keep track of the score aka food eaten
var score_text = "Score: " + scoreB;
ctxB.fillStyle = 'blue';
ctxB.fillText(score_text, 145, hB-5);
}
// create snake
var drawSnakeB = function() {
// snake length will be 5 square long at the start
var length = 4;
snakeB = [];
// for loop will push 5 square into the snake element
// every element will have x = 0 and the y will take the value of the index.
for (var i = length; i>=0; i--) {
snakeB.push({x:i, y:0});
}
}
// create food
var createFoodB = function() {
foodB = {
// food will be at random position
x: Math.floor((Math.random() * 30) + 1),
y: Math.floor((Math.random() * 30) + 1)
}
// look at the position of the snake's body.
for (var i=0; i>snakeB.length; i++) {
var snakeXB = snakeB[i].x;
var snakeYB = snakeB[i].y;
// if food is in the position of the snake, food will randomise again
if (foodB.x === snakeXB || foodB.y === snakeYB || foodB.y === snakeYB && foodB.x===snakeXB) {
foodB.x = Math.floor((Math.random() * 30) + 1);
foodB.y = Math.floor((Math.random() * 30) + 1);
}
}
}
// check for collision with body
var checkCollisionB = function(x, y, array) {
for(var i=0; i<array.length; i++) {
if(array[i].x === x && array[i].y === y)
return true;
}
return false;
}
var paintB = function () {
// the area within the canvas in which the snake will move
ctxB.fillStyle = 'lightgrey';
ctxB.fillRect(0, 0, wB, hB);
// the border of the play area
ctxB.strokeStyle = 'black';
ctxB.strokeRect(0, 0, wB, hB);
// start game button will be disable during game play
btn.setAttribute('disabled', true);
var snakeXB = snakeB[0].x;
var snakeYB = snakeB[0].y;
// using keyboard arrow keys to control the snake
// pop out the last element of the array and shift it on the top as first element
if (movement === 'right') {
snakeXB++;
} else if (movement === 'left') {
snakeXB--;
} else if (movement === 'up') {
snakeYB--;
} else if (movement === 'down') {
snakeYB++;
}
// game over when snake collide the wall
// game over when snake conllide it's own body (checkCollision function)
if (snakeXB == -1 || snakeXB == wB / snakeSizeB || snakeYB == -1 || snakeYB == hB / snakeSizeB || checkCollisionB(snakeXB, snakeYB, snakeB)) {
// stop the game
// start button enabled again
btn.removeAttribute('disabled', true);
// reset score
scoreB = 0;
// clean up the canvas
ctxB.clearRect(0, 0, wB, hB);
gameloopB = clearInterval(gameloopB);
return;
}
// snake eat food will grow in length
if (snakeXB == foodB.x && snakeYB == foodB.y) {
// add a new square at the end of the array instead of moving the tail to the front
var tailB = {
x: snakeXB,
y: snakeYB
};
// snake eat food will score point
scoreB++;
// create new food.
createFoodB();
} else {
// pop out the last cell
var tailB = snakeB.pop();
tailB.x = snakeXB;
tailB.y = snakeYB;
}
// pop out the tail and shift it to the front of the array
snakeB.unshift(tailB);
// creat snake using bodySnake function
for (var i = 0; i < snakeB.length; i++) {
bodySnakeB(snakeB[i].x, snakeB[i].y);
}
// create food using bait function
baitB(foodB.x, foodB.y);
// input scoreText using scoreText function
scoreTextB();
}
var initB = function () {
movement = "down";
drawSnakeB();
createFoodB();
gameloopB = setInterval(paintB, 80);
}
// to expose the init function
return {
initB: initB
};
// end of P2 drawModule
}());
// 2 players survival game module
var drawModuleC = (function () {
var bodySnake = function(x, y) {
// this is the single square of the snake
ctxC.fillStyle = 'yellow';
ctxC.fillRect(x*snakeSize, y*snakeSize, snakeSize, snakeSize);
// this is the border of the snake
ctxC.strokeStyle = 'darkgreen';
ctxC.strokeRect(x*snakeSize, y*snakeSize, snakeSize, snakeSize);
}
var bodySnakeB = function(x, y) {
// this is the single square of the snake
ctxC.fillStyle = 'green';
ctxC.fillRect(x*snakeSizeB, y*snakeSizeB, snakeSizeB, snakeSizeB);
// this is the border of the snake
ctxC.strokeStyle = 'darkblue';
ctxC.strokeRect(x*snakeSizeB, y*snakeSizeB, snakeSizeB, snakeSizeB);
}
var bait = function(x, y) {
// this is the single square of the food
ctxC.fillStyle = 'red';
ctxC.fillRect(x*snakeSize+1, y*snakeSize+1, snakeSize-2, snakeSize-2);
// this is the border of the food
ctxC.strokeStyle = 'white';
ctxC.strokeRect(x*snakeSize, y*snakeSize, snakeSize, snakeSize);
}
var scoreText = function() {
// this is to keep track of the score aka food eaten
var score_text = "Player Left: " + score;
ctxC.fillStyle = 'blue';
ctxC.fillText(score_text, 73, h-5);
}
var scoreTextB = function() {
// this is to keep track of the score aka food eaten
var score_textB = "Player Right: " + scoreB;
ctxC.fillStyle = 'blue';
ctxC.fillText(score_textB, 213, h-5);
}
// create snake
var drawSnake = function() {
// snake length will be 5 square long at the start
var length = 4;
snake = [];
// for loop will push 5 square into the snake element
// every element will have x = 0 and the y will take the value of the index.
for (var i = length; i>=0; i--) {
snake.push({x:i, y:0});
}
}
var drawSnakeB = function() {
// snake length will be 5 square long at the start
var length = 4;
snakeB = [];
// for loop will push 5 square into the snake element
// every element will have x = 0 and the y will take the value of the index.
for (var i = length; i>=0; i--) {
snakeB.push({x:i+30, y:0});
}
}
// create food
var createFood = function() {
food = {
// food will be at random position
x: Math.floor((Math.random() * 30) + 1),
y: Math.floor((Math.random() * 30) + 1)
}
// look at the position of the snake's body.
for (var i=0; i>snake.length; i++) {
var snakeX = snake[i].x;
var snakeY = snake[i].y;
// if food is in the position of the snake, food will randomise again
if (food.x === snakeX || food.y === snakeY || food.y === snakeY && food.x===snakeX) {
food.x = Math.floor((Math.random() * 30) + 1);
food.y = Math.floor((Math.random() * 30) + 1);
}
}
}
// check for collision with body
var checkCollision = function(x, y, array) {
for(var i=0; i<array.length; i++) {
if(array[i].x === x && array[i].y === y)
return true;
}
return false;
}
var paint = function () {
// the area within the canvas in which the snake will move
ctxC.fillStyle = 'lightgrey';
ctxC.fillRect(0, 0, w, h);
// the border of the play area
ctxC.strokeStyle = 'black';
ctxC.strokeRect(0, 0, w, h);
// start game button will be disable during game play
survivalBtn.setAttribute('disabled', true);
var snakeX = snake[0].x;
var snakeY = snake[0].y;
var snakeBX = snakeB[0].x;
var snakeBY = snakeB[0].y;
// using keyboard arrow keys to control the snake
// pop out the last element of the array and shift it on the top as first element
if (direction === 'right') {
snakeX++;
} else if (direction === 'left') {
snakeX--;
} else if (direction === 'up') {
snakeY--;
} else if (direction === 'down') {
snakeY++;
}
if (movement === 'right') {
snakeBX++;
} else if (movement === 'left') {
snakeBX--;
} else if (movement === 'up') {
snakeBY--;
} else if (movement === 'down') {
snakeBY++;
}
// game over when snake collide the wall
// game over when snake conllide it's own body (checkCollision function)
if (snakeX == -1 || snakeX == w / snakeSize || snakeY == -1 || snakeY == h / snakeSize || checkCollision(snakeX, snakeY, snake) || checkCollision(snakeX, snakeY, snakeB)) {
// stop the game
// start button enabled again
survivalBtn.removeAttribute('disabled', true);
// reset score
score = 0;
scoreB = 0;
console.log("Player 2 Win!")
// clean up the canvas
ctx.clearRect(0, 0, w, h);
gameloop = clearInterval(gameloop);
return;
} else if (snakeBX == -1 || snakeBX == w / snakeSizeB || snakeBY == -1 || snakeBY == h / snakeSizeB || checkCollision(snakeBX, snakeBY, snakeB) || checkCollision(snakeBX, snakeBY, snake)) {
// stop the game
// start button enabled again
survivalBtn.removeAttribute('disabled', true);
// reset score
score = 0;
scoreB = 0;
console.log("Player 1 Win!")
// clean up the canvas
ctx.clearRect(0, 0, w, h);
gameloop = clearInterval(gameloop);
return;
}
// snake eat food will grow in length
if (snakeX == food.x && snakeY == food.y) {
// add a new square at the end of the array instead of moving the tail to the front
var tail = {
x: snakeX,
y: snakeY
};
// snake eat food will score point
score++;
// create new food.
createFood();
} else {
// pop out the last cell
var tail = snake.pop();
tail.x = snakeX;
tail.y = snakeY;
}
// pop out the tail and shift it to the front of the array
snake.unshift(tail);
// worm eat food will grow in length
if (snakeBX == food.x && snakeBY == food.y) {
// add a new square at the end of the array instead of moving the tail to the front
var tailB = {
x: snakeBX,
y: snakeBY
};
// worm eat food will score point
scoreB++;
// create new food.
createFood();
} else {
// pop out the last cell
var tailB = snakeB.pop();
tailB.x = snakeBX;
tailB.y = snakeBY;
}
// pop out the tail and shift it to the front of the array
snakeB.unshift(tailB);
// create snake using bodySnake function
for (var i = 0; i < snake.length; i++) {
bodySnake(snake[i].x, snake[i].y);
}
// create worm using bodySnakeB function
for (var i=0; i<snakeB.length; i++) {
bodySnakeB(snakeB[i].x, snakeB[i].y);
}
// create food using bait function
bait(food.x, food.y);
// input score for P1 using scoreText function
scoreText();
// input score for P2 using scoreText function
scoreTextB();
}
var initC = function () {
direction = "down";
movement = "down";
drawSnake();
drawSnakeB();
createFood();
gameloop = setInterval(paint, 80);
}
// to expose the init function
return {
initC: initC
};
// end of 2 players survival drawModule
}());
// function to run the snake game
function runGame (drawModule, drawModuleB, drawModuleC) {
// eventListener to link "start game" button to init function
var btn = document.getElementById('btn');
btn.addEventListener("click", function () {
drawModule.init();
drawModuleB.initB();
});
var survivalBtn = document.getElementById('survivalBtn');
survivalBtn.addEventListener("click", function () {
drawModuleC.initC();
});
document.onkeydown = function (event) {
/* keyCode = window.event.keyCode;*/
// to identity the playing keys
playKey = event.keyCode;
switch (playKey) {
// player 1 - left control
case 65:
if (direction != 'right') {
direction = 'left';
console.log('P1 left');
}
break;
// player 1 - right control
case 68:
if (direction != 'left') {
direction = 'right';
console.log('P1 right');
}
break;
// player 1 - up control
case 87:
if (direction != 'down') {
direction = 'up';
console.log('P1 up');
}
break;
// player 1 - down control
case 83:
if (direction != 'up') {
direction = 'down';
console.log('P1 down');
}
break;
// player 2 - left control
case 74:
if (movement != 'right') {
movement = 'left';
console.log('P2 left');
}
break;
// player 2 - right control
case 76:
if (movement != 'left') {
movement = 'right';
console.log('P2 right');
}
break;
// player 2 - up control
case 73:
if (movement != 'down') {
movement = 'up';
console.log('P2 up');
}
break;
// player 2 - down control
case 75:
if (movement != 'up') {
movement = 'down';
console.log('P2 down');
}
break;
}
}
}
// calling the runGame function
runGame(drawModule, drawModuleB, drawModuleC);