-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1764 lines (1462 loc) · 66.6 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define TRUE 1
#define wall_row 20//width of boundary
#define wall_column 40//height of boundary
int i,j,Box[wall_row][wall_column],snake_xcoordinate,snake_ycoordinate,snake_head,snake_tail,snake_temp,game_over,food,food_xcoordinate,food_ycoordinate,user_input,direction,score,high_score,Speed;//global variables that are used in function
FILE *f,*f1;//we are creating file handler
int snake2_xcoordinate,snake2_ycoordinate,snake2_temp,snake2_head,snake2_tail,direction2,user2_input,score1,score2;
char name[20];
COORD coord={0,0}; // this is global variable
//center of axis is set to the top left corner of the screen
void gotoxy(int x,int y)
{
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void snake_body()//it will define all the property of snake like movement position etc
{
f=fopen("highscore.txt","r");//we are opening highscore file in snake body so that we want to open/print the highscore file/data in the starting of the game
fscanf(f,"%d",&high_score);//we are taking the value from the file and storing it in high_score variable
fclose(f);
for(i=0;i<wall_row;i++)
{
for(j=0;j<wall_column;j++)//nested for loop
{
Box[i][j]=0; // we are setting all the values of box field to zero
}
}
snake_xcoordinate=wall_row/2;//starting postion of snake in the middle of the box
snake_ycoordinate=wall_column/2;//snake position is set to middle with snake_xcoordinate =10 &snake_ycoordinate =20
snake_temp=snake_ycoordinate;
snake_head=5;
snake_tail=1;
game_over=0;
food=0;
direction='d';//we are intializing the dirction so that whenever our game starts the snake is moving in some direction
score=0;
Speed=100;
for(i=0;i<snake_head;i++)
{
snake_temp++;
Box[snake_xcoordinate][snake_temp-snake_head] = i+1;//setting coordinate for tail and head to appear
}
}
void print()//it will print boundary ,food and score
{
for(i=0;i<=wall_column+1;i++)//top vertical line of the box
{
if(i==0)
printf("%c",201);//top left corner
else if(i==wall_column+1)
printf("%c",187);//top right corner
else
printf("%c",205);//vertical line joining top left corner and top right corner
}
printf("CURRENT SCORE:%d \t HIGHSCORE:%d ",score,high_score);
printf("\n");
for(i=0;i<wall_row;i++)//will draw two horizontal line from top left corner and top right corner joining bottom left corner and bottom right corner respectively
{
printf("%c",186);
for(j=0;j<wall_column;j++)//NESTED FOR LOOP
{
if(Box[i][j]==0)
printf(" ");//print blank space where ever we have set the value of box fiwld to 0
if(Box[i][j]>0 && Box[i][j]!=snake_head)
printf("%c",176);//print tail of the snake
if(Box[i][j]==snake_head)
printf("%c",178);//prints head of the snake
if(Box[i][j]==-1)
printf("%c",15);
if(j==wall_column-1)
printf("%c\n",186);//horizontal line
}
}
for(i=0;i<=wall_column+1;i++)//bottom vertical line of the box
{
if(i==0)
printf("%c",200);//bottom left corner
else if(i==wall_column+1)
printf("%c",188);//bottom right corner
else
printf("%c",205);//vertival line joining bottom left corner and bottom right corner
}
}
void snake_movementeasy()//in this function the snake can go from all the 4 side of the wall and can come from all the opposite side
{
user_input = input_keyboard();//getting the input from the user and storing it
user_input = tolower(user_input);//converting the input in lower so that we donot worry about whether capslock is on or not
if(((user_input=='w'||user_input=='a')||(user_input=='s'||user_input=='d')) && (abs(direction-user_input)>5))//we are using abs so that snake cannot go onto itself when user press the key(THAT IS If it is running in left direction it cannot move in right direction in the same line and vice versa)
direction = user_input;
if(direction=='w')//up
{
snake_xcoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
{
GameOver();
}
if(snake_xcoordinate==0)//if snake goes from the up side of the wall it comes from the down side of the wall
snake_xcoordinate=wall_row-1;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='a')//left
{
snake_ycoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOver();
if(snake_ycoordinate==0)//if snake goes from the left side of the wall it comes from the right side of the wall
snake_ycoordinate=wall_column-1;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='s')//down
{
snake_xcoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOver();
if(snake_xcoordinate==wall_row-1)//if snake goes from the down side of the wall it comes from the up side of the wall
snake_xcoordinate=0;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='d')//right
{
snake_ycoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOver();
if(snake_ycoordinate==wall_column-1)//if snake goes from the right side of the wall it comes from the left side of the wall
snake_ycoordinate=0;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
}
void snake_movementmoderate()//in this function the snake can go from 2 side of the wall and can come from only 2 opposite side(when snake touches upper and bottom wall the game gets over)
{
user_input = input_keyboard();//getting the input from the user and storing it
user_input = tolower(user_input);//converting the input in lower so that we donot worry about whether capslock is on or not
if(((user_input=='w'||user_input=='a')||(user_input=='s'||user_input=='d')) && (abs(direction-user_input)>5))//we are using abs so that snake cannot go onto itself when user press the key(THAT IS If it is running in left direction it cannot move in right direction in the same line and vice versa)
direction = user_input;
if(direction=='w')//up
{
snake_xcoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOver();
if(snake_xcoordinate==0)//if snake goes from the up side of the wall it comes from the down side of the wall
{
game_over=1;
GameOver();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='a')//left
{
snake_ycoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOver();
if(snake_ycoordinate==0)//if snake goes from the left side of the wall it comes from the right side of the wall
snake_ycoordinate=wall_column-1;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='s')//down
{
snake_xcoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOver();
if(snake_xcoordinate==wall_row-1)//if snake goes from the down side of the wall it comes from the up side of the wall
{
game_over=1;
GameOver();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='d')//right
{
snake_ycoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOver();
if(snake_ycoordinate==wall_column-1)//if snake goes from the right side of the wall it comes from the left side of the wall
snake_ycoordinate=0;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
}
void snake_movementhard()//in this function snakes dies by hitting all the four wall
{
user_input = input_keyboard();//getting the input from the user and storing it
user_input = tolower(user_input);//converting the input in lower so that we donot worry about whether capslock is on or not
if(((user_input=='w'||user_input=='a')||(user_input=='s'||user_input=='d')) && (abs(direction-user_input)>5))//we are using abs so that snake cannot go onto itself when user press the key(THAT IS If it is running in left direction it cannot move in right direction in the same line and vice versa)
{
direction = user_input;
}
if(direction=='w')//up
{
snake_xcoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
{
GameOver();
}
if(snake_xcoordinate==0)//if snake goes from the up side of the wall it comes from the down side of the wall
{
GameOver();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='a')//left
{
snake_ycoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
{
GameOver();
}
if(snake_ycoordinate==0)//if snake goes from the left side of the wall it comes from the right side of the wall
{
GameOver();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='s')//down
{
snake_xcoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
{
GameOver();
}
if(snake_xcoordinate==wall_row-1)//if snake goes from the down side of the wall it comes from the up side of the wall
{
GameOver();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='d')//right
{
snake_ycoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
{
GameOver();
}
if(snake_ycoordinate==wall_column-1)//if snake goes from the right side of the wall it comes from the left side of the wall
{
GameOver();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
}//end of snake_movementhard function
void game_level()//function for selecting game level
{
bold_red();
gotoxy(50,12);
printf("SELECT YOUR LEVEL");
gotoxy(50,13);
printf("1)EASY");
gotoxy(50,14);
printf("2)MEDIUM");
gotoxy(50,15);
printf("3)HARD");
gotoxy(50,16);
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 480);
bold_red();
scanf("%d",&user_input);
switch(user_input)
{
case 1:
{//case 1 starts
system("Cls");
snake_body();
while(game_over==0)
{
print();
ResetScreenPosition();
Food_creation();
snake_movementeasy();
snaketail_movement();
Sleep(Speed); //this will increase this speed of snake
}
break;
}//case 1 ends
case 2:
{//case 2 satarts
system("Cls");
snake_body();
while(game_over==0)
{
print();
ResetScreenPosition();
Food_creation();
snake_movementmoderate();
snaketail_movement();
Sleep(Speed); //this will increase this speed of snake
}
break;
}//case 2 ends
case 3:
{//case 3 satarts
system("Cls");
snake_body();
while(game_over==0)
{
print();
ResetScreenPosition();
Food_creation();
snake_movementhard();
snaketail_movement();
Sleep(Speed); //this will increase this speed of snake
}
break;
}//case 3 ends
default:
{
gotoxy(50,16);
printf("WRONG INPUT");
gotoxy(50,17);
printf("PRESS ENTER TO GO BACK");
while(1)
{
user_input = input_keyboard();//taking input
if(user_input == 13)//if user press enter the game will run again
{
game_details();
game_level();
break;
}
else if(user_input == 27)//if user press escape the game will end
{
break;
}
}
}
}
}
void snakemulti_body()//it will define all the property of snake like movement position etc
{
for(i=0;i<wall_row;i++)
{
for(j=0;j<wall_column;j++)//nested for loop
{
Box[i][j]=0; // we are setting all the values of box field to zero
}
}
snake_xcoordinate=wall_row/2;//starting postion of snake in the middle of the box
snake_ycoordinate=wall_column/2;//snake position is set to middle with snake_xcoordinate =10 &snake_ycoordinate =20
snake_temp=snake_ycoordinate;
snake_head=5;
snake_tail=1;
game_over=0;
food=0;
direction='d';//we are intializing the dirction so that whenever our game starts the snake is moving in some direction
score=0;
Speed=100;
snake2_xcoordinate=wall_row/4;//starting postion of snake in the middle of the box
snake2_ycoordinate=wall_column/4;//snake position is set to middle with snake_xcoordinate =5 &snake_ycoordinate =10
snake2_temp=snake2_ycoordinate;
snake2_head=5;
snake2_tail=1;
direction2=UP;
for(i=0;i<snake_head;i++)
{
snake_temp++;
Box[snake_xcoordinate][snake_temp-snake_head] = i+1;//setting coordinate for tail and head to appear
}
for(i=0;i<snake2_head;i++)
{
snake2_temp++;
Box[snake2_xcoordinate][snake2_temp-snake2_head] = i+1;//setting coordinate for tail and head to appear
}
}
void printmulti()//it will print boundary ,food and score
{
for(i=0;i<=wall_column+1;i++)//top vertical line of the box
{
if(i==0)
printf("%c",201);//top left corner
else if(i==wall_column+1)
printf("%c",187);//top right corner
else
printf("%c",205);//vertical line joining top left corner and top right corner
}
printf("PLAYER1:%d PLAYER2:%d \t ",score1,score2);
printf("\n");
for(i=0;i<wall_row;i++)//will draw two horizontal line from top left corner and top right corner joining bottom left corner and bottom right corner respectively
{
printf("%c",186);
for(j=0;j<wall_column;j++)//NESTED FOR LOOP
{
if(Box[i][j]==0)
printf(" ");//print blank space where ever we have set the value of box fiwld to 0
if(Box[i][j]>0 && Box[i][j]!=snake_head)
printf("%c",176);//print tail of the snake
if(Box[i][j]==snake_head)
printf("%c",178);//prints head of the snake
if(Box[i][j]==-1)
printf("%c",15);
if(j==wall_column-1)
printf("%c\n",186);//horizontal line
}
}
for(i=0;i<=wall_column+1;i++)//bottom vertical line of the box
{
if(i==0)
printf("%c",200);//bottom left corner
else if(i==wall_column+1)
printf("%c",188);//bottom right corner
else
printf("%c",205);//vertival line joining bottom left corner and bottom right corner
}
if(Box[i+4][j+4]>0 && Box[i+4][j+4]!=snake2_head)
bold_red();
printf("%c",176);//print tail of the snake
if(Box[i+4][j+4]==snake2_head)
printf("%c",178);//prints head of the snake
}
void Food_creation()//code for food creation
{
srand(time(0));//as we run our program again and again we have the same random food location(that make our predictable) to over come this situation we will use srand(value) so that it generate food at different locatin
food_xcoordinate = 1+ rand() % 18; //x coordinate of food and we are adding(1) ,we are doing modulus by 18 so that food doesnot come on the boundary
food_ycoordinate = 1 + rand() % 38;//y coordinate of food and we are adding(1) ,we are doing modulus by 38 so that food doesnot come on the boundary
if(food == 0 && Box[food_xcoordinate][food_ycoordinate]==0)//checking if there is food or not and as well as there is empty space in the boc
{
Box[food_xcoordinate][food_ycoordinate]= -1;//we are setting special value to this coordinate so that we can use this while printing the food
food = 1;//it tells the food is already on the screen
if(Speed>10 && score!=0)//as the snake eats the food the speed of snake increases
{
Speed= Speed - 15;//in this we are decreasing it so that our halt time is less
}
}
}
void ResetScreenPosition()//resetting screen
{
HANDLE hOut;//handle allows us to handle the console of the program
COORD Position;//it gives post coordinate of the console screen
hOut = GetStdHandle(STD_OUTPUT_HANDLE);//hout variable(we have created) is used to handle the standard input of the program
Position.X = 0;//assiging top most corner of the output screen to the variable position
Position.Y = 0;
SetConsoleCursorPosition(hOut,Position);//just setting the cursor position
}
int input_keyboard()//creating this function to get the character when keyboard is hit
{
if(_kbhit())//when button is press it returns the character
{
return _getch();
}
else
{
return -1;
}
}
void snakemulti_movementeasy()//in this function the snake can go from all the 4 side of the wall and can come from all the opposite side
{
user_input = input_keyboard();//getting the input from the user and storing it
//user_input = tolower(user_input);//converting the input in lower so that we donot worry about whether capslock is on or not
if(((user_input=='w'||user_input=='a')||(user_input=='s'||user_input=='d')) && (abs(direction-user_input)>5))//we are using abs so that snake cannot go onto itself when user press the key(THAT IS If it is running in left direction it cannot move in right direction in the same line and vice versa)
direction = user_input;
if(((user_input== UP||user_input==DOWN)||(user_input==RIGHT||user_input==LEFT)) && ((abs(direction2-user_input)!=2) && (abs(direction2-user_input)!=8) ))//we are using abs so that snake cannot go onto itself when user press the key(THAT IS If it is running in left direction it cannot move in right direction in the same line and vice versa)
direction2 = user_input;
if(direction=='w')//up
{
snake_xcoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
{
GameOvermulti();
}
if(snake_xcoordinate==0)//if snake goes from the up side of the wall it comes from the down side of the wall
snake_xcoordinate=wall_row-1;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='a')//left
{
snake_ycoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake_ycoordinate==0)//if snake goes from the left side of the wall it comes from the right side of the wall
snake_ycoordinate=wall_column-1;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='s')//down
{
snake_xcoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake_xcoordinate==wall_row-1)//if snake goes from the down side of the wall it comes from the up side of the wall
snake_xcoordinate=0;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='d')//right
{
snake_ycoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake_ycoordinate==wall_column-1)//if snake goes from the right side of the wall it comes from the left side of the wall
snake_ycoordinate=0;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction2==UP)//up
{
snake2_xcoordinate--;
if(Box[snake2_xcoordinate][snake2_ycoordinate]!=0 && Box[snake2_xcoordinate][snake2_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
{
GameOvermulti();
}
if(snake2_xcoordinate==0)//if snake goes from the up side of the wall it comes from the down side of the wall
snake2_xcoordinate=wall_row-1;
if(Box[snake2_xcoordinate][snake2_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score2+=5;//increasing the score when snake eats the food
snake2_tail -= 1;//for increasing snake length
}
snake2_head++;
Box[snake2_xcoordinate][snake2_ycoordinate]= snake2_head;
}
if(direction2==LEFT)//left
{
snake2_ycoordinate--;
if(Box[snake2_xcoordinate][snake2_ycoordinate]!=0 && Box[snake2_xcoordinate][snake2_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake2_ycoordinate==0)//if snake goes from the left side of the wall it comes from the right side of the wall
snake2_ycoordinate=wall_column-1;
if(Box[snake2_xcoordinate][snake2_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score2+=5;//increasing the score when snake eats the food
snake2_tail -= 1;//for increasing snake length
}
snake2_head++;
Box[snake2_xcoordinate][snake2_ycoordinate]= snake2_head;
}
if(direction2==DOWN)//down
{
snake2_xcoordinate++;
if(Box[snake2_xcoordinate][snake2_ycoordinate]!=0 && Box[snake2_xcoordinate][snake2_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake2_xcoordinate==wall_row-1)//if snake goes from the down side of the wall it comes from the up side of the wall
snake2_xcoordinate=0;
if(Box[snake2_xcoordinate][snake2_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score2+=5;//increasing the score when snake eats the food
snake2_tail -= 1;//for increasing snake length
}
snake2_head++;
Box[snake2_xcoordinate][snake2_ycoordinate]= snake2_head;
}
if(direction2==RIGHT)//right
{
snake2_ycoordinate++;
if(Box[snake2_xcoordinate][snake2_ycoordinate]!=0 && Box[snake2_xcoordinate][snake2_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake2_ycoordinate==wall_column-1)//if snake goes from the right side of the wall it comes from the left side of the wall
snake2_ycoordinate=0;
if(Box[snake2_xcoordinate][snake2_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score2+=5;//increasing the score when snake eats the food
snake2_tail -= 1;//for increasing snake length
}
snake2_head++;
Box[snake2_xcoordinate][snake2_ycoordinate]= snake2_head;
}
}
void snakemulti_movementmoderate()//in this function the snake can go from 2 side of the wall and can come from only 2 opposite side(when snake touches upper and bottom wall the game gets over)
{
user_input = input_keyboard();//getting the input from the user and storing it
//user_input = tolower(user_input);//converting the input in lower so that we donot worry about whether capslock is on or not
if(((user_input=='w'||user_input=='a')||(user_input=='s'||user_input=='d')) && (abs(direction-user_input)>5))//we are using abs so that snake cannot go onto itself when user press the key(THAT IS If it is running in left direction it cannot move in right direction in the same line and vice versa)
direction = user_input;
if(((user_input== UP||user_input==DOWN)||(user_input==RIGHT||user_input==LEFT)) && ((abs(direction2-user_input)!=2) && (abs(direction2-user_input)!=8) ))//we are using abs so that snake cannot go onto itself when user press the key(THAT IS If it is running in left direction it cannot move in right direction in the same line and vice versa)
direction2 = user_input;
if(direction=='w')//up
{
snake_xcoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake_xcoordinate==0)//if snake goes from the up side of the wall it comes from the down side of the wall
{
game_over=1;
GameOvermulti();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='a')//left
{
snake_ycoordinate--;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake_ycoordinate==0)//if snake goes from the left side of the wall it comes from the right side of the wall
snake_ycoordinate=wall_column-1;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='s')//down
{
snake_xcoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake_xcoordinate==wall_row-1)//if snake goes from the down side of the wall it comes from the up side of the wall
{
game_over=1;
GameOvermulti();
}
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction=='d')//right
{
snake_ycoordinate++;
if(Box[snake_xcoordinate][snake_ycoordinate]!=0 && Box[snake_xcoordinate][snake_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake_ycoordinate==wall_column-1)//if snake goes from the right side of the wall it comes from the left side of the wall
snake_ycoordinate=0;
if(Box[snake_xcoordinate][snake_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score1+=5;//increasing the score when snake eats the food
snake_tail -= 1;//for increasing snake length
}
snake_head++;
Box[snake_xcoordinate][snake_ycoordinate]= snake_head;
}
if(direction2==UP)//up
{
snake2_xcoordinate--;
if(Box[snake2_xcoordinate][snake2_ycoordinate]!=0 && Box[snake2_xcoordinate][snake2_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake2_xcoordinate==0)//if snake goes from the up side of the wall it comes from the down side of the wall
{
game_over=1;
GameOvermulti();
}
if(Box[snake2_xcoordinate][snake2_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score2+=5;//increasing the score when snake eats the food
snake2_tail -= 1;//for increasing snake length
}
snake2_head++;
Box[snake2_xcoordinate][snake2_ycoordinate]= snake2_head;
}
if(direction2==LEFT)//left
{
snake2_ycoordinate--;
if(Box[snake2_xcoordinate][snake2_ycoordinate]!=0 && Box[snake2_xcoordinate][snake2_ycoordinate]!=-1) //in this we are checking that snake is on the food or not and or it is not on an empty space.if this satisfies the game should end because that means snake has eaten itself
GameOvermulti();
if(snake2_ycoordinate==0)//if snake goes from the left side of the wall it comes from the right side of the wall
snake2_ycoordinate=wall_column-1;
if(Box[snake2_xcoordinate][snake2_ycoordinate]==-1)
{
food = 0;//for creating new food when snake surpasses the food
score2+=5;//increasing the score when snake eats the food
snake2_tail -= 1;//for increasing snake length
}
snake2_head++;
Box[snake2_xcoordinate][snake2_ycoordinate]= snake2_head;
}