-
Notifications
You must be signed in to change notification settings - Fork 0
/
SNAKE.CPP
974 lines (952 loc) · 23.2 KB
/
SNAKE.CPP
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
/*
------------
SNAKE XENZIA
------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a basic snake game which is commonly
found in old nokia phones. This game has a
snake the player has to move with the arrow
keys. There will be food particles generated
on the screen from time to time. When the
snake eats them, its length increases. The
game ends when the snake bits its own body.
The player's objective is to get the highest
possible score by making the snake as long as
possible.
Features of this game:
1. 3 difficulty levels: Easy, Medium, Hard
Harder the difficulty level, faster will
be the movement of the snake.
2. The player can choose 3 different screen
sizes: Small, Medium, Large
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Created by: Anirudh Panigrahi
XI-E
Remal Public School, Sector-3
Rohini, New Delhi
Roll no.-11534
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h> //for remove()
#define ARR_X 100
#define ARR_Y 100 //dimensions of pos_key matrix
char pos_key[ARR_X][ARR_Y]; //matrix corresponding to every position on the screen
void keystroke(int); //function which receives and maps keystrokes and decides the corresponding movement
void adjustxy(int&, int&); //helps in swapping screen sides when snake reaches one edge of screen
void movehead(int, int); //prints the head of the snake
void movetail(char [ARR_X][ARR_Y]); //replaces the last element of the snake with " "
void addfood(); //adds food particles in the game
int checkfood(); //checks if snake has eaten food or not
int checkdie(); //checks if snake has bitten itself or not
void printgameover(); //terminates game if checkdie() is true
char getdir(); //converts arrow key input to alphabets
void screen(); //sets all screen parameters at the beginning of the program
void frame(); //prints the borders of the screen
int pause(); //manages pause option
int select(int , int , int [], int); //helps in option selection in the game menus with arrow keys
void highscore(int); //to print and modify highscores
void continuegame(int, char &); //to continue a previously incomplete game
int MAX_X; //maximum horizontal coordinate of screen
int MIN_X; //minimum horizontal coordinate of screen
int MAX_Y; //maximum vertical coordinate of screen
int MIN_Y; //minimum vertical coordinate of screen
int max_xs; //MAX_X for small screen
int max_xm; //MAX_X for medium screen
int max_xl; //MAX_X for large screen
int max_ys; //MAX_X for small screen
int max_ym; //MAX_X for medium screen
int max_yl; //MAX_X for large screen
int x, y; //head coordinates
int x1, y1; //tail coordinates
int x2, y2; //food coordinates
int snaklen; //records length of the snake(to show score at end)
int frame_width = 150; //sets default game speed
int counter = 0; //records the no. of times the game is played
int savedgamestatus; //records if a saved game is present or not
void main()
{
if(counter==0)
//FUNCTIONS TO RUN ONLY AT THE START OF THE PROGRAM//
{
randomize(); //to seed the random() function in addfood()
screen();
continuegame(2, 'k');
}
///////WELCOME SCREEN//////////
int choice;
char choice2;
flag3:
snaklen = 1;
for(int l = 0; l<ARR_X; ++l)
{
for(int m = 0; m<ARR_Y; ++m)
{
pos_key[l][m] = 'k'; //to reset pos_key at start of every new game
}
}
clrscr(); frame();
_setcursortype(_NOCURSOR);
////////MAIN MENU/////////
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 - 6);
textcolor(YELLOW);
cprintf("********************");
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 - 4);
cprintf("****");
textcolor(MAGENTA);
cprintf("SNAKE XENZIA");
textcolor(YELLOW);
cprintf("****");
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 - 2);
cprintf("********************");
textcolor(WHITE);
if(!savedgamestatus) //if a saved game is not present
{ textcolor(DARKGRAY); }
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2);
cprintf("1. Continue");
textcolor(WHITE);
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 1);
cout<<"2. New Game";
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 2);
cout<<"3. Controls";
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 3);
cout<<"4. Options";
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 4);
cout<<"5. Highscores";
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 5);
cout<<"6. Exit";
/////////END OF MAIN MENU/////////
int pos_gap1[12] = {1, 1, 1, 1, 1, 1, -1};
choice = select((MAX_X - MIN_X + 1)/2 - 11, (MAX_Y - MIN_Y + 1)/2, pos_gap1, 6);
switch (choice)
{
case 1 :
if(savedgamestatus) keystroke(1);
break;
case 2 :
clrscr(); frame();
x = (MAX_X - MIN_X + 1)/2, y = (MAX_Y - MIN_Y + 1)/2;
addfood();
keystroke(0);
break;
case 3 :
clrscr(); frame();
gotoxy((MAX_X - MIN_X + 1)/2 - 3, (MAX_Y - MIN_Y + 1)/2 - 3);
textcolor(YELLOW);
cprintf("CONTROLS");
textcolor(WHITE);
gotoxy((MAX_X - MIN_X + 1)/2 - 15, (MAX_Y - MIN_Y + 1)/2 - 1);
cout<<"1. Esc - Pauses the game";
gotoxy((MAX_X - MIN_X + 1)/2 - 15, (MAX_Y - MIN_Y + 1)/2 + 1);
cout<<"2. Arrow keys to move the snake";
gotoxy((MAX_X - MIN_X + 1)/2 - 18, (MAX_Y - MIN_Y + 1)/2 + 3);
cout<<"Press any key to return to main menu...";
getch();
goto flag3;
case 4 :
flag4:
clrscr(); frame();
////////OPTIONS MENU////////
gotoxy((MAX_X - MIN_X + 1)/2 - 3, (MAX_Y - MIN_Y + 1)/2 - 6);
textcolor(YELLOW);
cprintf("OPTIONS");
textcolor(WHITE);
gotoxy((MAX_X - MIN_X + 1)/2 - 8, (MAX_Y - MIN_Y + 1)/2 - 4);
cout<<"1. Set difficulty level: ";
gotoxy((MAX_X - MIN_X + 1)/2 - 7, (MAX_Y - MIN_Y + 1)/2 - 3);
cout<<"1.1 Easy";
gotoxy((MAX_X - MIN_X + 1)/2 - 7, (MAX_Y - MIN_Y + 1)/2 - 2);
cout<<"1.2 Medium";
gotoxy((MAX_X - MIN_X + 1)/2 - 7, (MAX_Y - MIN_Y + 1)/2 - 1);
cout<<"1.3 Hard";
gotoxy((MAX_X - MIN_X + 1)/2 - 8, (MAX_Y - MIN_Y + 1)/2 + 1);
cout<<"2. Set screen size";
gotoxy((MAX_X - MIN_X + 1)/2 - 7, (MAX_Y - MIN_Y + 1)/2 + 2);
cout<<"2.1 Small";
gotoxy((MAX_X - MIN_X + 1)/2 - 7, (MAX_Y - MIN_Y + 1)/2 + 3);
cout<<"2.2 Medium";
gotoxy((MAX_X - MIN_X + 1)/2 - 7, (MAX_Y - MIN_Y + 1)/2 + 4);
cout<<"2.3 Large";
gotoxy((MAX_X - MIN_X + 1)/2 - 8, (MAX_Y - MIN_Y + 1)/2 + 6);
cout<<"3. Back to main menu";
int pos_gap2[12] = {1, 1, 3, 3, 3, 1, 1, 2, 2, 1, -1};
choice = select((MAX_X - MIN_X + 1)/2 - 9, (MAX_Y - MIN_Y + 1)/2 - 3, pos_gap2, 10);
switch (choice)
{
case 1:
frame_width = 150;
gotoxy((MAX_X - MIN_X + 1)/2 - 16, (MAX_Y - MIN_Y + 1)/2 + 9);
textcolor(GREEN);
cprintf("The difficulty level is now set to 'Easy'");
textcolor(WHITE);
getch();
goto flag4;
case 2:
frame_width = 100;
gotoxy((MAX_X - MIN_X + 1)/2 - 16, (MAX_Y - MIN_Y + 1)/2 + 9);
textcolor(BLUE);
cprintf("The difficulty level is now set to 'Medium'");
textcolor(WHITE);
getch();
goto flag4;
case 3:
frame_width = 50;
gotoxy((MAX_X - MIN_X + 1)/2 - 16, (MAX_Y - MIN_Y + 1)/2 + 9);
textcolor(RED);
cprintf("The difficulty level is now set to 'Hard'");
textcolor(WHITE);
getch();
goto flag4;
case 6:
MAX_X = max_xs;
MAX_Y = max_ys;
clrscr(); frame();
gotoxy((MAX_X - MIN_X + 1)/2 - 16, (MAX_Y - MIN_Y + 1)/2 + 9);
textcolor(CYAN);
cprintf("The screen size is now set to 'Small'");
textcolor(WHITE);
getch();
goto flag4;
case 7:
MAX_X = max_xm;
MAX_Y = max_ym;
clrscr(); frame();
gotoxy((MAX_X - MIN_X + 1)/2 - 16, (MAX_Y - MIN_Y + 1)/2 + 9);
textcolor(CYAN);
cprintf("The screen size is now set to 'Medium'");
textcolor(WHITE);
getch();
goto flag4;
case 8:
MAX_X = max_xl;
MAX_Y = max_yl;
clrscr(); frame();
gotoxy((MAX_X - MIN_X + 1)/2 - 16, (MAX_Y - MIN_Y + 1)/2 + 9);
textcolor(CYAN);
cprintf("The screen size is now set to 'Large'");
textcolor(WHITE);
getch();
goto flag4;
case 10:
goto flag3;
}
break;
////////END OF OPTIONS MENU/////////
case 5 :
highscore(0);
break;
case 6 :
clrscr(); frame();
gotoxy((MAX_X - MIN_X + 1)/2 - 15, (MAX_Y - MIN_Y + 1)/2 - 1);
textcolor(LIGHTRED);
cprintf("Are you sure you want to exit?");
gotoxy((MAX_X - MIN_X + 1)/2 - 15, (MAX_Y - MIN_Y + 1)/2 + 1);
cprintf("(hit key y or n...)");
textcolor(WHITE);
choice2 = getch();
if(choice2=='y' || choice2=='Y')
{
exit(0);
}
else
{
goto flag3;
}
break;
}
goto flag3;
/////END OF WELCOME SCREEN/////
/////END OF MAIN/////
}
void keystroke(int tocontinue)//tocontinue: determines whether to continue a saved game or not
{
flag:
movehead(x, y); //to show initial direction of the snake
char c0, c;
if(tocontinue == 0)
{ c = getdir(); }
else
{ c = 'w'; }
//TO SET INITIAL TAIL COORDINATES//
switch(c)
{
case 'w':
x1 = x;
y1 = y+1;
break;
case 's':
x1 = x;
y1 = y-1;
break;
case 'a':
x1 = x+1;
y1 = y;
break;
case 'd':
x1 = x-1;
y1 = y;
break;
case 27 :
if(pause())
{
return; //gives the option of exiting the game before starting to play
}
else
{
goto flag;
}
default :
goto flag;
}
pos_key[x1][y1] = c;
if(tocontinue != 0)
{
continuegame(1, c);
if(pause())
{
continuegame(0, c);
return;
}
if(kbhit())
{
c = getdir();
}
}
//LOOPS TO SET HEAD COORDINATES, MAP THE DIRECTION VALUE OF THE HEAD//
//CORRESPONDING TO ITS POSITION AND CALL MOVEHEAD() AND MOVETAIL() //
flag2:
switch(c)
{
case 'w':
do
{
y--;
adjustxy(x, y);
movehead(x, y);
y==MAX_Y ? pos_key[x][MIN_Y] : pos_key[x][y+1] = 'w';
movetail(pos_key);
printgameover();
delay(frame_width);
}while(!kbhit());
break;
case 's':
do
{
y++;
adjustxy(x, y);
movehead(x, y);
y==MIN_Y ? pos_key[x][MAX_Y] : pos_key[x][y-1] = 's';
movetail(pos_key);
printgameover();
delay(frame_width);
}while(!kbhit());
break;
case 'a':
do
{
x--;
adjustxy(x, y);
movehead(x, y);
x==MAX_X ? pos_key[MIN_X][y] : pos_key[x+1][y] = 'a';
movetail(pos_key);
printgameover();
delay(frame_width);
}while(!kbhit());
break;
case 'd':
do
{
x++;
adjustxy(x, y);
movehead(x, y);
x==MIN_X ? pos_key[MAX_X][y] : pos_key[x-1][y] = 'd';
movetail(pos_key);
printgameover();
delay(frame_width);
}while(!kbhit());
break;
}
c0 = getdir();
//TO IGNORE OPPOSITE DIRECTION KEYSTROKE AND ANY OTHER KEYSTROKE//
if(c0==27)
{
if(pause())
{
continuegame(0, c);
return;
}
else
{
goto flag2;
}
}
else
{
switch (c)
{
case 'w':
if (c0=='a' || c0=='d')
{
c = c0;
goto flag2;
}
else
{
goto flag2;
}
case 's':
if (c0=='a' || c0=='d')
{
c = c0;
goto flag2;
}
else
{
goto flag2;
}
case 'a':
if (c0=='w' || c0=='s')
{
c = c0;
goto flag2;
}
else
{
goto flag2;
}
case 'd':
if (c0=='w' || c0=='s')
{
c = c0;
goto flag2;
}
else
{
goto flag2;
}
}
}
}
void adjustxy(int &x, int &y)
{
if (y == MAX_Y + 1)
y = MIN_Y;
else if (y == MIN_Y - 1)
y = MAX_Y;
if (x == MAX_X + 1)
x = MIN_X;
else if (x == MIN_X - 1)
x = MAX_X;
// gotoxy(x, y);
// cout<<"@";//<<x<<", "<<y; //for testing purposes
return;
}
void movehead (int x, int y)
{
gotoxy(x, y);
textcolor(WHITE);
cprintf("@");
// delay(500); //for testing purposes
}
void movetail (char pos_key[ARR_X][ARR_Y])
{
if(checkfood()!=0)
{
++snaklen;
addfood();
return;
}
else if (checkfood()==0)
{
//TO SET THE NEXT TAIL COORDINATES ACCORDING TO THE DIRECTION //
//VALUE STORED IN POS_KEY AT THE EXISTING POSITION OF THE TAIL//
switch(pos_key[x1][y1])
{
case 'w':
pos_key[x1][y1] = ' ';
y1--;
adjustxy(x1, y1);
gotoxy(x1, y1);
cout<<" ";
break;
case 's':
pos_key[x1][y1] = ' ';
y1++;
adjustxy(x1, y1);
gotoxy(x1, y1);
cout<<" ";
break;
case 'a':
pos_key[x1][y1] = ' ';
x1--;
adjustxy(x1, y1);
gotoxy(x1, y1);
cout<<" ";
break;
case 'd':
pos_key[x1][y1] = ' ';
x1++;
adjustxy(x1, y1);
gotoxy(x1, y1);
cout<<" ";
break;
}
}
// delay(500); //for testing purposes
return;
}
void addfood()
{
//LOOP WILL RUN UNTIL X2, Y2 ARE SET TO VALUES WHICH ARE NOT ON THE SNAKE//
do
{
x2 = random(MAX_X - MIN_X + 1) + MIN_X;
y2 = random(MAX_Y - MIN_Y + 1) + MIN_Y;
}while(x2==x && y2==y || (pos_key[x2][y2]=='w' || pos_key[x2][y2]=='s' ||
pos_key[x2][y2]=='a' || pos_key[x2][y2]=='d' ));
gotoxy(x2, y2);
textcolor(LIGHTMAGENTA);
cprintf("@");
textcolor(WHITE);
return;
}
int checkfood()
{
if(x==x2 && y==y2)
{
return 1;
}
else
{
return 0;
}
}
int checkdie()
{
////IF POS_KEY[X][Y] IS ALREADY MAPPED WITH A////
////DIRECTION VALUE WHEN THE HEAD REACHES (X, Y) COORDINATES////
if(pos_key[x][y]=='w' || pos_key[x][y]=='s' ||
pos_key[x][y]=='a' || pos_key[x][y]=='d' )
{
return 1;
}
else
{
return 0;
}
}
void printgameover()
{
if(checkdie()!=0)
{
int k = 1;
delay(frame_width);
//LOOP TO FLASH "GAME OVER" 4 TIMES//
do
{
textcolor(RED);
gotoxy((MAX_X - MIN_X + 1)/2 - 5 , (MAX_Y - MIN_Y + 1)/2);
cprintf("GAME OVER!!\a");
textcolor(WHITE);
delay(400);
clrscr(); frame();
delay(400);
++k;
}while(k<=4);
while(kbhit()) //to ignore keystrokes pressed while displaying game over//
{ //so that final score can be visible//
getch();
}
textcolor(RED);
gotoxy((MAX_X - MIN_X + 1)/2 - 5 , (MAX_Y - MIN_Y + 1)/2);
cprintf("GAME OVER!!");
textcolor(WHITE);
gotoxy((MAX_X - MIN_X + 1)/2 - 8 , (MAX_Y - MIN_Y + 1)/2 + 1);
cout<<"Final Score: "<<snaklen*10;
gotoxy((MAX_X - MIN_X + 1)/2 - 12 , (MAX_Y - MIN_Y + 1)/2 + 2);
cout<<"Press any key to exit...";
getch();
while(kbhit()) { getch(); }
highscore(snaklen*10);
++counter;
main();
}
}
char getdir()
{
char ch = getch();
if(ch==0)
{
ch = getch();
switch(ch)
{
case 'H':
return 'w';
case 'P':
return 's';
case 'K':
return 'a';
case 'M':
return 'd';
default :
return 'x';
}
}
else if(ch==27)
{
return ch;
}
else
{
return 'x'; //any random character, so that it can be ignored by keystroke()
}
}
void screen()
{
struct text_info info;
gettextinfo(&info);
MAX_X = (int) info.winright - 1;
MIN_X = (int) info.winleft + 1;
MAX_Y = (int) info.winbottom - 2;
MIN_Y = (int) info.wintop + 1;
max_xs = (int) MAX_X * 0.8;
max_xm = (int) MAX_X * 0.9;
max_xl = (int) MAX_X;
max_ys = (int) MAX_Y * 0.8;
max_ym = (int) MAX_Y * 0.9;
max_yl = (int) MAX_Y;
frame();
return;
}
void frame()
{
int width = MAX_X - MIN_X + 3;
int height = MAX_Y - MIN_Y + 4;
textcolor(YELLOW);
gotoxy(1,1);
for(int i = 0; i < width; i++)
{
cprintf("%c", '*');
}
for(i = 2; i <= height - 2; i++)
{
gotoxy(1, i); cprintf ("%c", '*');
gotoxy(width, i); cprintf ("%c", '*');
}
gotoxy(1, height - 1);
for(i = 0; i < width; i++)
{
cprintf ("%c", '*');
}
textcolor(WHITE);
}
int pause()
{
textcolor(GREEN);
gotoxy((MAX_X-MIN_X+1)/2 - 15, MIN_Y - 1);
cprintf("PAUSED*Press esc again to exit");
gotoxy((MAX_X-MIN_X+1)/2 - 15, MAX_Y + 1);
cprintf("Press any other key to resume");
textcolor(WHITE);
char ch = getch();
frame();
if(ch==27)
{
return 1;
}
else if (ch == 0)
{
ungetch(0); //so that arrow keys may be responded to by keystroke when pressed during pause()
return 0;
}
else
{
return 0;
}
}
int select(int x_opt, int y_opt, int pos_gap[], int totalgap)
//X_OPT - X COORDINATE OF THE BULLET IN ALL POSITIONS//
//Y_OPT - Y COORDINATE OF BULLET FOR FIRST OPTION//
//POS_GAP[Y] - THE GAP B/W THE OPTION AT (Y+1) COORDINATE AND THE NEXT OPTION TO IT//
//TOTALGAP - THE TOTAL LINES OCCUPIED BY ALL OPTIONS//
{
char ch = 26;
int y_init = y_opt;
int x_init = x_opt;
gotoxy(x_init, y_opt);
cprintf("%c", ch);
do
{
//gotoxy(2, 2);cout<<x_init<<" "<<y_init<<" "
//cout<<x_opt<<" "<<y_opt<<" "<<pos_gap[y_opt-y_init]; //for testing purposes
char c = getch();
if(c==0)
{
c = getch();
gotoxy(x_init, y_opt);
cout<<' '; //to delete initial bullet
//TO SET Y COORDINATE OF NEW BULLET ACCORDING TO ARROW KEY PRESSED//
switch(c)
{
case 'H' :
if(y_opt==y_init)
{
y_opt = y_init + totalgap - 1;
}
else
{
y_opt = y_opt - pos_gap[y_opt-y_init - 1];
}
break;
case 'P' :
if(y_opt==(y_init + totalgap - 1))
{
y_opt = y_init;
}
else
{
y_opt = y_opt + pos_gap[y_opt-y_init];
}
break;
}
gotoxy(x_init, y_opt); //to print new bullet
cprintf("%c", ch);
}
else if(c==13)
{
return y_opt - y_init + 1; //to return option value according
//to y coordinate of bullet
}
}while(1);
}
void highscore(int s)
{
struct player_score
{
int difficulty;
int score;
char name[9];
}p_s[9];
////INITIALIZING STRUCTURE TO AVOID GARBAGE VALUES////
for(int i = 0; i<9; ++i)
{
strcpy(p_s[i].name, " ");
p_s[i].score = 0;
}
for(i = 0; i<9; ++i)
{
if(i<3)
p_s[i].difficulty = 150;
else if(i>=3 && i<6)
p_s[i].difficulty = 100;
else if(i>=6 && i<9)
p_s[i].difficulty = 50;
}
////HIGHSCORES WILL BE STORED IN BINARY FILE 'HIGHSCOR'////
ifstream finout;
finout.open("HIGHSCOR", ios::in | ios::nocreate | ios::binary);
////TO CREATE 'HIGHSCOR' FILE IF IT IS NOT PRESENT////
if(finout == 0)
{
ofstream ftemp;
ftemp.open("HIGHSCOR", ios::out | ios::binary);
for(int i = 0; i < 9; ++i)
{
ftemp.write((char *) &p_s[i], sizeof(player_score));
}
ftemp.close();
ifstream finout;
finout.open("HIGHSCOR", ios::in | ios::nocreate | ios::binary);
}
for(i = 0; i < 9; ++i)
{
finout.read((char *) &p_s[i] , sizeof(player_score));
}
finout.close();
int line_no, j = 2;
if(s != 0)
{
int j = 2;
while(p_s[j].difficulty != frame_width)
{
j = j + 3;
}
for(int i = j - 2; s < p_s[i].score; ++i){}
if(i>j){ return; }
line_no = i;
while(j > line_no)
{
p_s[j] = p_s[j-1];
--j;
}
p_s[line_no].score = s;
}
////////////////////HIGHSCORES PAGE/////////////////////
clrscr(); frame();
gotoxy((MAX_X - MIN_X + 1)/2 - 5, (MAX_Y - MIN_Y + 1)/2 - 7);
textcolor(YELLOW);
cprintf("HIGHSCORES");
textcolor(WHITE);
gotoxy((MAX_X - MIN_X + 1)/2 - 17, (MAX_Y - MIN_Y + 1)/2 - 5);
cout<<"EASY: ";
for(i = 0; i < 3; ++i)
{
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 - 5 + i);
cout<<i+1<<". ";
for(int j = 0; j<8; ++j)
cout.put(p_s[i].name[j]);
cout<<" "<<p_s[i].score;
}
gotoxy((MAX_X - MIN_X + 1)/2 - 19, (MAX_Y - MIN_Y + 1)/2 - 1);
cout<<"MEDIUM: ";
for(i = 3; i < 6; ++i)
{
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 - 4 + i);
cout<<i+1<<". ";
for(int j = 0; j<8; ++j)
cout.put(p_s[i].name[j]);
cout<<" "<<p_s[i].score;
}
gotoxy((MAX_X - MIN_X + 1)/2 - 17, (MAX_Y - MIN_Y + 1)/2 + 3);
cout<<"HARD: ";
for(i = 6; i < 9; ++i)
{
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 - 3 + i);
cout<<i+1<<". ";
for(int j = 0; j<8; ++j)
cout.put(p_s[i].name[j]);
cout<<" "<<p_s[i].score;
}
if(s == 0)
{
finout.close();
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 7);
cout<<"Press any key to go back to main menu...";
getch();
}
else
{
gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 7);
cout<<"Enter your name...";
_setcursortype(_SOLIDCURSOR);
char name[8];
for(int i = 0; p_s[i].difficulty != frame_width; i = i + 3){}
gotoxy((MAX_X - MIN_X + 1)/2 - 7, line_no + (i / 3) + (MAX_Y - MIN_Y + 1)/2 - 5);
cout<<" ";
gotoxy((MAX_X - MIN_X + 1)/2 - 7, line_no + (i / 3) + (MAX_Y - MIN_Y + 1)/2 - 5);
for(i = 0;; ++i)
{
name[i] = getch();
if(name[i] == 13)
{
name[i] = '\0';
break;
}
else if(name[i] == 8)
{
name[i-1]=' ';
cout<<'\b'<<' '<<'\b';
i = i - 2;
continue;
}
if(i<8)
cout<<name[i];
else if(i == 8)
{
--i;
}
}
strcpy(p_s[line_no].name, " ");
strcpy(p_s[line_no].name, name);
_setcursortype(_NOCURSOR);
ofstream finout;
finout.open("HIGHSCOR", ios::out | ios::binary);
for(i = 0; i < 9; ++i)
{
finout.write((char *) &p_s[i] , sizeof(player_score));
}
finout.close();
}
}
void continuegame(int read, char &c)
//if read == 1, then game status is loaded from file//
//if read == 0, then game status is saved to file//
//else, savedgamestatus is set in the program//
{
struct game_status
{
int x, y, x1, y1, x2, y2, snaklen, frame_width, MAX_X, MIN_X, MAX_Y, MIN_Y;
char pos_key[ARR_X][ARR_Y], c;
char screen[10000]; //stores the complete game screen
}g_s;
if(read == 0)
{
g_s.x = x;
g_s.y = y;
g_s.x1 = x1;
g_s.y1 = y1;
g_s.x2 = x2;
g_s.y2 = y2;
g_s.snaklen = snaklen;
g_s.frame_width = frame_width;
g_s.MAX_X = MAX_X;
g_s.MIN_X = MIN_X;
g_s.MAX_Y = MAX_Y;
g_s.MIN_Y = MIN_Y;
for(int i = 0; i < ARR_X; ++i)
for(int j = 0; j < ARR_Y; ++j)
{ g_s.pos_key[i][j] = pos_key[i][j]; }
g_s.c = c;
gettext(::MIN_X - 1, ::MIN_Y - 1, ::MAX_X + 1, ::MAX_Y + 1, g_s.screen);
savedgamestatus = 1;
ofstream fout;
fout.open("SAVEGAME", ios::out | ios::binary);
fout.write((char *) &g_s, sizeof(game_status));
fout.close();
}
else if (read == 1)
{
ifstream fin;
fin.open("SAVEGAME", ios::in | ios::binary);
fin.read((char *) &g_s, sizeof(game_status));
fin.close();
x = g_s.x;
y = g_s.y;
x1 = g_s.x1;
y1 = g_s.y1;
x2 = g_s.x2;
y2 = g_s.y2;
snaklen = g_s.snaklen;
frame_width = g_s.frame_width;
MAX_X = g_s.MAX_X;
MIN_X = g_s.MIN_X;
MAX_Y = g_s.MAX_Y;
MIN_Y = g_s.MIN_Y;
for(int i = 0; i < ARR_X; ++i)
for(int j = 0; j < ARR_Y; ++j)
{ pos_key[i][j] = g_s.pos_key[i][j]; }
c = g_s.c;
clrscr();
puttext(::MIN_X - 1, ::MIN_Y - 1, ::MAX_X + 1, ::MAX_Y + 1, g_s.screen);
savedgamestatus = 0;
remove("SAVEGAME");
}
else
{
ifstream fin;
fin.open("SAVEGAME", ios::in | ios::binary | ios::nocreate);
if(fin == 0)
{
savedgamestatus = 0;
}
else
{
savedgamestatus = 1;
}
}
}