Repository navigation
Expand file tree
/
Copy pathSnake_multiPlayer.cpp
More file actions
1297 lines (955 loc) · 35.4 KB
/
Copy pathSnake_multiPlayer.cpp
File metadata and controls
1297 lines (955 loc) · 35.4 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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* S N A K E G A M E
*
* !! M U L T I P L A Y E R !!
*
*
* ~ made using nCurses and Panels :)
* @Date of Completion:
03.12.2018 [DD.MM.YYYY]
* @File:
Snake_multiPlayer.cpp
* @Author:
AMRITANSHU SIKDAR
*/
// Header Files
#include <ncurses.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <panel.h>
using namespace std;
// Custom Defitions
#define SPEED 100000 // units is microseconds
#define HeadOfSnake 'O' // the snake's head
#define BodyOfSnake ACS_LANTERN // the snake's body/tail
#define FoodOfSnake ACS_PI // food of the snake
// Function Declarations
int kbhit(void);
void CommenceTheGame(void);
void welcomeScreen(void);
void assignPositionToSnakes(void);
void gameBorder(void);
void foodNscore(void);
void foodCheck(void);
void playerUP_SNAKE_1(void);
void playerDOWN_SNAKE_1(void);
void playerLEFT_SNAKE_1(void);
void playerRIGHT_SNAKE_1(void);
void playerUP_SNAKE_2(void);
void playerDOWN_SNAKE_2(void);
void playerLEFT_SNAKE_2(void);
void playerRIGHT_SNAKE_2(void);
void checkCollision(void);
void pauseMenu(void);
void gamePlay_Snake_SinglePlayer();
void gamePlay_Snake_MultiPlayer();
void gameOver(void);
void thanksForPlaying(void);
void multiplayerGameMenu(void);
void winnerOfGame(const char*);
void showHighScores(void);
// Global variables
bool newPlay = true; // boolean flag to check whether its a new game
int foodX,foodY; // for snakefood
int snakeHead_1_X,snakeHead_1_Y; // position coordinates of Snake_1
int snakeHead_2_X,snakeHead_2_Y; // position coordinates of Snake_2
int centerX, centerY; // containers for center of the screen
int lengthSnake_1 = 1; // length of the snake 1 body
int lengthSnake_2 = 1; // length of the snake 2 body
int score_1 = 0; // Score of Player (snake) 1
int score_2 = 0; // Score of Player (snake) 2
int snakeTail_1_X[200], snakeTail_1_Y[200]; // the tail of the Snake 1
int snakeTail_2_X[200], snakeTail_2_Y[200]; // the tail of the Snake 2
int i_1 = 0; // directory of the tail of first snake ---- counter variable just to keep track
int i_2 = 0; // directory of the tail of second snake ---- counter variable just to keep track
int gameMenuUserSelection = 0; // self-explanatory
int __init__Snake_1_POSITION_X; // initial positionX of snake 1
int __init__Snake_1_POSITION_Y; // initial positionY of snake 1
int __init__Snake_2_POSITION_X; // initial positionX of snake 2
int __init__Snake_2_POSITION_Y; // initial positionY of snake 2
char playerName_1[10]; // Name of the first player {will also be used for single player name}
char playerName_2[10]; // Name of the second player
/* ~~ START OF MAIN FUNCTION ~~ */
// int main()
// {
// initscr();
// cbreak();
// noecho();
// curs_set(0);
// showHighScores();
// endwin();
// return 0;
// }
int main()
{
CommenceTheGame();
return 0;
}
/* ~~ END OF MAIN FUNCTION ~~ */
/* ~~ ~ -------------------------------------------------------- ~ ~~ */
/* ~~ USER-DEFINED FUNCTION DEFINITIONS ~~ */
/*
Natively not supported in Unix based systems. (only in DOS systems)
A custom defitions is written to implement the same.
It checks for USER_INPUT without blocking the loop process.
*/
int kbhit(void)
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
void CommenceTheGame(void)
{
initscr(); // Initializes the Game Screen a.k.a. Game Console
cbreak(); // Exiting with Ctrl+C in case of loop hang
noecho(); // not displaying the characters that are pressed during the entire gameplay
assignPositionToSnakes();
curs_set(0); // sets the visibility of the terminal cursor (0,1,2)
start_color(); // start coloring the terminal window
welcomeScreen();
multiplayerGameMenu(); // The Main Menu of the Multiplayer Snake Game
// starting the game menu and eventually the gameplay
refresh();
getch();
endwin();
}
void assignPositionToSnakes(void)
{
getmaxyx(stdscr,centerY,centerX); // attains the maximum width and height of the console
centerX /= 2; // center of the standard Terminal Screen - Coordinate X
centerY /= 2; // center of the standard Terminal Screen - Coordinate Y
/*
assigning either CENTER or LEFT alignment to the X Coordinate of Snake 1
depending upon whether the user has chosen SINGLE PLAYER or MULTIPLAYER
*/
if(gameMenuUserSelection == 0)
__init__Snake_1_POSITION_X = centerX;
else if(gameMenuUserSelection == 1)
__init__Snake_1_POSITION_X = centerX - (centerX/2);
__init__Snake_1_POSITION_Y = centerY;
/*
assigning RIGHT alignment to the X Coordinate of Snake 2
X coordinate of Snake 2 can have the same (Right) alignment as it doens't matter in case of
Single Player gameplay
*/
__init__Snake_2_POSITION_X = centerX + (centerX/2);
__init__Snake_2_POSITION_Y = centerY;
snakeHead_1_X = __init__Snake_1_POSITION_X; // initializing the positionX of snake 1
snakeHead_1_Y = __init__Snake_1_POSITION_Y; // initializing the positionY of Snake 1
snakeHead_2_X = __init__Snake_2_POSITION_X; // initializing the positionX of Snake 2
snakeHead_2_Y = __init__Snake_2_POSITION_Y; // initializing the positionY of Snake 2
}
void gameBorder(void)
{
int left,right,top,bottom,topLeftCorner,topRightCorner,bottomLeftCorner,bottomRightCorner;
int line = 1;
topLeftCorner = topRightCorner = bottomLeftCorner = bottomRightCorner = ACS_CKBOARD;
left = right = (int)'|';
top = bottom = (int)'~';
init_pair(1,COLOR_CYAN,COLOR_BLACK); // color pair 1
attron(COLOR_PAIR(1));
attron(A_BOLD);
border(left,right,top,bottom,topLeftCorner,topRightCorner,bottomLeftCorner,bottomRightCorner);
// printing a line just below the scoreboard
mvaddch(3,line,ACS_DIAMOND);
while(line != getmaxx(stdscr)-3)
{
mvprintw(3,line+1,"-");
line++;
}
addch(ACS_DIAMOND);
attroff(COLOR_PAIR(1));
attroff(A_BOLD);
}
void foodNscore(void) // function to print the Food and Score [same function does both the jobs]
{
int height = 3, width = 10, startX, startY; // foodNscore window
int nameLimit = 8; // Why 8? 'cause that's my roll number xD
startX = (getmaxx(stdscr) - width) / 2; startY=1; // Positioning the foodNscore
if(gameMenuUserSelection == 0)
{
// Getting the name of Single Player
if(newPlay == true)
{
WINDOW * player_1_Name_window = newwin(height, width*3, (getmaxy(stdscr)-height)/2,startX-width);
PANEL * player_1_Name_panel = new_panel(player_1_Name_window);
wattron(player_1_Name_window,A_REVERSE);
wattron(player_1_Name_window, A_BOLD);
curs_set(1);
echo();
box(player_1_Name_window,(int)'!',(int)'=');
mvwprintw(player_1_Name_window,1,1,"Enter your Name: "); // Extra spaces for filling up the dark gaps
wmove(player_1_Name_window,1,18);
wgetnstr(player_1_Name_window,playerName_1,nameLimit); // getting the actual input
wattroff(player_1_Name_window,A_REVERSE);
wattroff(player_1_Name_window, A_BOLD);
hide_panel(player_1_Name_panel);
doupdate();
curs_set(0);
noecho();
wclear(player_1_Name_window);
}
refresh();
// ScoreBoard of the Single Player
WINDOW * scoreBoard_Player_1 = newwin(height, width+(nameLimit/2)+1, startY, startX); // Creating the foodNscore {window for scoreboard on the top}
wattron(scoreBoard_Player_1,A_REVERSE);
wattron(scoreBoard_Player_1,A_BOLD);
attron(A_BOLD);
box(scoreBoard_Player_1,(int)'!',(int)'=');
mvwprintw(scoreBoard_Player_1,1,1," "); // filling up white spaces
mvwprintw(scoreBoard_Player_1,1,((getmaxx(scoreBoard_Player_1) - strlen(playerName_1)) / 2) - 2,"%s: %d",playerName_1,score_1); // printing the score
wrefresh(scoreBoard_Player_1); // refreshing foodNscore
wattroff(scoreBoard_Player_1,A_BOLD);
wattroff(scoreBoard_Player_1,A_REVERSE);
}
if(gameMenuUserSelection == 1)
{
// Getting names of First and Second Players
if(newPlay == true)
{
// Player 1
WINDOW * player_1_Name_window = newwin(height, width*3, (getmaxy(stdscr)-height)/2,startX-width);
PANEL * player_1_Name_panel = new_panel(player_1_Name_window);
wattron(player_1_Name_window,A_REVERSE);
wattron(player_1_Name_window, A_BOLD);
curs_set(1);
echo();
box(player_1_Name_window,(int)'!',(int)'=');
mvwprintw(player_1_Name_window,1,1,"Name of Player 1: "); // Extra spaces for filling up the dark gaps
wmove(player_1_Name_window,1,19);
wgetnstr(player_1_Name_window,playerName_1,nameLimit); // getting the actual input
wattroff(player_1_Name_window,A_REVERSE);
wattroff(player_1_Name_window, A_BOLD);
hide_panel(player_1_Name_panel);
doupdate();
wclear(player_1_Name_window);
// Player 2
WINDOW * player_2_Name_window = newwin(height, width*3, (getmaxy(stdscr)-height)/2,startX-width);
PANEL * player_2_Name_panel = new_panel(player_2_Name_window);
wattron(player_2_Name_window,A_REVERSE);
wattron(player_2_Name_window, A_BOLD);
box(player_2_Name_window,(int)'!',(int)'=');
mvwprintw(player_2_Name_window,1,1,"Name of Player 2: "); // Extra spaces for filling up the dark gaps
wmove(player_2_Name_window,1,19);
wgetnstr(player_2_Name_window,playerName_2,nameLimit); // getting the actual input
wattroff(player_2_Name_window,A_REVERSE);
wattroff(player_2_Name_window, A_BOLD);
hide_panel(player_2_Name_panel);
doupdate();
curs_set(0);
noecho();
wclear(player_2_Name_window);
}
refresh();
// ScoreBoard of Player 1
WINDOW * scoreBoard_Player_1 = newwin(height, width+(nameLimit/2)+1, startY, startX - (getmaxx(stdscr)/4)); // Creating the foodNscore
wattron(scoreBoard_Player_1,A_REVERSE);
wattron(scoreBoard_Player_1,A_BOLD);
attron(A_BOLD);
box(scoreBoard_Player_1,(int)'!',(int)'=');
mvwprintw(scoreBoard_Player_1,1,1," "); // filling up white spaces
mvwprintw(scoreBoard_Player_1,1,((getmaxx(scoreBoard_Player_1) - strlen(playerName_1)) / 2) - 2,"%s: %d",playerName_1,score_1); // printing the score
wrefresh(scoreBoard_Player_1); // refreshing foodNscore
wattroff(scoreBoard_Player_1,A_BOLD);
wattroff(scoreBoard_Player_1,A_REVERSE);
// ScoreBoard of Player 2
WINDOW * scoreBoard_Player_2 = newwin(height, width+(nameLimit/2)+1, startY, startX + (getmaxx(stdscr)/4)); // Creating the foodNscore
wattron(scoreBoard_Player_2,A_REVERSE);
wattron(scoreBoard_Player_2,A_BOLD);
attron(A_BOLD);
box(scoreBoard_Player_2,(int)'!',(int)'=');
mvwprintw(scoreBoard_Player_2,1,1," "); // filling up white spaces
mvwprintw(scoreBoard_Player_2,1,((getmaxx(scoreBoard_Player_2) - strlen(playerName_2)) / 2) - 2,"%s: %d",playerName_2,score_2); // printing the score
wrefresh(scoreBoard_Player_2); // refreshing foodNscore
wattroff(scoreBoard_Player_2,A_BOLD);
wattroff(scoreBoard_Player_2,A_REVERSE);
}
// Making the Food!
srand ((unsigned) time(NULL)); // using time as reference to randomize()
foodX = rand() % (getmaxx(stdscr)-4) + 2; // random X coordinate for the snakeFood according to the console size
foodY = rand() % (getmaxy(stdscr)-8) + 6; //random Y coordinate for the snakeFood according to the console size
init_pair(2,COLOR_RED,COLOR_YELLOW);
attron(A_BLINK); // blinking the food
attron(COLOR_PAIR(2));
mvwaddch(stdscr,foodY,foodX,FoodOfSnake); // printing the food item
attroff(A_BLINK);
attroff(COLOR_PAIR(2));
attroff(A_BOLD);
}
void foodCheck(void)
{
if(foodX == snakeHead_1_X && foodY == snakeHead_1_Y)
{
score_1 += 10;
lengthSnake_1++;
foodNscore();
refresh();
}
refresh();
if(foodX == snakeHead_2_X && foodY == snakeHead_2_Y)
{
score_2 += 10;
lengthSnake_2++;
foodNscore();
refresh();
}
refresh();
}
// Player Movement - SnakeBody 1
void playerUP_SNAKE_1(void) // function to move the snake UP
{
attron(A_BOLD);
if(i_1 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_1_X[k] = snakeTail_1_X[k+100];
snakeTail_1_Y[k] = snakeTail_1_Y[k+100];
}
i_1 = 100;
}
mvaddch(snakeHead_1_Y,snakeHead_1_X,BodyOfSnake);
if(i_1 == 0)
mvaddch(__init__Snake_1_POSITION_Y,__init__Snake_1_POSITION_X,' ');
snakeHead_1_Y--; // directing the snake upwards
snakeTail_1_X[i_1] = snakeHead_1_X;
snakeTail_1_Y[i_1] = snakeHead_1_Y;
mvaddch(snakeTail_1_Y[i_1],snakeTail_1_X[i_1],HeadOfSnake);
mvaddch(snakeTail_1_Y[i_1 - lengthSnake_1],snakeTail_1_X[i_1 - lengthSnake_1],' ');
i_1++;
usleep(SPEED/2);
attroff(A_BOLD);
refresh();
}
void playerDOWN_SNAKE_1(void) // function to move the snake DOWN
{
attron(A_BOLD);
if(i_1 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_1_X[k] = snakeTail_1_X[k+100];
snakeTail_1_Y[k] = snakeTail_1_Y[k+100];
}
i_1 = 100;
}
mvaddch(snakeHead_1_Y,snakeHead_1_X,BodyOfSnake);
if(i_1 == 0)
mvaddch(__init__Snake_1_POSITION_Y,__init__Snake_1_POSITION_X,' ');
snakeHead_1_Y++; // directing the snake downwards
snakeTail_1_X[i_1] = snakeHead_1_X;
snakeTail_1_Y[i_1] = snakeHead_1_Y;
mvaddch(snakeTail_1_Y[i_1],snakeTail_1_X[i_1],HeadOfSnake);
mvaddch(snakeTail_1_Y[i_1 - lengthSnake_1],snakeTail_1_X[i_1 - lengthSnake_1],' ');
i_1++;
usleep(SPEED/2);
attroff(A_BOLD);
refresh();
}
void playerLEFT_SNAKE_1(void) // function to move the snake LEFT
{
attron(A_BOLD);
if(i_1 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_1_X[k] = snakeTail_1_X[k+100];
snakeTail_1_Y[k] = snakeTail_1_Y[k+100];
}
i_1 = 100;
}
mvaddch(snakeHead_1_Y,snakeHead_1_X,BodyOfSnake);
if(i_1 == 0)
mvaddch(__init__Snake_1_POSITION_Y,__init__Snake_1_POSITION_X,' ');
snakeHead_1_X--; // directing the snake to the left
snakeTail_1_X[i_1] = snakeHead_1_X;
snakeTail_1_Y[i_1] = snakeHead_1_Y;
mvaddch(snakeTail_1_Y[i_1],snakeTail_1_X[i_1],HeadOfSnake);
mvaddch(snakeTail_1_Y[i_1 - lengthSnake_1],snakeTail_1_X[i_1 - lengthSnake_1],' ');
i_1++;
usleep(SPEED/3);
attroff(A_BOLD);
refresh();
}
void playerRIGHT_SNAKE_1(void) // function to move the snake RIGHT
{
attron(A_BOLD);
if(i_1 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_1_X[k] = snakeTail_1_X[k+100];
snakeTail_1_Y[k] = snakeTail_1_Y[k+100];
}
i_1 = 100;
}
mvaddch(snakeHead_1_Y,snakeHead_1_X,BodyOfSnake);
if(i_1 == 0)
mvaddch(__init__Snake_1_POSITION_Y,__init__Snake_1_POSITION_X,' ');
snakeHead_1_X++; // directing the snake to the right
snakeTail_1_X[i_1] = snakeHead_1_X;
snakeTail_1_Y[i_1] = snakeHead_1_Y;
mvaddch(snakeTail_1_Y[i_1],snakeTail_1_X[i_1],HeadOfSnake);
mvaddch(snakeTail_1_Y[i_1 - lengthSnake_1],snakeTail_1_X[i_1 - lengthSnake_1],' ');
i_1++;
usleep(SPEED/3);
attroff(A_BOLD);
refresh();
}
// Player Movement - SnakeBody 2
void playerUP_SNAKE_2(void) // function to move the snake UP
{
attron(A_BOLD);
if(i_2 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_2_X[k] = snakeTail_2_X[k+100];
snakeTail_2_Y[k] = snakeTail_2_Y[k+100];
}
i_2 = 100;
}
mvaddch(snakeHead_2_Y,snakeHead_2_X,BodyOfSnake);
if(i_2 == 0)
mvaddch(__init__Snake_2_POSITION_Y,__init__Snake_2_POSITION_X,' ');
snakeHead_2_Y--; // directing the snake upwards
snakeTail_2_X[i_2] = snakeHead_2_X;
snakeTail_2_Y[i_2] = snakeHead_2_Y;
mvaddch(snakeTail_2_Y[i_2],snakeTail_2_X[i_2],HeadOfSnake);
mvaddch(snakeTail_2_Y[i_2 - lengthSnake_2],snakeTail_2_X[i_2 - lengthSnake_2],' ');
i_2++;
usleep(SPEED/2);
attroff(A_BOLD);
refresh();
}
void playerDOWN_SNAKE_2(void) // function to move the snake DOWN
{
attron(A_BOLD);
if(i_2 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_2_X[k] = snakeTail_2_X[k+100];
snakeTail_2_Y[k] = snakeTail_2_Y[k+100];
}
i_2 = 100;
}
mvaddch(snakeHead_2_Y,snakeHead_2_X,BodyOfSnake);
if(i_2 == 0)
mvaddch(__init__Snake_2_POSITION_Y,__init__Snake_2_POSITION_X,' ');
snakeHead_2_Y++; // directing the snake downwards
snakeTail_2_X[i_2] = snakeHead_2_X;
snakeTail_2_Y[i_2] = snakeHead_2_Y;
mvaddch(snakeTail_2_Y[i_2],snakeTail_2_X[i_2],HeadOfSnake);
mvaddch(snakeTail_2_Y[i_2 - lengthSnake_2],snakeTail_2_X[i_2 - lengthSnake_2],' ');
i_2++;
usleep(SPEED/2);
attroff(A_BOLD);
refresh();
}
void playerLEFT_SNAKE_2(void) // function to move the snake LEFT
{
attron(A_BOLD);
if(i_2 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_2_X[k] = snakeTail_2_X[k+100];
snakeTail_2_Y[k] = snakeTail_2_Y[k+100];
}
i_2 = 100;
}
mvaddch(snakeHead_2_Y,snakeHead_2_X,BodyOfSnake);
if(i_2 == 0)
mvaddch(__init__Snake_2_POSITION_Y,__init__Snake_2_POSITION_X,' ');
snakeHead_2_X--; // directing the snake to the left
snakeTail_2_X[i_2] = snakeHead_2_X;
snakeTail_2_Y[i_2] = snakeHead_2_Y;
mvaddch(snakeTail_2_Y[i_2],snakeTail_2_X[i_2],HeadOfSnake);
mvaddch(snakeTail_2_Y[i_2 - lengthSnake_2],snakeTail_2_X[i_2 - lengthSnake_2],' ');
i_2++;
usleep(SPEED/3);
attroff(A_BOLD);
refresh();
}
void playerRIGHT_SNAKE_2(void) // function to move the snake RIGHT
{
attron(A_BOLD);
if(i_2 == 200)
{
for(int k = 0; k < 100; k++)
{
snakeTail_2_X[k] = snakeTail_2_X[k+100];
snakeTail_2_Y[k] = snakeTail_2_Y[k+100];
}
i_2 = 100;
}
mvaddch(snakeHead_2_Y,snakeHead_2_X,BodyOfSnake);
if(i_2 == 0)
mvaddch(__init__Snake_2_POSITION_Y,__init__Snake_2_POSITION_X,' ');
snakeHead_2_X++; // directing the snake to the right
snakeTail_2_X[i_2] = snakeHead_2_X;
snakeTail_2_Y[i_2] = snakeHead_2_Y;
mvaddch(snakeTail_2_Y[i_2],snakeTail_2_X[i_2],HeadOfSnake);
mvaddch(snakeTail_2_Y[i_2 - lengthSnake_2],snakeTail_2_X[i_2 - lengthSnake_2],' ');
i_2++;
usleep(SPEED/3);
attroff(A_BOLD);
refresh();
}
// To print the GAME OVER message when the snake gets killed
void gameOver(void)
{
int height = 3, width = 30, startX, startY;
startX = (getmaxx(stdscr) - width) / 2;
startY = getmaxy(stdscr) / 2;
WINDOW * gameOver = newwin(height, width, startY, startX);
box(gameOver,(int)'!',(int)'='); // border over the "GameOver" message
init_pair(3,COLOR_BLACK,COLOR_WHITE);
wattron(gameOver,A_BOLD);
wattron(gameOver,A_STANDOUT);
wattron(gameOver,COLOR_PAIR(3));
mvwprintw(gameOver,getmaxy(gameOver)/2,3,"!! G A M E O V E R !!");
wattroff(gameOver,A_BOLD);
wattroff(gameOver,A_STANDOUT);
wattroff(gameOver,COLOR_PAIR(3));
wrefresh(gameOver);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 13); // waits till ENTER key is pressed
wclear(gameOver);
wrefresh(gameOver);
multiplayerGameMenu();
refresh();
}
void pauseMenu(void)
{
int height = 8, width = 20, startX = (getmaxx(stdscr) - width) / 2, startY = (getmaxy(stdscr) - height) / 2, choice;
char menuOptions[2][15] = {"Continue Game", "Exit"};
int maxx=0; // limiting the boundary of Exit button [BUG FIX]
int pauseMenuUserSelection = 0;
WINDOW * pauseMenu_window = newwin(height, width, startY, startX);
PANEL * pauseMenu_panel = new_panel(pauseMenu_window);
box(pauseMenu_window,(int)'^',(int)'|');
keypad(pauseMenu_window, TRUE);
while(true)
{
for(int j = 0; j < 2; j++)
{
if(j == pauseMenuUserSelection)
{
wattron(pauseMenu_window,A_REVERSE);
}
// adjusting the "Exit" option, as it was going out of bounds
if(j == 0)
maxx = 5;
else if(j == 1)
maxx = 3;
mvwprintw(pauseMenu_window, ((getmaxy(pauseMenu_window) - strlen(menuOptions[j])) / 2) + maxx, (getmaxx(pauseMenu_window) - strlen(menuOptions[j])) / 2, menuOptions[j]);
wattroff(pauseMenu_window,A_REVERSE);
}
choice = wgetch(pauseMenu_window);
switch(choice)
{
case KEY_UP:
pauseMenuUserSelection--;
if(pauseMenuUserSelection == -1)
pauseMenuUserSelection = 0;
break;
case KEY_DOWN:
pauseMenuUserSelection++;
if(pauseMenuUserSelection == 2)
pauseMenuUserSelection = 1;
break;
default:
break;
}
if(choice == 10) // pressing the enter/return key
{
wclear(pauseMenu_window);
break;
}
}
if(pauseMenuUserSelection == 0 && gameMenuUserSelection == 0)
{
hide_panel(pauseMenu_panel);
refresh();
gamePlay_Snake_SinglePlayer();
}
else if(pauseMenuUserSelection == 0 && gameMenuUserSelection == 1)
{
hide_panel(pauseMenu_panel);
refresh();
gamePlay_Snake_MultiPlayer();
}
else if(pauseMenuUserSelection == 1)
{
wclear(stdscr);
wrefresh(stdscr);
newPlay = true;
multiplayerGameMenu();
}
update_panels(); // Updating the Panels
doupdate(); // Showing it on the Terminal Screen
refresh();
}
void checkCollision(void)
{
// checking collision with just the boundaries
if(snakeHead_1_Y<5 || snakeHead_1_X<2 || snakeHead_1_X>getmaxx(stdscr)-3 || snakeHead_1_Y>getmaxy(stdscr)-3) // in case the snake 1 head hits the game boundaries
{
score_1 = 0;
score_2 = 0;
lengthSnake_1 = 1;
lengthSnake_2 = 1;
newPlay = true;
if(gameMenuUserSelection == 1)
winnerOfGame("! ~ Player 2 WINS ~ !");
else if(gameMenuUserSelection == 0)
gameOver();
refresh();
}
if(snakeHead_2_Y<5 || snakeHead_2_X<2 || snakeHead_2_X>getmaxx(stdscr)-3 || snakeHead_2_Y>getmaxy(stdscr)-3) // in case the snake 2 head hits the game boundaries
{
score_1 = score_2 = 0;
lengthSnake_1 = lengthSnake_2 = 1;
newPlay = true;
winnerOfGame("! ~ Player 1 WINS ~ !");
refresh();
}
attron(COLOR_PAIR(1));
mvaddch(0,0,ACS_CKBOARD);
attroff(COLOR_PAIR(1));
}
// Gameplay Of Snake 1
void gamePlay_Snake_SinglePlayer(void)
{
int arrow_input; // takes in input from the arrow keys to control the snake
keypad(stdscr,TRUE); // initializes keyboard input
gameBorder(); // printing the game borders
foodNscore(); // initializing the Food and ScoreBoard
foodCheck(); // calling the referee
if(newPlay == true)
{
i_1 = 0;
newPlay = false;
clear();
gameBorder(); // printing the game borders
foodNscore(); // initializing the Food and ScoreBoard
foodCheck(); // calling the referee
refresh();
}
while(true)
{
arrow_input = getch(); // takes input from the user
foodCheck();
switch(arrow_input)
{
case KEY_UP:
{
while(!kbhit())
{
playerUP_SNAKE_1();
checkCollision();
foodCheck();
refresh();
}
break;
}
case KEY_LEFT:
{
while(!kbhit())
{
playerLEFT_SNAKE_1();
checkCollision();
foodCheck();
refresh();
}
break;
}
case KEY_DOWN:
{
while(!kbhit())
{
playerDOWN_SNAKE_1();
checkCollision();
foodCheck();
refresh();
}
break;
}
case KEY_RIGHT:
{
while(!kbhit())
{
playerRIGHT_SNAKE_1();
checkCollision();
foodCheck();
refresh();
}
break;
}
default:
pauseMenu();
refresh();
break;
}
refresh();
}
}
// Gameplay of Snake 2
void gamePlay_Snake_MultiPlayer(void)
{
char mp1, mp2, multiplayerInput; // takes in input from the arrow keys to control the snake
keypad(stdscr,TRUE); // initializes keyboard input
gameBorder(); // printing the game borders
foodNscore(); // initializing the Food and ScoreBoard
foodCheck(); // calling the referee
if(newPlay == true)
{
i_1 = 0;
i_2 = 0;
newPlay = false;
clear();
gameBorder(); // printing the game borders
foodNscore(); // initializing the Food and ScoreBoard
foodCheck(); // calling the referee
refresh();
}
while(true)
{
if(kbhit())
{
multiplayerInput = getch(); // takes input from the user
switch(multiplayerInput)
{