-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNAKEF3.C
1492 lines (1480 loc) · 36.3 KB
/
SNAKEF3.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 <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <process.h>
//NOTE: movesnake() function's prototype might be needed to chnage
void startup(int);//will draw the logo of the project & main screen
void drawline(int , int ,int ,int, int, int);//to draw lines using putpixel
void playgame();//the snake game code
void level1();
void level2();
void contribution();//shows the contribution of the group members
void displaygraphic_1(void);//diplay the graphic prior to level 1
void about();//to display about this project
void highscores();//display high scores from the .txt file
void addhighscore();//add ann entry to the high scores if it is among the top 5
void congrats();//display a graphic if the score is highest of all entries
void settings ();// change settings like speed & color,level,game type
void drawlogo ();//to draw the logo of this game;
void progexit ();//function to exit the program & display a thank you note
void level1_layout();//function to draw the layout of level 1
void level2_layout();
void updatescore(int, int);//function to display the updated score on the screen
// functions required to make the snake mobile in the game
void touchsnake (struct snake *,struct food *);//function to check if the snake hs touched itself
void gameplay (struct snake *,struct food*);//function that actually controls the movement of snake and displays it by using plot function
void gameplay2 (struct snake *,struct food*);
void plot (struct snake *,struct food *);//function to plot the snake and its food at a specified position
void gameover (void);//function to display a graphic when the game is over by any means
void choice (void);//function to display 2 choices after the game is over
//choice also displays the final score after the game is over
//structures required in the gameplay
struct loc
{
int x;
int y;
};
struct snake
{
struct loc snakeloc;
struct snake *next;
char dir;//direction of each node of the snake
};
struct food
{
struct loc foodloc;
int n;//to calculate no of food hits
};
struct score_record
{
long s;
char name[20];
}arr[6],temp;
//global variables
char getkey;//variable to record the keystroke as pressed by the user
long score=0,prevscore=0;//variable to keep the track of the current score
int count=0,bye=0;
int lifelost=0;//flag value to check if the snake has lost a life
int level=1,speed=1,color=1,lives=1;//attributes for the snake
int prevlives;
int putscoretozero=0;//flag variable to check if the score of the player has been reseted befor starting a new game
int records;//to maintain the number of highscores in the game
FILE *fp;//File pointer for storing the highscores in a txt file
void main ()
{
int gd=DETECT,gm;
initgraph (&gd,&gm,"C:\\tc\\bgi");
drawlogo();
cleardevice();
startup(2);
// highscores();
// choice ();
// about();
// gameover ();
// settings();
// level1_layout();
cleardevice ();
// printf ("%d",count);// to check if the keyboard selection is working or not
getch();
fflush (stdin);
closegraph();
}// main
//THE DRAW LOGO FUNCTION
void drawlogo()
{
int x,y,i;
for(i=0;i<500;i++)
{
putpixel(rand()%640,rand()%480,rand()%15);
delay(1);
}
sound(240);
delay(50);
nosound();
for(x=100;x<=550;x++)
{
//setlinestyle(0,SOLID_LINE,4);
putpixel(x,150,15);
delay(3);
}
for(y=150;y<=300;y++)
{putpixel(550,y,14);
delay(3);
}
for(x=550;x>=100;x--)
{putpixel(x,300,13);
delay(3);}
for(y=300;y>=150;y--)
{putpixel(100,y,12);
delay(3);
}
setcolor(RED);
settextstyle(BOLD_FONT,0,5);
outtextxy(115,170,"SNAKE");
setcolor(GREEN);
outtextxy(350,170,"GAME");
delay(3000);
}//END OF DRAW LOGO
void drawline (int x, int y, int x2, int y2, int color,int d)
{
int i;
int w=x2-x;
int h=y2-y;
double m=h/(double)w;
double j=y;
for (i=x;i<=x2;i++)
{
putpixel (i,(int)j,color);
delay (d);
j+=m;
}
}//drawline
void about () //working
{ // the content in about needs to be aligned
a:
cleardevice ();
settextstyle (GOTHIC_FONT,0,5);
setcolor (LIGHTGREEN);
outtextxy (280,20, "ABOUT");
settextstyle (DEFAULT_FONT,0,1);
setcolor (WHITE);
outtextxy (20,80, "This is a basic snake game created using linked list data structure in c.");
outtextxy (20,100, "INSTRUCTIONS: Use the w-a-s-d keys to control the snake in the game and make ");
outtextxy (20,120, " it hit the target. ");
outtextxy (20,140, " The snake starts with 1 life, if you hit the wall or the snake ");
outtextxy (20,160, " touches itself, it looses a life. Once it looses a life, press ");
outtextxy (20,180, " any key to continue with remaining lives (if any). ");
outtextxy (20,200, " As the score increases, the snake's length also increases. ");
outtextxy (20,220, " The speed of the snake however, remains constant throughout.");
outtextxy (20,240, " You can also pause the game by pressing and holding any key ");
outtextxy (20,260, " except the w-a-s-d keys. To resume the game simply release the ");
outtextxy (20,280, " pressed key. ");
outtextxy (20,300, " Customizing options are available under the settings option ");
outtextxy (20,320, " here, you can change snake's speed, game levels, snake's color ");
outtextxy (20,340, " and the number of lives the snake has. ");
setcolor (RED);
outtextxy (20,360, "SCORE CALCULATION:");
setcolor (15);
outtextxy (20,380, " HIGHER THE SPEED, GREATER THE SCORE PER HIT. ");
outtextxy (20,400, " Speed 1: 20 points per hit. ");
outtextxy (20,420, " Speed 2: 40 points per hit. ");
outtextxy (20,440, " Speed 3: 60 points per hit. ");
//enjoy
settextstyle (SANS_SERIF_FONT,0,2);
setcolor (YELLOW);
outtextxy (30,400,"ENJOY..!!");
settextstyle (DEFAULT_FONT,0,2);
setcolor (LIGHTRED);
outtextxy (480,400, "Snake v1.0");
rectangle (550,430,630,460);
settextstyle (DEFAULT_FONT,0,2);
setcolor (LIGHTGRAY);
outtextxy (560,440, "BACK");
outtextxy (520,440,">>");
if (getch()==13)
{
sound (400);
delay (50);
nosound ();
cleardevice();
return;
}
else
goto a;
}//about
void displaygraphic_1 ()
{
char x;
if (level==1)
{
cleardevice();
setbkcolor(WHITE);
settextstyle (DEFAULT_FONT,0,4);
setcolor (BLUE);
rectangle (100,100,540,380);
outtextxy (220,120,"LEVEL 1 ");
setcolor (RED);
outtextxy (230,200,"READY ?? ");
setcolor (BROWN);
settextstyle (TRIPLEX_FONT,0,1);
outtextxy (500,440,"press enter ");
while (getch()!=13)
{
}
sound (400);
delay (50);
nosound ();
setcolor (WHITE);
outtextxy (500,440,"press enter ");
delay (1000);
setcolor (LIGHTGREEN);
settextstyle (DEFAULT_FONT,0,4);
outtextxy (230,280, "BEGIN !! ");
delay (1000);
// getch();
// progexit();
cleardevice();
}//if level==1
else if(level==2)
{
cleardevice();
setbkcolor(WHITE);
settextstyle (DEFAULT_FONT,0,4);
setcolor (BLUE);
rectangle (100,100,540,380);
outtextxy (220,120,"LEVEL 2");
setcolor (RED);
outtextxy (230,200,"READY ?? ");
setcolor (BROWN);
settextstyle (TRIPLEX_FONT,0,1);
outtextxy (500,440,"press enter ");
while (getch()!=13)
{
}
sound (400);
delay (50);
nosound ();
setcolor (WHITE);
outtextxy (500,440,"press enter ");
delay (1000);
setcolor (LIGHTGREEN);
settextstyle (DEFAULT_FONT,0,4);
outtextxy (230,280, "BEGIN !! ");
delay (1000);
// getch();
// progexit();
cleardevice();
}
return;
}
void playgame () ///working
{
displaygraphic_1();
cleardevice ();
if (level==1)
level1_layout();
else
{
level2_layout();
}
return;
}//playgame
void highscores ()//will display all the highscores in hscore.txt
{ //working as it should
int i,d=0;
long score;
char name[10],buffer_score[100],buffer_name[10];
cleardevice ();
//pixelated bckground
fp=fopen ("hscore.txt","r");
setviewport (0,0,640,480,0);
for (i=0;i<1000;i++)
{
putpixel (rand()%640,rand()%480,rand()%15);
}
settextstyle (EUROPEAN_FONT,0,4);
setcolor (LIGHTGREEN);
outtextxy (200,50,"HIGHSCORES");
setcolor (WHITE);
line (195,50,500,50);
line (195,120,500,120);
settextstyle (1,0,3);
setcolor (LIGHTGRAY);
outtextxy (200,170,"NAME");
outtextxy (450,170,"SCORE");
//code to read and display the highscores from the file
settextstyle (DEFAULT_FONT,0,1);
setcolor (WHITE);
rewind (fp);
while (fscanf (fp,"%s %ld",name,&score)!=EOF)
{
sprintf (buffer_score,"%ld",score);
sprintf (buffer_name,"%s",name);
if (score>0)
{
outtextxy (200,210+d,buffer_name);
outtextxy (450,210+d,buffer_score);
d+=40;
}
//i+=1;
}
fclose (fp);
getch();
clearviewport();
return;
}//highscores
void settings ()
{
int arrow_y=100,co=0;
char x;
cleardevice();
setbkcolor (WHITE);
// printf ("\n in settings ");
// getch ();
q:
settextstyle (SANS_SERIF_FONT,0,4);
setcolor (LIGHTGRAY);
outtextxy (280,20,"SETTINGS ");
setlinestyle(3,1,1);
line (40,60,600,60);
line (40,75,600,75);
settextstyle (TRIPLEX_FONT,0,3);
setcolor (DARKGRAY);
if (level==1)
outtextxy (50,100," Level :<< 1 >>");
else if (level==2)
outtextxy (50,100," Level :<< 2 >>");
else if (level==3)
outtextxy (50,100," Level :<< 3 >>");
if (speed==1)
outtextxy (50,140," Speed :<< 1 >> ");
else if (speed==2)
outtextxy (50,140," Speed :<< 2 >> ");
else if (speed==3)
outtextxy (50,140," Speed :<< 3 >> ");
if (color==1)
{
setcolor (BLUE);
outtextxy (50,180," Color :<< BLUE >> ");
}
else if (color==2)
{
setcolor (GREEN);
outtextxy (50,180," Color :<< GREEN >> ");
}
else if (color==3)
{
setcolor (CYAN);
outtextxy (50,180," Color :<< CYAN >> ");
}
else if (color==4)
{
setcolor (RED);
outtextxy (50,180," Color :<< RED >> ");
}
setcolor (DARKGRAY);
if (lives==1)
outtextxy (50,220," Lives :<< 1 >> ");
else if (lives==2)
outtextxy (50,220," Lives :<< 2 >> ");
else if (lives==3)
outtextxy (50,220," Lives :<< 3 >> ");
setlinestyle (2,1,1);
rectangle (40,255,120,290);
outtextxy (50,260,"BACK" );
// setcolor (RED); linestyle
outtextxy (10,arrow_y,">>");
while (arrow_y<=260)
{
x=getch();
if (arrow_y==260&&x==13)
break;
if (x==80 && arrow_y<260) //arrow up down selection process
{
sound (400);
delay (50);
nosound ();
cleardevice ();
arrow_y+=40;
co++;
goto q;
}
else if (x==72 && arrow_y>100)
{
sound (400);
delay (50);
nosound ();
cleardevice ();
arrow_y-=40;
co--;
goto q;
}
else if (x==75 && arrow_y<260)//left
{
sound (400);
delay (50);
nosound ();
switch (arrow_y) //decrement value of attribute at arrow_y
{
case 100:
if (level>1)
{
level--;
cleardevice ();
goto q;
}
break;
case 140:
if (speed>1)
{
speed--;
cleardevice ();
goto q;
}
break;
case 180:
if (color>1)
{
color--;
cleardevice ();
goto q;
}
break;
case 220:
if (lives>1)
{
lives--;
cleardevice();
goto q;
}
break;
}//switch case
}//else if for x==75
else if (x==77 && arrow_y<260)
{
sound (400);
delay (50);
nosound ();
switch (arrow_y) //increment value of attribute at arrow_y
{
case 100:
if (level<3)
{
level++;
cleardevice ();
goto q;
}
break;
case 140:
if (speed<3)
{
speed++;
cleardevice ();
goto q;
}
break;
case 180:
if (color<4)
{
color++;
cleardevice ();
goto q;
}
break;
case 220:
if (lives<3)
{
lives++;
cleardevice();
goto q;
}
break;
}//switch case
}//else if for x=77
}//if (kbhit())
cleardevice();
setlinestyle (SOLID_LINE,0,1);
startup (0);
}//settings
void contribution()
{
cleardevice();
settextstyle(GOTHIC_FONT,0,6);
outtextxy(220,10,"CREDITS");
settextstyle(EUROPEAN_FONT,0,1);
setcolor(11);
outtextxy(10,190,"Made by PSX95");
getch();
return;
}
void progexit ()
{
contribution();
// delay(17000);
cleardevice ();
bye=1;// flag variable
closegraph();
exit(1);
}//progexit
void startup(int x) // function working as it should
{
int i,j,d=20;
char b;
count=1;
// setlinestyle (0,1,3); //linestyle
setviewport (0,0,640,480,1);
setbkcolor (BLACK);
q:
//pixelated background
for (i=0;i<1000;i++)
{
putpixel (rand()%640,rand()%480,rand()%15);
}
for (i=150;i>=50;i--)
{
putpixel (i,50,RED);
delay(x);
}
for (i=50;i<=120;i++)
{
putpixel (50,i,WHITE);
delay(x);
}
for (i=50;i<=150;i++)
{
putpixel (i,120,RED);
delay(x);
}
for (i=120;i<=190;i++)
{
putpixel (150,i,WHITE);
delay(x);
}
for (i=150;i>=50;i--)
{
putpixel (i,190,RED);
delay(x);
}
// S
for (i=190;i>=50;i--)
{
putpixel (180,i,YELLOW);
delay(x);
}
setcolor (RED);
drawline (180,50,280,190,3,x);//try using putpixel instead
for (i=190;i>=50;i--)
{
putpixel (280,i,GREEN);
delay(x);
}
//N
drawline (310,190,360,50,15,x);
drawline (360,50,410,190,4,x);
drawline (330,140,390,140,14,x);
//A
// drawline (430,190,430,50,15,x); (not working)
for (i=190;i>=50;i--)
{
putpixel (440,i,14);
delay (x);
}
drawline (440,120,510,190,15,x);
drawline (440,120,510,50,4,x);
//K
for (i=600;i>=540;i--)
{
putpixel (i,190,1);
delay(x);
}
for (i=190;i>=50;i--)
{
putpixel (540,i,5);
delay (x);
}
for (i=540;i<=580;i++)
{
putpixel (i,120,6);
delay (x);
}
for (i=540;i<=600;i++)
{
putpixel (i,50,15);
delay (x);
}
//E
// Writing snake on the logo screen complete
// Displaying the options
setcolor (LIGHTGRAY);
rectangle (getmaxx()/2-100,getmaxy()/2+10,getmaxx()/2+100,getmaxy()/2+40);
settextstyle (DEFAULT_FONT,0,2);
setcolor (GREEN);
outtextxy (getmaxx()/2-90,getmaxy()/2+20,"PLAY GAME !!");
rectangle (getmaxx()/2-100,getmaxy()/2+60,getmaxx()/2+100,getmaxy()/2+90);
setcolor (YELLOW);
outtextxy (getmaxx()/2-90,getmaxy()/2+70,"HIGH SCORES");
rectangle (getmaxx()/2-100,getmaxy()/2+110,getmaxx()/2+100,getmaxy()/2+140);
setcolor (LIGHTMAGENTA);
outtextxy (getmaxx()/2-90,getmaxy()/2+120," SETTINGS ");
rectangle (getmaxx()/2-100,getmaxy()/2+160,getmaxx()/2+100,getmaxy()/2+190);
setcolor (RED);
outtextxy (getmaxx()/2-90,getmaxy()/2+170," ABOUT");
rectangle (getmaxx()/2-100,getmaxy()/2+210,getmaxx()/2+100,getmaxy()/2+240);
setcolor (LIGHTGRAY);
outtextxy (getmaxx()/2-90,getmaxy()/2+220," EXIT");
setcolor (LIGHTRED);
outtextxy (getmaxx()/2-129,getmaxy()/2+d, ">> <<");
while (b!=13) //arrow moving and selection process through keyboard
{
b=getch();
if (b==80)
{
sound (400);
delay (50);
nosound();
while (d>=20&&d<210) //displaying arrow mve on pressing down arrow
{
cleardevice();
x=0;
d+=50;
count++;
goto q;
}
}
else
{
if (b==72) //displaying arrrow move on pressing up arrow
{
sound (400);
delay(50);
nosound();
while (d>20&&d<=220)
{
cleardevice();
x=0;
d-=50;
count--;
goto q;
}///while
}//if
}//else
}//while
b='\0';
// Starting to call other functions from this function
// Control has to be returned to this function after exiting from the other functions
switch (count)
{
case 1:
playgame();
break;
case 2:
highscores();
break;
case 3:
settings();
break;
case 4:
about();
break;
case 5:
progexit ();
break;
}//switch case
if (bye==1)
return;
else
goto q;
}//drawlogo_program
//plot- function to plot the snake at a specified x-y co ordinate
//function will also plot the food location at a specified x-y co-ordinate
//NOTE: setviewport coordinates needs to be changed
void plot (struct snake *head, struct food *f)
{
struct snake *temp; // will draw the target and
temp=head; //the snake at the specified
setfillstyle (9,15); //x-y co ordinates
// settextstyle ("TRIPLEX_FONT",0,2);
setviewport (35,80,610,438,1);
// bar (temp->snakeloc.x-5,temp->snakeloc.y-5,temp->snakeloc.x+5,temp->snakeloc.y+5);
setcolor (5);
if(level==2)
{
setfillstyle(9,12);
bar (150,165,420,185);
setfillstyle(9,14);
bar(50,40,70,320);
setfillstyle(9,13);
bar(490,40,510,320);
}
circle (temp->snakeloc.x,temp->snakeloc.y,5);
temp=temp->next;//updating pointer to be started from
setfillstyle (9,3);
setcolor (color);
while (temp!=NULL)
{
// bar (temp->snakeloc.x-4,temp->snakeloc.y-4,temp->snakeloc.x+4,temp->snakeloc.y+4);
circle (temp->snakeloc.x,temp->snakeloc.y,4);
temp=temp->next;
}//while
//now to draw the food
setcolor (14);
circle (f->foodloc.x,f->foodloc.y,3);
circle (f->foodloc.x,f->foodloc.y,2);
circle (f->foodloc.x,f->foodloc.y,1);
if (speed==1)
delay (40);//controlling speed of the snake (working)
else if (speed==2)
delay (30);
else if (speed==3)
delay (20);
while (!kbhit())//untill any key on the keyboard is pressed
{
goto q;
}
getkey=getch();
// touchsnake (head);
q:
clearviewport();
}
//touchsnake- function to check if the snake has touch its own body
void touchsnake (struct snake *head,struct food *f)//touchsnake
{
struct snake *temp,*temp1;
temp=head->next;
//head should be preserved in a seperate variable
//logic applied: chek if the head's x-y co-ordinates matches any
//node's x-y coordinte
while (temp!=NULL)
{
if (temp->dir!=head->dir) //snake's head cannot hit the nodes which
{ //have the same direction as the snake's head
if (head->dir=='a')
{
if (temp->dir!='d')
{
if (head->snakeloc.x==temp->snakeloc.x&&head->snakeloc.y==temp->snakeloc.y)
{
if (lives==0)
{
clearviewport();
gameover();
}
else
{
prevlives=lives;
--lives;
lifelost=1;
//to display the update life on the screen
updatescore(score,prevscore);
// setting the snake's position back to intial
head->snakeloc.x=250;
head->snakeloc.y=250;
plot (head,f);
getch();
}
// return 1;
} //if 3
}//if 2
}//if 1
else
if (head->dir=='d')
{
if (temp->dir!='a')
{
if (head->snakeloc.x==temp->snakeloc.x&&head->snakeloc.y==temp->snakeloc.y)
{
if (lives==0)
{
clearviewport();
gameover();
}
else
{
prevlives=lives;
--lives;
lifelost=1;
//to display the update life on the screen
updatescore(score,prevscore);
// setting the snake's position back to intial
head->snakeloc.x=250;
head->snakeloc.y=250;
plot (head,f);
getch();
}
// return 1;
} //if 2
}//if 1
}// if 'd'
else
if (head->dir=='w')
{
if (temp->dir!='s')
{
if (head->snakeloc.y==temp->snakeloc.y&&head->snakeloc.x==temp->snakeloc.x)
{
if (lives==0)
{
clearviewport();
gameover ();
}
else
{
prevlives=lives;
--lives;
lifelost=1;
//to display the update life on the screen
updatescore(score,prevscore);
// setting the snake's position back to intial
head->snakeloc.x=250;
head->snakeloc.y=250;
plot (head,f);
getch();
}
} //if 1
}//if 2
}//if 'w'
else
if (head->dir=='s')
{
if (temp->dir!='w')
{
if (head->snakeloc.y==temp->snakeloc.y&&head->snakeloc.x==temp->snakeloc.x)
{
if (lives==0)
{
clearviewport();
gameover();
}
else
{
prevlives=lives;
--lives;
lifelost=1;
//to display the update life on the screen
updatescore(score,prevscore);
// setting the snake's position back to intial
head->snakeloc.x=250;
head->snakeloc.y=250;
plot (head,f);
getch();
}
} //if 1
} //if 2
}// if 's'
}//if temp!=head->dir
temp=temp->next;
}//while
return ;
}
void gameplay (struct snake *head, struct food *f)
{
int fx,fy;
char buffer[10];
struct snake *temp,pre,nxt;
temp=head;//so that head remains unchanged
// to check if the snake touches the boundry
while (1)
{
if (head->snakeloc.x<=5||head->snakeloc.y<=0||head->snakeloc.x>=570||head->snakeloc.y>=355)
{//this needs to be fixed
if (lives==0)
{
clearviewport();
gameover();//gameover condition
}
else
{
//updating value of lives
prevlives=lives;
--lives;
lifelost=1;
//to display the update life on the screen
updatescore(score,prevscore);
// setting the snake's position back to intial
head->snakeloc.x=250;
head->snakeloc.y=250;
plot (head,f);
getch();
}
}
// touchsnake (head);
//now to check if the snake's "head" actually hits the target (the food)
if(head->snakeloc.x>=f->foodloc.x-5&&head->snakeloc.x<=f->foodloc.x+5&&head->snakeloc.y>=f->foodloc.y-5&&head->snakeloc.y<=f->foodloc.y+5)
{
// change the food loaction and update the score
sound (420);
delay (20);
nosound();
temp=head;
prevscore=score;
score=(score+20*speed);
updatescore (score,prevscore);
fx=random(450);
while (fx<=50)
fx=random(470);
f->foodloc.x=fx;
fy=random(300);
while (fy<=90)
fy=random(320);
f->foodloc.y=fy;
f->n+=1;
// since the snake has hit the target, so now snake's length needs to be updated
// to update length
while (temp->next!=NULL)
{
temp=temp->next;
}//to reach the end of linked list
//adding a new node for increasing snake's length
temp->next=(struct snake *)malloc (sizeof (struct snake));
temp->next->next=NULL;
temp->next->snakeloc.x=temp->snakeloc.x;
temp->next->snakeloc.y=temp->snakeloc.y;
temp->next->dir=temp->dir;
}
//now to check if the diretion of the snake has been changed or not
switch (getkey)
{
case 'a': if (head->dir!='d')
{
head->dir='a';
head->snakeloc.x-=3;
}
else
{
getkey=head->dir;
}
break;
case 'd': if (head->dir!='a')
{
head->dir='d';
head->snakeloc.x+=3;
}
else
{
getkey=head->dir;
}
break;
case 'w':
if (head->dir!='s')
{
head->dir='w';
head->snakeloc.y-=3;
}
else
{
getkey=head->dir;
}
break;
case 's':
if (head->dir!='w')
{
head->dir='s';
head->snakeloc.y+=3;
}
else
{
getkey=head->dir;
}
break;
default:
getkey=head->dir;
break;
}//switch case
// updatescore (score,prevscore);
plot (head,f);//to plot the current location of snake and the food
//now to swap previous and the next nodes of the linked list
// to make it appear as a moving snake
temp=head;
pre=*temp;
while (temp->next!=NULL) //swapping adjecent nodes
{
nxt.snakeloc.x=temp->next->snakeloc.x; // assigning the temp->link node to nxt
nxt.snakeloc.y=temp->next->snakeloc.y; //nxt=temp->link
nxt.dir=temp->next->dir;
temp->next->snakeloc.x=pre.snakeloc.x;//temp->link=pre
temp->next->snakeloc.y=pre.snakeloc.y;
temp->next->dir=pre.dir;
temp=temp->next;
pre=nxt;
}
//updated node (in case of direction change will now be added to the end
//of the linked list to make it look like a moving snake
touchsnake (head,f);
}//while
}//gameplay
//level 1 of the game
//code has been integrated from trysnake.c
//the function below will control all the gameplay of level 2
//seperate function has been made to avoid confusion
void gameplay2 (struct snake *head, struct food *f)
{