#define PAUSED 20
#define RESUME 21
#define RIGHT 1
#define UP 11
#define LEFT 111
#define DOWN 1111
#define MAXSIZE 99
#define INITIALSPEED 50
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
int snakex[MAXSIZE],snakey[MAXSIZE];
int specialFoodX,specialFoodY,specialScore=0;
int spflag,spFood,dirPaused;
int speedTimer=INITIALSPEED,game,themeColor=6;
int highScore=0,currentScore,firstTime=1,setFlag=0,exitFlag,soundFlag=1,musicFlag=1;
int restartFlag=0,lowSpeed=0,snakeColor=3;
char name[10];
void instructions();
int getDir(char key,int dir);
void motion(int dir,int length);
void check();
int getFoodX();
int getFoodY();
void specialFood(int flag, int numFood);
void showFood(int x,int y);
void drawSnake(int length, int color);
void defaultSnake();
void gameOver(int snakeLength);
void specialFoodConsumed();
int angel(int dir,int snakeLength);
void drawBoard();
void settings();
void dead(int snakeLength);
void loading();
void scoreBoard();
void eatSound();
void music();
void main(){
int gd = DETECT, gm;
int foodConsumed;
int dir=RIGHT;
int snakeLength;
char keyPressed;
int food[2];
foodConsumed=1;
spflag=0;
spFood=0;
specialScore=0;
currentScore=0;
game=RESUME;
speedTimer=INITIALSPEED;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
if(firstTime==1){
loading();
instructions();
}
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
firstTime=0;
snakeLength=3;
defaultSnake();
setcolor(themeColor);
rectangle(40,40,620,390);
setviewport(41,41,619,389,1);
drawBoard();
drawSnake(snakeLength,CYAN);
scoreBoard();
if(musicFlag==1)
music();
start: ;
if(foodConsumed){
food[0]=getFoodX();
food[1]=getFoodY();
}
drawBoard();
while( !kbhit() && snakex[0]<580 && snakex[0]>0 && snakey[0]>0 && snakey[0]<300 ){
drawBoard();
foodConsumed=0;
currentScore=snakeLength-3+specialScore;
if((snakex[0]>=specialFoodX-1 && snakex[0]<=specialFoodX+10 && snakey[0]>=specialFoodY-1 && snakey[0]<=specialFoodY+10) || ( snakex[0]+5>=specialFoodX-1 && snakex[0]+5<=specialFoodX+5 && snakey[0]+5>=specialFoodY-1 && snakey[0]+5<=specialFoodY+5 )){
specialFoodX=0;
specialFoodY=0;
specialFoodConsumed();
}
if((snakex[0]>=food[0]-1 && snakex[0]<=food[0]+5 && snakey[0]>=food[1]-1 && snakey[0]<=food[1]+5) || ( snakex[0]+5>=food[0]-1 && snakex[0]+5<=food[0]+5 && snakey[0]+5>=food[1]-1 && snakey[0]+5<=food[1]+5 ))
{
if(soundFlag==1)
eatSound();
snakeLength++;
if((snakeLength-3) %5==0){
spflag=1;
spFood=1;
speedTimer=20;
}
food[0]=getFoodX();
food[1]=getFoodY();
}
showFood(food[0],food[1]);
specialFood(spflag,snakeLength-3);
drawSnake(snakeLength,snakeColor);
dir= getDir(keyPressed,dir);
if(dir==PAUSED){
dir=angel(dirPaused,snakeLength);
keyPressed=1;
}
if(setFlag==1){
settings();
setFlag=0;
keyPressed=1;
}
if(exitFlag==0){
exitFlag=1;
keyPressed=1;
}
if(restartFlag==1){
restartFlag=0;
keyPressed=1;
main();
}
motion(dir,snakeLength);
scoreBoard();
delay(speedTimer);
clearviewport();
}
if(snakex[0]+5>=580 || snakex[0]<=0 || snakey[0]<=0 || snakey[0]+5>=300)
{
gameOver(snakeLength);
}
dead(snakeLength);
keyPressed=getch();
goto start;
}
void instructions(){
char start;
printf("-----------------ShadowHack's Snake Game----------------\n");
printf("\nInstructions:\n");
printf("1. Use left,up,right and down arrow keys to move the snake left, top, right, down respectively.\n");
printf("2. Press 'e' anytime during the game to exit the game.\n");
printf("3. Prevent snake from hiting the wall.\n");
printf("4. Collect as many foods as possible to increase the score.\n");
printf("5. Special food appears during the game which increases the speed of snake.\n");
printf("6. To bring snake to normal speed consume special food before normal food.\n");
printf("7. If special food is not consumed before normal food, it disappears.\n");
printf("8. Special food increases the score thrice as much as the normal food.\n");
printf("9. Theme change applies on outer boundary after next restart of game.\n");
printf("\nPlease Enter your name: ");
scanf("%s",name);
printf("Press 's' to start the game or 'e' to exit....");
while(1){
start=getch();
if(start=='s')
break;
else if(start=='e')
exit(0);
else{
printf("\nPress 's' key to start the game..\n");
continue;
}
}
}
int getDir(char key,int dir){
char exits;
if(key==75 && dir!=RIGHT)
dir=LEFT;
else if(key==72 && dir!=DOWN )
dir=UP;
else if(key==80 && dir!=UP)
dir=DOWN;
else if(key==77 && dir!=LEFT)
dir=RIGHT;
else if(key=='e'){
while(1){
setfillstyle(SOLID_FILL,BLACK);
bar(150,100,350,200);
setcolor(themeColor);
rectangle(150,100,350,180);
outtextxy(160,110,"Are you sure to exit ? ");
outtextxy(175,150,"Yes('y')");
outtextxy(265,150,"No('n')");
exits=getch();
if(exits=='y')
exit(0);
else if(exits=='n'){
exitFlag=0;
break;
}
else
continue;
}
}
else if(key=='p'){
// game=PAUSED;
dirPaused=dir;
dir=PAUSED;
}
else if(key=='s')
setFlag=1;
return dir;
}
void motion(int dir,int length){
int i;
if(dir==RIGHT){
for(i=length-1;i>0;i--){
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
snakex[0]+=7; // increased by 7 to keep a 2 pixel distance between each part
}
if(dir==UP){
for(i=length-1;i>0;i--){
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
snakey[0]-=7;
}
if(dir==LEFT){
for(i=length-1;i>0;i--){
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
snakex[0]-=7;
}
if(dir==DOWN){
for(i=length-1;i>0;i--){
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
snakey[0]+=7;
}
}
void drawSnake(int length,int color){
int i;
setfillstyle(11,color);
for(i=0;i<length;i++){
if(i)
setfillstyle(SOLID_FILL,color);
else
setfillstyle(11,color);
bar(snakex[i],snakey[i],snakex[i]+5,snakey[i]+5);
}
}
void check(){
delay(200);
clrscr();
rectangle(40,40,620,340);
printf("%d %d",snakex,snakey);
}
int getFoodX(){
int foodX;
foodX= rand()%570;
return foodX;
}
int getFoodY(){
int foodY;
foodY=rand()%290;
return foodY;
}
void specialFood(int flag,int numFood){
if(flag){
specialFoodX=rand()%570;
specialFoodY=rand()%290;
}
if(numFood%5==0&&spFood==1) {
flag=0;
setfillstyle(SOLID_FILL,GREEN);
bar(specialFoodX,specialFoodY,specialFoodX+10,specialFoodY+10);
spflag=0;
}
}
void showFood(int x,int y){
setfillstyle(SOLID_FILL,RED);
bar(x,y,x+5,y+5);
}
void defaultSnake(){
snakex[0]=340;
snakey[0]=240;
snakex[1]=333;
snakey[1]=240;
snakex[2]=326;
snakey[2]=240;
}
void gameOver(int snakeLength){
char score[4],hi_score[4];
char ex;
setcolor(RED);
outtextxy(260,150,"GAME OVER !!");
setcolor(GREEN);
score[0]=48+(currentScore)/100;
score[1]=48+(currentScore)/10;
score[2]=48+(currentScore)%10;
score[3]='\0';
hi_score[0]=48+(highScore)/100;
hi_score[1]=48+(highScore)/10;
hi_score[2]=48+(highScore)%10;
hi_score[3]='\0';
outtextxy(250,170,"Your Score: ");
outtextxy(340,170,score);
if(highScore>currentScore){
outtextxy(250,190,"High Score: ");
outtextxy(340,190,hi_score);
moveto(200,210);
outtext(name);
outtext(", you can play better !!");
}
else if(highScore!=0){
outtextxy(250,190,"New High Score: ");
moveto(250,210);
outtext("Well done,");
outtext(name);
outtext("!");
}
setfillstyle(SOLID_FILL,RED);
drawSnake(snakeLength,RED);
printf("Press Esc to exit...\n");
printf("Press 'r' to restart the game..");
while(1){
drawBoard();
ex=getch();
if(ex==27)
exit(0);
else if(ex=='r'){
cleardevice();
closegraph();
main();
}
else
continue;
}
}
void specialFoodConsumed(){
if(soundFlag==1)
eatSound();
setfillstyle(SOLID_FILL,BLACK);
bar(specialFoodX,specialFoodY,specialFoodX+10,specialFoodY+10);
if(lowSpeed==1)
speedTimer=100;
else
speedTimer=50;
spFood=0;
specialScore+=2;
}
int angel(int dir,int snakeLength){
setcolor(themeColor);
scoreBoard(snakeLength);
setcolor(themeColor);
outtextxy(150,100,"Game Paused! Press 'p' to resume");
while(!('p'==getch()));
return dir;
}
void scoreBoard(){
char score[4],hi_score[4];
score[0]=48+(currentScore)/100;
score[1]=48+(currentScore)/10;
score[2]=48+(currentScore)%10;
score[3]='\0';
if(currentScore>highScore)
highScore=currentScore;
hi_score[0]=48+(highScore)/100;
hi_score[1]=48+(highScore)/10;
hi_score[2]=48+(highScore)%10;
hi_score[3]='\0';
setcolor(themeColor);
outtextxy(3,331,"HighScore: ");
outtextxy(3,311,"Score:");
outtextxy(150,311,"Pause: 'p' ");
outtextxy(150,331,"Exit: 'e'");
outtextxy(250,311,"Settings: 's'");
rectangle(280,331,580,353);
moveto(370,311);
setcolor(YELLOW);
outtext("Welcome, ");
outtext(name);
outtext("!");
setcolor(themeColor);
moveto(312,335);
outtext("www.shadowhackit.blogspot.com");
outtextxy(51,311,score);
outtextxy(87,331,hi_score);
}
void drawBoard(){
setcolor(themeColor);
line(0,302,578,302);
}
void loading(){
int i;
int dot;
///settextstyle(1,0,3);
outtextxy(10,160,"Loading");
setcolor(LIGHTRED);
rectangle(10,180,610,200);
setfillstyle(SOLID_FILL,LIGHTRED);
for(i=0;i<600;i++) {
bar(10,180,10+i,200);
delay(6);
if(i%50==0){
outtextxy(90+dot,160,". ");
dot+=10;
}
}
cleardevice();
closegraph();
}
void dead(int snakeLength){
int i,j;
for(i=1;i<snakeLength;i++)
for(j=1;j<snakeLength;j++){
if((snakex[0]==snakex[i]||snakey[0]==snakey[i]+5 || snakey[0]==snakey[i]-5) && (snakey[0]==snakey[i] || snakey[0]==snakey[i]+5 ||snakey[0]==snakey[i]-5))
gameOver(snakeLength);
}
}
void settings(){
char option,exits;
int startx=200,starty=40,endx=400,endy=330;
while(1){
clearviewport();
setfillstyle(SOLID_FILL,BLACK);
setcolor(themeColor);
rectangle(startx,starty,endx,endy);
outtextxy(startx+10,starty+10," SETTINGS");
rectangle(startx+5,starty+35,endx-5,starty+60);
outtextxy(startx+10,starty+43,"Resume Game 'r'");
rectangle(startx+5,starty+65,endx-5,starty+90);
outtextxy(startx+10,starty+73,"Restart 't'");
rectangle(startx+5,starty+95,endx-5,starty+120);
outtextxy(startx+10,starty+103,"Sound 'o'");
if(soundFlag==1)
outtextxy(startx+60,starty+103,"(ON)");
if(soundFlag==0)
outtextxy(startx+60,starty+103,"(OFF)");
rectangle(startx+5,starty+125,endx-5,starty+150);
outtextxy(startx+10,starty+133,"Music 'm'");
if(musicFlag==1)
outtextxy(startx+60,starty+133,"(ON)");
if(musicFlag==0)
outtextxy(startx+60,starty+133,"(OFF)");
rectangle(startx+5,starty+155,endx-5,starty+180);
outtextxy(startx+10,starty+163,"Theme ");
moveto(startx+160,starty+163);
setcolor(BROWN);
outtext("1");
setcolor(MAGENTA);
outtext("2");
setcolor(DARKGRAY);
outtext("3");
setcolor(LIGHTRED);
outtext("4");
setcolor(themeColor);
rectangle(startx+5,starty+185,endx-5,starty+210);
outtextxy(startx+10,starty+193,"Speed 'h' 'l'");
moveto(startx+55,starty+193);
if(speedTimer==100)
outtext("(Low)");
else
outtext("(Hi)");
rectangle(startx+5,starty+215,endx-5,starty+240);
outtextxy(startx+10,starty+223,"Snake Color ");
moveto(startx+160,starty+223);
setcolor(CYAN);
outtext("5");
setcolor(LIGHTBLUE);
outtext("6");
setcolor(WHITE);
outtext("7");
setcolor(LIGHTMAGENTA);
outtext("8");
setcolor(themeColor);
rectangle(startx+5,starty+245,endx-5,starty+270);
outtextxy(startx+10,starty+253,"Exit Game 'e'");
while(1){
option=getch();
if(option=='r')
break;
if(option=='e')
break;
if(option=='o')
break;
if(option=='m')
break;
if(option=='t')
break;
if(option=='1'||option=='2'||option=='3'||option=='4')
break;
if(option=='5'||option=='6'||option=='7'||option=='7')
break;
if(option=='h'||option=='l')
break;
}
if(option=='r')
break;
if(option=='t'){
currentScore=0;
restartFlag=1;
break;
}
if(option=='o'){
if(soundFlag==1)
soundFlag=0;
else soundFlag=1;
}
if(option=='m'){
if(musicFlag==1)
musicFlag=0;
else
musicFlag=1;
}
if(option=='1')
themeColor=BROWN;
if(option=='2')
themeColor=MAGENTA;
if(option=='3')
themeColor=DARKGRAY;
if(option=='4')
themeColor=LIGHTRED;
if(option=='5')
snakeColor=CYAN;
if(option=='6')
snakeColor=LIGHTBLUE;
if(option=='7')
snakeColor=WHITE;
if(option=='8')
snakeColor=LIGHTMAGENTA;
if(option=='h'){
lowSpeed=0;
speedTimer=50;
}
if(option=='l'){
lowSpeed=1;
speedTimer=100;
}
if(option=='e')
while(1){
setfillstyle(SOLID_FILL,BLACK);
bar(150,100,350,200);
setcolor(themeColor);
rectangle(150,100,350,180);
outtextxy(160,110,"Are you sure to exit ? ");
outtextxy(175,150,"Yes('y')");
outtextxy(265,150,"No('n')");
exits=getch();
if(exits=='y')
exit(0);
else if(exits=='n'){
clearviewport();
break;
}
else
continue;
}
}
}
void eatSound(){
sound(2000*2);
delay(5);
nosound();
}
void music(){
sound(4000);
delay(400);
sound(3400);
delay(400);
sound(3000);
delay(400);
sound(3600);
delay(400);
sound(2400);
delay(500);
delay(100);
nosound();
}
Friday, 4 September 2015
Snake Game with sound and other settings
Subscribe to:
Post Comments (Atom)






No comments:
Post a Comment