/*
Author: <Removed>
Created in Spring 2004
This is the classic game of Snake. The object of the game is to eat the food
that appears on the board, which increases the length of your snake by a factor
of one. Your score is the amount of additional lengths you aquire before losing.
You lose by either running into yourself or into a barrier or wall.
I created this game for my end-of-the-year project in Computer Science II for my
senior year of high school. I used Borland C++ 5.02 to create and compile this
program.
Thank you to my Computer Science I and II teacher, <Removed>. She has taught me
me everything I know, supports me 100%, and continues to challenge me.
Also Thanks to Mohammed Ali Akbani for creating the In9.h header file and releasing
it as freeware. This header file was "key" to my program.
*/
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#include <stdio.h>
#include <fstream.h>
#include <windows.h>
#include "dosgraphics.h"
#include "int9.h"
int num;
void main()
{ //open main
int choice=0,player,menu();
void intro(),game(int),instructions();
opengraph(); //activates BGI graphics
intro(); //calls intro function
getch();
cleardevice();
while(choice!=3)
{ //open while
cleardevice();
choice=menu(); //the option selected from the menu
cleardevice();
if(choice==1)
player=1;
if(choice==4)
player=2;
if (choice==1||choice==4)
game(player); //calls the game function
if (choice==2)
instructions(); //calls the instructions function
if (choice==3)
NULL; //closes program
} //close while
closegraph(); //deactivates BGI graphics
} //close main
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~INTRO FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void intro()
{ //open intro function
int speed=0,x=0,y=30,a=0;
bool plus=true;
setbkcolor(DARKGRAY);
while(!kbhit())
{ //open while
cleardevice();
if(a==0)
{ //open if
x++;
y--;
} //close if
if(a==1)
{ //open if
x--;
y++;
} //close if
settextstyle(TRIPLEX_FONT,HORIZ_DIR,14);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(WHITE);
outtextxy(160,(speed+240),"S");
outtextxy(240,(-speed+240),"N");
outtextxy(320,(speed+240),"A");
outtextxy(400,(-speed+240),"K");
outtextxy(480,(speed+240),"E");
if(speed==-10)
plus=true;
if(speed==10)
plus=false;
if(plus)
speed++;
if(!plus)
speed--;
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(320,400,"Press any key to continue.");
setcolor(GREEN);
circle(50,50,x);
circle(590,430,x);
circle(50,430,y);
circle(590,50,y);
circle(50,50,y);
circle(590,430,y);
circle(50,430,x);
circle(590,50,x);
if(x==30)
a=1;
if(x==1)
a=0;
delay(25);
} //close while
settextstyle(TRIPLEX_FONT,HORIZ_DIR,14);
for(num=240;num>50;num=num-2)
{ //open for
outtextxy(160,num,"S");
outtextxy(240,num,"N");
outtextxy(320,num,"A");
outtextxy(400,num,"K");
outtextxy(480,num,"E");
delay(25);
cleardevice();
} //close for
} //close intro function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GAME FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void game(int player)
{ //open game function
int hs,score=0;
char difficulty(),diff,hold[5],*hsname1="",*hsname2="",*name[2]={"",""};
ifstream infile;
void movement(int&,char),newhs(int),movement2(int&,char);
setbkcolor(DARKGRAY);
diff=difficulty(); //gets the difficulty setting
cleardevice();
setcolor(RED);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,50,"Controls");
if(player==1)
{ //open if
outtextxy(320,150,"Up - Up Arrow");
outtextxy(320,170,"Down - Down Arrow");
outtextxy(320,190,"Left - Left Arrow");
outtextxy(320,210,"Right - Right Arrow");
} //close if
if(player==2)
{ //open if
setcolor(YELLOW);
outtextxy(320,150,"Player 1 (YELLOW)");
setcolor(RED);
outtextxy(320,200,"Up - Up Arrow");
outtextxy(320,220,"Down - Down Arrow");
outtextxy(320,240,"Left - Left Arrow");
outtextxy(320,260,"Right - Right Arrow");
setcolor(BLUE);
outtextxy(320,340,"Player 2 (BLUE)");
setcolor(RED);
outtextxy(320,390,"Up - W");
outtextxy(320,410,"Down - S");
outtextxy(320,430,"Left - A");
outtextxy(320,450,"Right - D");
} //close if
getch();
cleardevice();
setcolor(WHITE);
line(54,54,586,54); //boundaries
line(586,54,586,426);
line(586,426,54,426);
line(54,426,54,54);
settextjustify(LEFT_TEXT,CENTER_TEXT);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(400,435,"Press 'Q' to quit");
settextjustify(RIGHT_TEXT,CENTER_TEXT);
outtextxy(240,435,"Press 'P' to pause");
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(100,45,"Score:");
settextjustify(LEFT_TEXT,CENTER_TEXT);
outtextxy(140,45,"0");
if(player==1)
{ //open if
infile.open("hs1.dat");
if(infile.fail()) //if hs.dat doesn't exist, sets the high score to zero
hs=0;
else
infile>>hs>>name[0];
} //close if
if(player==2)
{ //open if
infile.open("hs2.dat");
if(infile.fail()) //if hs2.dat doesn't exist, sets the high score to zero
hs=0;
else
infile>>hs;
} //close if
sprintf(hold,"%d",hs);
outtextxy(350,45,"High Score:");
outtextxy(460,45,hold);
hold=="";
outtextxy(500,45,"-");
settextjustify(CENTER_TEXT, CENTER_TEXT);
setcolor(RED);
outtextxy(320,240,"Press any key to begin.");
getch();
setcolor(DARKGRAY);
outtextxy(320,240,"Press any key to begin.");
settextjustify(LEFT_TEXT,CENTER_TEXT);
if(player==1)
{ //open if
outtextxy(520,45,name[0]);
movement(score,diff); //calls the movement function (1 Player)
} //close if
if(player==2)
{ //open if
infile>>name[1];
outtextxy(520,45,name[1]);
infile>>name[0];
outtextxy(520,25,name[0]);
movement2(score,diff); //calls the movement2 function (2 Player)
} //close if
settextstyle(GOTHIC_FONT,HORIZ_DIR,5);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(RED);
outtextxy(320,260,"GAME OVER");
delay(1000);
getch();
if(score>hs) //if current score is greater than current high score
{ //open if
cleardevice();
newhs(score);
cleardevice();
setbkcolor(BLACK);
ofstream outfile;
if(player==1)
{ //open if
cout <<"First Name or Initials: ";
cin >>hsname1;
while(hsname1=="")
cin >>hsname1;
outfile.open("hs1.dat");
outfile <<score<<" "<<hsname1;
} //close if
if(player==2)
{ //open if
outfile.open("hs2.dat");
outfile <<score<<" ";
cout <<"Player 1's First Name or Initials: ";
cin >>hsname1;
outfile <<hsname1<<" ";
cout <<"Player 2's First Name or Initials: ";
cin >>hsname2;
outfile <<hsname2;
} //close if
} //close if
} //close game function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~DIFFICULTY FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
char difficulty()
{ //open difficulty function
char select=' ';
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(RED);
outtextxy(320,150,"Select your difficulty");
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
outtextxy(320,250,"1) Easy (Normal Speed, Normal Length Increments");
outtextxy(320,300,"2) Normal (Fast Speed, Normal Length Increments");
outtextxy(320,350,"3) Hard (Fast Speed, Fast Length Increments");
while(select!='3'&&select!='2'&&select!='1') //select must be 1, 2, or 3
select=getch();
cleardevice();
if(select=='1')
outtextxy(320,240,"Easy difficulty selected.");
if(select=='2')
outtextxy(320,240,"Normal difficulty selected.");
if(select=='3')
outtextxy(320,240,"Hard difficulty selected.");
delay(1500);
return select;
} //close difficulty function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MOVEMENT FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void movement(int& score,char diff)
{ //open movement function
int x=520,y=240,len=5,cir[199][2],foodx=300,foody=300,speed=100,inc=10;
char dir=KEY_LEFT,olddir,hold[5]="0";
bool hit=false;
randomize();
if(diff=='1')
{ //open if
speed=100;
inc=10;
} //close if
if(diff=='2')
{ //open if
speed=65;
inc=15;
} //close if
if(diff=='3')
{ //open if
speed=65;
inc=20;
} //close if
for(num=0;num<len;num++)
{ //open for
cir[num][0]=x;
cir[num][1]=y;
x=x+10;
} //close for
setcolor(GREEN);
setfillstyle(1,GREEN);
for(num=0;num<len;num++)
{ //open for
circle(cir[num][0],cir[num][1],5);
floodfill(cir[num][0],cir[num][1],GREEN);
} //close for
setcolor(RED);
circle(foodx,foody,3); //draws the initial food
setfillstyle(1,RED);
floodfill(foodx,foody,RED);
while(dir!='q'&&dir!='Q')
{ //open while
while(!kbhit()&&!hit)
{ //open while
if(cir[0][0]==foodx&&cir[0][1]==foody) //if the snake runs over the food
{ //open if
if(diff=='3')
len=len+4; //increases length
else
len=len+2;
setcolor(DARKGRAY);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
hold=="";
score=score+inc; //increases score
setcolor(WHITE);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
foodx=(random(53)+1)*10+50; //generates new food location
foody=(random(37)+1)*10+50;
for(num=0;num<len;num++) //incase the food generates on top of the snake
if(foodx==cir[num][0]&&foody==cir[num][1])
{ //open if
foodx=(random(53)+1)*10+50;
foody=(random(37)+1)*10+50;
num=0;
} //close if
setcolor(RED);
circle(foodx,foody,3); //draws food
setfillstyle(1,RED);
floodfill(foodx,foody,RED);
} //close if
setcolor(DARKGRAY);
setfillstyle(1,DARKGRAY);
circle(cir[(len-1)][0],cir[(len-1)][1],5);
floodfill(cir[(len-1)][0],cir[(len-1)][1],DARKGRAY);
for(num=(len-1);num>0;num--)
{ //open for
cir[num][0]=cir[(num-1)][0];
cir[num][1]=cir[(num-1)][1];
} //close for
if(dir==KEY_UP)
cir[0][1]=cir[0][1]-10;
if(dir==KEY_DOWN)
cir[0][1]=cir[0][1]+10;
if(dir==KEY_LEFT)
cir[0][0]=cir[0][0]-10;
if(dir==KEY_RIGHT)
cir[0][0]=cir[0][0]+10;
setcolor(GREEN);
setfillstyle(1,GREEN);
circle(cir[0][0],cir[0][1],5); //draws the head
floodfill(cir[0][0],cir[0][1],GREEN); //fills the head
setcolor(YELLOW);
setfillstyle(1,YELLOW);
if(dir==KEY_UP||dir==KEY_DOWN)
{ //open if
circle((cir[0][0]+1),(cir[0][1]-1),1); //draws the eye
floodfill((cir[0][0]+1),(cir[0][1]-1),YELLOW); //fills the eye
} //close if
if(dir==KEY_LEFT||dir==KEY_RIGHT)
{ //open if
circle((cir[0][0]-1),(cir[0][1]-1),1);
floodfill((cir[0][0]-1),(cir[0][1]-1),YELLOW);
} //close if
if(cir[0][0]<55||cir[0][0]>585) //If the snake hits horizontal wall
hit=true;
if(cir[0][1]<55||cir[0][1]>425) //If the snake hits vertical wall
hit=true;
if(hit)
{ //open if
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"You ran into the wall!");
} //close if
for(num=1;num<len;num++) //checks to see if the head runs into the body
{ //open for
if(cir[0][0]==cir[num][0]&&cir[0][1]==cir[num][1])
{ //open if
hit=true;
num=len;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"You ran into yourself!");
} //close if
} //close for
delay(speed);
} //close while
if(dir==KEY_LEFT) //holds the previous direction of the snake
olddir='a';
if(dir==KEY_RIGHT)
olddir='d';
if(dir==KEY_UP)
olddir='w';
if(dir==KEY_DOWN)
olddir='s';
if(!hit)
dir=getch(); //holds the new direction of the snake
if(dir=='p') //to pause the game
{ //open if
setcolor(WHITE);
outtextxy(320,240,"Paused");
dir=NULL;
while(dir!='p')
dir=getch();
setcolor(DARKGRAY);
outtextxy(320,240,"Paused");
} //close if
if(olddir=='a'&&dir==KEY_RIGHT) //if the previous direction and the new
dir=olddir; //direction are opposites, set the new
if(olddir=='d'&&dir==KEY_LEFT) //direction equal to the previous direction.
dir=olddir;
if(olddir=='w'&&dir==KEY_DOWN)
dir=olddir;
if(olddir=='s'&&dir==KEY_UP)
dir=olddir;
if(dir=='a') //reinstates the new direction of the snake
dir=KEY_LEFT;
if(dir=='d')
dir=KEY_RIGHT;
if(dir=='w')
dir=KEY_UP;
if(dir=='s')
dir=KEY_DOWN;
if(hit)
dir='q';
} //close while
} //close movement function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MOVEMENT2 FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void movement2(int& score,char diff)
{ //open movement function
int x=520,y=240,len=5,cir[199][2],foodx=300,foody=300,speed=100,inc=10;
int x2=120,y2=240,len2=5,cir2[199][2],foodx2=500,foody2=300;
char dir=KEY_LEFT,olddir,hold[5]="0",dirhold='a';
char dir2=KEY_D,olddir2;
bool hit=false;
randomize();
if(diff=='1')
{ //open if
speed=100;
inc=10;
} //close if
if(diff=='2')
{ //open if
speed=65;
inc=15;
} //close if
if(diff=='3')
{ //open if
speed=65;
inc=20;
} //close if
for(num=0;num<len;num++)
{ //open for
cir[num][0]=x;
cir[num][1]=y;
x=x+10;
} //close for
for(num=0;num<len2;num++)
{ //open for
cir2[num][0]=x2;
cir2[num][1]=y2;
x2=x2-10;
} //close for
setcolor(GREEN);
setfillstyle(1,GREEN);
for(num=0;num<len;num++)
{ //open for
circle(cir[num][0],cir[num][1],5);
floodfill(cir[num][0],cir[num][1],GREEN);
} //close for
for(num=0;num<len2;num++)
{ //open for
circle(cir2[num][0],cir2[num][1],5);
floodfill(cir2[num][0],cir2[num][1],GREEN);
} //close for
setcolor(RED);
circle(foodx,foody,3); //draws the initial food
circle(foodx2,foody2,3);
setfillstyle(1,RED);
floodfill(foodx,foody,RED);
floodfill(foodx2,foody2,RED);
while(dirhold!='q'&&dirhold!='Q')
{ //open while
while(!kbhit()&&!hit)
{ //open while
if(cir[0][0]==foodx&&cir[0][1]==foody) //if the snake runs over the food
{ //open if
if(diff=='3')
len=len+4; //increases length
else
len=len+2;
setcolor(DARKGRAY);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
hold=="";
score=score+inc; //increases score
setcolor(WHITE);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
foodx=(random(53)+1)*10+50; //generates new food location
foody=(random(37)+1)*10+50;
for(num=0;num<len;num++) //incase the food generates on top of the snake
if(foodx==cir[num][0]&&foody==cir[num][1])
{ //open if
foodx=(random(53)+1)*10+50;
foody=(random(37)+1)*10+50;
num=0;
} //close if
for(num=0;num<len2;num++) //incase the food generates on top of the snake2
if(foodx==cir2[num][0]&&foody==cir2[num][1])
{ //open if
foodx=(random(53)+1)*10+50;
foody=(random(37)+1)*10+50;
num=0;
} //close if
setcolor(RED);
circle(foodx,foody,3); //draws food
setfillstyle(1,RED);
floodfill(foodx,foody,RED);
} //close if
if(cir[0][0]==foodx2&&cir[0][1]==foody2) //if the snake runs over the food
{ //open if
if(diff=='3')
len=len+4; //increases length
else
len=len+2;
setcolor(DARKGRAY);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
hold=="";
score=score+inc; //increases score
setcolor(WHITE);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
foodx2=(random(53)+1)*10+50; //generates new food location
foody2=(random(37)+1)*10+50;
for(num=0;num<len;num++) //incase the food generates on top of the snake
if(foodx2==cir[num][0]&&foody2==cir[num][1])
{ //open if
foodx2=(random(53)+1)*10+50;
foody2=(random(37)+1)*10+50;
num=0;
} //close if
for(num=0;num<len2;num++) //incase the food generates on top of the snake
if(foodx2==cir2[num][0]&&foody2==cir2[num][1])
{ //open if
foodx2=(random(53)+1)*10+50;
foody2=(random(37)+1)*10+50;
num=0;
} //close if
setcolor(RED);
circle(foodx2,foody2,3); //draws food
setfillstyle(1,RED);
floodfill(foodx2,foody2,RED);
} //close if
if(cir2[0][0]==foodx&&cir2[0][1]==foody) //if the snake runs over the food
{ //open if
if(diff=='3')
len2=len2+4; //increases length
else
len2=len2+2;
setcolor(DARKGRAY);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
hold=="";
score=score+inc; //increases score
setcolor(WHITE);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
foodx=(random(53)+1)*10+50; //generates new food location
foody=(random(37)+1)*10+50;
for(num=0;num<len2;num++) //incase the food generates on top of the snake
if(foodx==cir2[num][0]&&foody==cir2[num][1])
{ //open if
foodx=(random(53)+1)*10+50;
foody=(random(37)+1)*10+50;
num=0;
} //close if
setcolor(RED);
circle(foodx,foody,3); //draws food
setfillstyle(1,RED);
floodfill(foodx,foody,RED);
} //close if
if(cir2[0][0]==foodx2&&cir2[0][1]==foody2) //if the snake runs over the food
{ //open if
if(diff=='3')
len2=len2+4; //increases length
else
len2=len2+2;
setcolor(DARKGRAY);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
hold=="";
score=score+inc; //increases score
setcolor(WHITE);
sprintf(hold, "%d",score);
outtextxy(140,45,hold);
foodx2=(random(53)+1)*10+50; //generates new food location
foody2=(random(37)+1)*10+50;
for(num=0;num<len2;num++) //incase the food generates on top of the snake
if(foodx2==cir2[num][0]&&foody2==cir2[num][1])
{ //open if
foodx2=(random(53)+1)*10+50;
foody2=(random(37)+1)*10+50;
num=0;
} //close if
setcolor(RED);
circle(foodx2,foody2,3); //draws food
setfillstyle(1,RED);
floodfill(foodx2,foody2,RED);
} //close if
setcolor(DARKGRAY);
setfillstyle(1,DARKGRAY);
circle(cir[(len-1)][0],cir[(len-1)][1],5);
floodfill(cir[(len-1)][0],cir[(len-1)][1],DARKGRAY);
circle(cir2[(len2-1)][0],cir2[(len2-1)][1],5);
floodfill(cir2[(len2-1)][0],cir2[(len2-1)][1],DARKGRAY);
for(num=(len-1);num>0;num--)
{ //open for
cir[num][0]=cir[(num-1)][0];
cir[num][1]=cir[(num-1)][1];
} //close for
for(num=(len2-1);num>0;num--)
{ //open for
cir2[num][0]=cir2[(num-1)][0];
cir2[num][1]=cir2[(num-1)][1];
} //close for
if(dir==KEY_UP)
cir[0][1]=cir[0][1]-10;
if(dir==KEY_DOWN)
cir[0][1]=cir[0][1]+10;
if(dir==KEY_LEFT)
cir[0][0]=cir[0][0]-10;
if(dir==KEY_RIGHT)
cir[0][0]=cir[0][0]+10;
if(dir2==KEY_W)
cir2[0][1]=cir2[0][1]-10;
if(dir2==KEY_S)
cir2[0][1]=cir2[0][1]+10;
if(dir2==KEY_A)
cir2[0][0]=cir2[0][0]-10;
if(dir2==KEY_D)
cir2[0][0]=cir2[0][0]+10;
setcolor(GREEN);
setfillstyle(1,GREEN);
circle(cir[0][0],cir[0][1],5); //draws the head
floodfill(cir[0][0],cir[0][1],GREEN); //fills the head
circle(cir2[0][0],cir2[0][1],5); //draws the head
floodfill(cir2[0][0],cir2[0][1],GREEN); //fills the head
setcolor(YELLOW);
setfillstyle(1,YELLOW);
if(dir==KEY_UP||dir==KEY_DOWN)
{ //open if
circle((cir[0][0]+1),(cir[0][1]-1),1); //draws the dot
floodfill((cir[0][0]+1),(cir[0][1]-1),YELLOW); //fills the dot
} //close if
if(dir==KEY_LEFT||dir==KEY_RIGHT)
{ //open if
circle((cir[0][0]-1),(cir[0][1]-1),1);
floodfill((cir[0][0]-1),(cir[0][1]-1),YELLOW);
} //close if
setcolor(BLUE);
setfillstyle(1,BLUE);
if(dir2==KEY_W||dir2==KEY_S)
{ //open if
circle((cir2[0][0]+1),(cir2[0][1]-1),1); //draws the dot
floodfill((cir2[0][0]+1),(cir2[0][1]-1),BLUE); //fills the dot
} //close if
if(dir2==KEY_A||dir2==KEY_D)
{ //open if
circle((cir2[0][0]-1),(cir2[0][1]-1),1);
floodfill((cir2[0][0]-1),(cir2[0][1]-1),BLUE);
} //close if
if(cir[0][0]<55||cir[0][0]>585) //If the snake hits horizontal wall
hit=true;
if(cir[0][1]<55||cir[0][1]>425) //If the snake hits vertical wall
hit=true;
if(cir2[0][0]<55||cir2[0][0]>585) //If the snake hits horizontal wall
hit=true;
if(cir2[0][1]<55||cir2[0][1]>425) //If the snake hits vertical wall
hit=true;
if(hit)
{ //open if
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
if((cir[0][0]<55||cir[0][0]>585)||(cir[0][1]<55||cir[0][1]>425))
outtextxy(320,220,"Player 1 ran into the wall!");
if((cir2[0][0]<55||cir2[0][0]>585)||(cir2[0][1]<55||cir2[0][1]>425))
outtextxy(320,220,"Player 2 ran into the wall!");
} //close if
if(cir[0][0]==cir2[0][0]&&cir[0][1]==cir2[0][1])
{ //open if
if((dir==KEY_LEFT&&dir2==KEY_D)||(dir==KEY_RIGHT&&dir2==KEY_A))
{ //open if
hit=true;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"Head on collision!");
} //close if
else if((dir==KEY_UP&&dir2==KEY_S)||(dir==KEY_DOWN&&dir2==KEY_W))
{ //open else if
hit=true;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"Head on collision!");
} //close else if
else
{ //open else
hit=true;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"Massive snake accident!");
} //close else
} //close if
if(!hit)
for(num=1;num<len;num++) //checks to see if the head runs into the body
{ //open for
if(cir[0][0]==cir[num][0]&&cir[0][1]==cir[num][1])
{ //open if
hit=true;
num=len;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"Player 1 ran into himself!");
} //close if
if(cir2[0][0]==cir[num][0]&&cir2[0][1]==cir[num][1])
{ //open if
hit=true;
num=len;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"Player 2 ran into Player 1!");
} //close if
} //close for
if(!hit)
for(num=1;num<len2;num++) //checks to see if the head runs into the body
{ //open for
if(cir2[0][0]==cir2[num][0]&&cir2[0][1]==cir2[num][1])
{ //open if
hit=true;
num=len2;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"Player 2 ran into himself!");
} //close if
if(cir[0][0]==cir2[num][0]&&cir[0][1]==cir2[num][1])
{ //open if
hit=true;
num=len2;
setcolor(RED);
settextstyle(COMPLEX_FONT,HORIZ_DIR,3);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,220,"Player 1 ran into Player 2!");
} //close if
} //close for
delay(speed);
} //close while
if(dir2==KEY_A) //holds the previous direction of the snake
olddir2='a';
if(dir2==KEY_D)
olddir2='d';
if(dir2==KEY_W)
olddir2='w';
if(dir2==KEY_S)
olddir2='s';
if(dir==KEY_LEFT) //holds the previous direction of the snake
olddir='a';
if(dir==KEY_RIGHT)
olddir='d';
if(dir==KEY_UP)
olddir='w';
if(dir==KEY_DOWN)
olddir='s';
if(!hit)
dirhold=getch(); //holds the new direction of the snake
if(dirhold=='p') //to pause the game
{ //open if
setcolor(WHITE);
outtextxy(320,240,"Paused");
dirhold=NULL;
while(dirhold!='p')
dirhold=getch();
setcolor(DARKGRAY);
outtextxy(320,240,"Paused");
} //close if
if(dirhold=='a'||dirhold=='s'||dirhold=='d'||dirhold=='w')
{ //open if
if(dirhold=='a')
dir2=KEY_A;
if(dirhold=='d')
dir2=KEY_D;
if(dirhold=='w')
dir2=KEY_W;
if(dirhold=='s')
dir2=KEY_S;
if(olddir2=='a'&&dir2==KEY_D) //if the previous direction and the new
dir2=olddir2; //direction are opposites, set the new
if(olddir2=='d'&&dir2==KEY_A) //direction equal to the previous direction.
dir2=olddir2;
if(olddir2=='w'&&dir2==KEY_S)
dir2=olddir2;
if(olddir2=='s'&&dir2==KEY_W)
dir2=olddir2;
if(dir2=='a') //reinstates the new direction of the snake
dir2=KEY_A;
if(dir2=='d')
dir2=KEY_D;
if(dir2=='w')
dir2=KEY_W;
if(dir2=='s')
dir2=KEY_S;
} //close if
if(dirhold==KEY_UP||dirhold==KEY_DOWN||dirhold==KEY_LEFT||dirhold==KEY_RIGHT)
{ //open if
if(dirhold==KEY_UP)
dir=KEY_UP;
if(dirhold==KEY_RIGHT)
dir=KEY_RIGHT;
if(dirhold==KEY_DOWN)
dir=KEY_DOWN;
if(dirhold==KEY_LEFT)
dir=KEY_LEFT;
if(olddir=='a'&&dir==KEY_RIGHT) //if the previous direction and the new
dir=olddir; //direction are opposites, set the new
if(olddir=='d'&&dir==KEY_LEFT) //direction equal to the previous direction.
dir=olddir;
if(olddir=='w'&&dir==KEY_DOWN)
dir=olddir;
if(olddir=='s'&&dir==KEY_UP)
dir=olddir;
if(dir=='a') //reinstates the new direction of the snake
dir=KEY_LEFT;
if(dir=='d')
dir=KEY_RIGHT;
if(dir=='w')
dir=KEY_UP;
if(dir=='s')
dir=KEY_DOWN;
} //close if
if(hit)
dirhold='q';
} //close while
} //close movement2 function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NEWHS FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void newhs(int score)
{ //open newhs function
int rand1,rand2;
char hold[5];
for(num=0;num<5;num++) //this is incase the user presses enter too fast
{ //open for
rand1=random(16);
setbkcolor(rand1);
settextstyle(COMPLEX_FONT,HORIZ_DIR,6);
rand2=random(16);
while(rand2==rand1)
rand2=random(16);
setcolor(rand2);
outtextxy(320,200,"NEW HIGH SCORE!!!");
settextstyle(SIMPLEX_FONT,HORIZ_DIR,4);
sprintf(hold, "%d",score);
outtextxy(320,300,hold);
settextstyle(SCRIPT_FONT,HORIZ_DIR,2);
outtextxy(320,400,"Press any key to continue.");
delay(500);
} //close for
while(!kbhit())
{ //open while
rand1=random(16);
setbkcolor(rand1);
settextstyle(COMPLEX_FONT,HORIZ_DIR,6);
rand2=random(16);
while(rand2==rand1)
rand2=random(16);
setcolor(rand2);
outtextxy(320,200,"NEW HIGH SCORE!!!");
settextstyle(SIMPLEX_FONT,HORIZ_DIR,4);
sprintf(hold, "%d",score);
outtextxy(320,300,hold);
settextstyle(SCRIPT_FONT,HORIZ_DIR,2);
outtextxy(320,400,"Press any key to continue.");
delay(500);
} //close while
getch();
} //close newhs function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~INSTRUCTIONS FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void instructions()
{ //open instructions function
setcolor(WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,50,"Instructions");
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,CENTER_TEXT);
outtextxy(50,100,"The object of Snake is to eat as much food as you possibly");
outtextxy(50,120,"can without running into the wall or yourself.");
outtextxy(50,160,"Control the movement of the snake by using the arrow keys");
outtextxy(50,180,"located on your keyboard.");
outtextxy(50,220,"You may only move in directions perpendicular to your current direction.");
outtextxy(50,260,"Navigate the snake to the food (the red circle) to gain points.");
outtextxy(50,300,"The game ends when you hit the wall or yourself.");
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,360,"2 Player Mode");
settextjustify(LEFT_TEXT,CENTER_TEXT);
outtextxy(50,400,"Player 1 is yellow and Player 2 is blue.");
outtextxy(50,430,"You must also try to avoid hitting your team mate.");
outtextxy(50,460,"You work together to gain as much points as possible.");
getch();
} //close instructions function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MENU FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int menu()
{ //open menu function
int pos;
char x=0,y=0;
setbkcolor(DARKGRAY);
setcolor(GREEN);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,12);
line(0,150,640,150); //line between title and menu
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(WHITE);
outtextxy(160,50,"S");
outtextxy(240,50,"N");
outtextxy(320,50,"A");
outtextxy(400,50,"K");
outtextxy(480,50,"E"); //title
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,LEFT_TEXT);
setcolor(RED);
outtextxy(100,235,"Play Game");
outtextxy(100,285,"Instructions");
outtextxy(100,335,"Exit");
setcolor(LIGHTGRAY);
rectangle(60,220,200,340);
setcolor(WHITE);
outtextxy(150,450,"Written by Jason Robinson");
setcolor(GREEN);
arc(525,400,180,370,75); //bottom bottom snake
line(450,400,475,300); //middle left snake
arc(450,300,0,90,25); //top bottom snake
ellipse(540,400,180,370,57,50); //top bottom arc
line(597,393,598,388); //close off tail
line(482,400,500,280); //middle right snake
arc(475,280,0,90,25); //top top snake
line(475,255,450,263); //top head snake
ellipse(450,266,90,270,2,3); //upper lip
ellipse(450,272,90,270,2,3); //lower lip
setfillstyle(1,GREEN);
floodfill(470,260,GREEN); //fills the snake green
setcolor(RED);
ellipse(480,266,0,360,4,2); //eye
setfillstyle(1,RED);
floodfill(480,266,RED); //fills the eye red
setcolor(DARKGRAY);
circle(478,266,1); //pupil
line(478,266,478,266); //pupil filler
setcolor(DARKGRAY);
line(449,269,454,269); //line between lips
setcolor(WHITE);
for (num=0;num<6;num++)
circle(70,230,num); //pointer drawn on position 1
pos=1; //position default on 1
while (x!='\r')
{ //open while
while(!kbhit())
{ //open while
if(int ((clock()/CLK_TCK))%3==0)
{ //open if
setcolor(RED);
ellipse(445,269,0,180,3,1); //tongue
ellipse(439,269,180,360,3,1);
ellipse(444,269,0,180,3,1);
ellipse(438,269,180,360,3,1);
line(436,269,434,267); //tongue split
line(436,269,434,271);
line(435,269,433,267);
line(435,269,433,271);
delay(500);
setcolor(DARKGRAY);
ellipse(445,269,0,180,3,1); //tongue
ellipse(439,269,180,360,3,1);
ellipse(444,269,0,180,3,1);
ellipse(438,269,180,360,3,1);
line(436,269,434,267); //tongue split
line(436,269,434,271);
line(435,269,433,267);
line(435,269,433,271);
} //close if
} //close while
x=getch();
if (x== KEY_UP)
{ //open if
if (pos==1)
{ //open nested if
for (num=0;num<6;num++)
{ //open for
setcolor(DARKGRAY);
circle(70,230,num);
setcolor(WHITE);
circle(70,330,num);
} //close for
pos=3;
} //close nested if
else if (pos==2)
{ //open else if
for (num=0;num<6;num++)
{ //open for
setcolor(DARKGRAY);
circle(70,280,num);
setcolor(WHITE);
circle(70,230,num);
} //close for
pos=1;
} //close else if
else if (pos==3)
{ //open else if
for (num=0;num<6;num++)
{ //open for
setcolor(DARKGRAY);
circle(70,330,num);
setcolor(WHITE);
circle(70,280,num);
} //close for
pos=2;
} //close else if
} //close if
if (x== KEY_DOWN)
{ //open if
if (pos==1)
{ //open nested if
for (num=0;num<6;num++)
{ //open for
setcolor(DARKGRAY);
circle (70,230,num);
setcolor(WHITE);
circle (70,280,num);
} //close for
pos=2;
} //close nested if
else if (pos==2)
{ //open else if
for (num=0;num<6;num++)
{ //open for
setcolor(DARKGRAY);
circle (70,280,num);
setcolor(WHITE);
circle (70,330,num);
} //close for
pos=3;
} //close else if
else if (pos==3)
{ //open else if
for (num=0;num<6;num++)
{ //open for
setcolor(DARKGRAY);
circle (70,330,num);
setcolor(WHITE);
circle (70,230,num);
} //close for
pos=1;
} //close else if
} //close nested if
} //close while
if(pos==1)
{ //open if
setcolor(WHITE);
outtextxy(115,255,"1P");
outtextxy(155,255,"2P");
setcolor(RED);
line(100,245,100,255);
line(100,245,110,250);
line(100,255,110,250);
setfillstyle(1,RED);
floodfill(105,250,RED);
while (y!='\r')
{ //open while
y=getch();
if(y==KEY_RIGHT)
{ //open if
if(pos==1)
{ //open if
setcolor(WHITE);
line(100,245,100,255);
line(100,245,110,250);
line(100,255,110,250);
setfillstyle(1,DARKGRAY);
floodfill(105,250,WHITE);
setcolor(DARKGRAY);
line(100,245,100,255);
line(100,245,110,250);
line(100,255,110,250);
setcolor(RED);
line(140,245,140,255);
line(140,245,150,250);
line(140,255,150,250);
setfillstyle(1,RED);
floodfill(145,250,RED);
pos=4;
} //close if
} //close if
if(y==KEY_LEFT)
{ //open if
if(pos==4)
{ //open if
setcolor(WHITE);
line(140,245,140,255);
line(140,245,150,250);
line(140,255,150,250);
setfillstyle(1,DARKGRAY);
floodfill(145,250,WHITE);
setcolor(DARKGRAY);
line(140,245,140,255);
line(140,245,150,250);
line(140,255,150,250);
setcolor(RED);
line(100,245,100,255);
line(100,245,110,250);
line(100,255,110,250);
setfillstyle(1,RED);
floodfill(105,250,RED);
pos=1;
} //close if
} //close if
} //close while
} //close if
return pos; //returns the position the pointer was last left in
} //close menu function