snake game in c using function
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#include<time.h>
enum direction
{
stop=0, up, down, left, right, die
};
enum flag
{
end=0, start
};
int x, y, fruitX, fruitY, i, j;
const int width=40;
const int height=20;
int score=0, ntail=0, live=3;
int tailX[100], tailY[100];
enum direction dir, prvdir;
enum flag gameover;
void gotoxy(int x, int y);
void print();
void setup();
void display();
void getKey();
void logic();
void gotoxy(int x, int y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void print()
{
system("cls");
gotoxy(35, 14);
printf("WELCOME\n");
gotoxy(31, 15);
for(int r=0;r<15;r++){
for(int q=0;q<100000000;q++);
printf("%c", 177);
}
getch();
}
void setup()
{
dir=stop;
ntail=0;
live=3;
gameover=end;
x=width/2;
y=height/2;
srand(5);
fruitX=rand()%(width-1);
fruitY=rand()%(height-1);
}
void setup2()
{
gameover=end;
x=width/2;
y=height/2;
srand(5);
fruitX=rand()%(width-1);
fruitY=rand()%(height-1);
}
void display()
{
int found;
system("cls");
for(i=0;i<height;i++){
for(j=0;j<width;j++){
if(i==0 || j==0 || i==height-1 || j==width-1)
printf("#");
else if(j==x && i==y)
printf("%c", 153);
else if(j==fruitX && i==fruitY)
printf("%c", 220);
else{
found=0;
for(int k=0;k<ntail;k++){
if(j==tailX[k] && i==tailY[k]){
found=1;
printf("o");
}
}
if(!found)
printf(" ");
}
}
printf("\n");
}
printf("SCORE : [ %d ] LIVE : [ %d ]\n", score, live);
printf("\n[ESC] : EXIT | [SPACE] : PAUSE\n");
}
void getKey()
{
int ch;
if(kbhit()){
ch=getch();
if(ch==' ')
getch();
if(ch==27)
dir=die;
else{
if(ch==224)
ch=getch();
if(ch==72 && dir!=down && dir!=up)
dir=up;
else if(ch==80 && dir!=up && dir!=down)
dir=down;
else if(ch==75 && dir!=right && dir!=left)
dir=left;
else if(ch==77 && dir!=left && dir!=right)
dir=right;
}
}
}
void logic()
{
int prevx=tailX[0];
int prevy=tailY[0];
int prev2x, prev2y;
tailX[0]=x;
tailY[0]=y;
for(i=1;i<=ntail;i++){
prev2x=tailX[i];
prev2y=tailY[i];
tailX[i]=prevx;
tailY[i]=prevy;
prevx=prev2x;
prevy=prev2y;
}
switch(dir)
{
case die:
gameover=start;
live=0;
break;
case up:
y--;
break;
case down:
y++;
break;
case left:
x--;
break;
case right:
x++;
break;
default:
break;
}
if(x==fruitX && y==fruitY){
score+=5;
ntail++;
fruitX=rand()%(width-1);
if(!fruitX){fruitX+=1;}
fruitY=rand()%(height-1);
if(!fruitY){fruitY+=1;}
}
for(int k=0;k<ntail;k++){
if(x==tailX[k] && y==tailY[k]){
live==0?gameover=start:(--live);
setup();
}
}
/*if(x<=0) x=width-1;
if(x>=width) x=1;
if(y<=0) y=height-1;
if(y>=height) y=1;*/
if(x==0 || y==0 || x==width-1 || y==height-1){
live==0?gameover=start:(--live);
setup2();
}
}
void delay()
{
int r, q;
for(r=0;r<10;r++){
for(q=0;q<10000000;q++);
}
}
void intro()
{
printf("\t\t\tWELCOME TO MINI SNAKE GAME\n");
printf("\t\t\t--------------------------");
getch();
system("cls");
printf("\n\nHOW TO PLAY :\n\n\n >use up, down, left, right arrow key to move the snake.\n");
printf(" >There is only one Live, if you hit the walls you will die\n");
printf(" >If you hit the body of snake with its head, then also you will die\n\n");
printf("\n\nGood Luck...");
getch();
}
int main()
{
int ch;
// intro();
//print();
main:
setup();
while(!gameover){
do{
display();
getKey();
logic();
Sleep(30);
}while(live);
if(live==0)
break;
}
printf("\n\nGAMEOVER... !!");
printf("\nEnter any key to continue.."); getch();
while(1){
system("cls");
printf("\nPLAY AGAIN [1] | EXIT [0]");
printf("\nEnter choice : ");
scanf("%d", &ch);
switch(ch){
case 0:
exit(0);
case 1:
goto main;
}
}
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#include<time.h>
enum direction
{
stop=0, up, down, left, right, die
};
enum flag
{
end=0, start
};
int x, y, fruitX, fruitY, i, j;
const int width=40;
const int height=20;
int score=0, ntail=0, live=3;
int tailX[100], tailY[100];
enum direction dir, prvdir;
enum flag gameover;
void gotoxy(int x, int y);
void print();
void setup();
void display();
void getKey();
void logic();
void gotoxy(int x, int y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void print()
{
system("cls");
gotoxy(35, 14);
printf("WELCOME\n");
gotoxy(31, 15);
for(int r=0;r<15;r++){
for(int q=0;q<100000000;q++);
printf("%c", 177);
}
getch();
}
void setup()
{
dir=stop;
ntail=0;
live=3;
gameover=end;
x=width/2;
y=height/2;
srand(5);
fruitX=rand()%(width-1);
fruitY=rand()%(height-1);
}
void setup2()
{
gameover=end;
x=width/2;
y=height/2;
srand(5);
fruitX=rand()%(width-1);
fruitY=rand()%(height-1);
}
void display()
{
int found;
system("cls");
for(i=0;i<height;i++){
for(j=0;j<width;j++){
if(i==0 || j==0 || i==height-1 || j==width-1)
printf("#");
else if(j==x && i==y)
printf("%c", 153);
else if(j==fruitX && i==fruitY)
printf("%c", 220);
else{
found=0;
for(int k=0;k<ntail;k++){
if(j==tailX[k] && i==tailY[k]){
found=1;
printf("o");
}
}
if(!found)
printf(" ");
}
}
printf("\n");
}
printf("SCORE : [ %d ] LIVE : [ %d ]\n", score, live);
printf("\n[ESC] : EXIT | [SPACE] : PAUSE\n");
}
void getKey()
{
int ch;
if(kbhit()){
ch=getch();
if(ch==' ')
getch();
if(ch==27)
dir=die;
else{
if(ch==224)
ch=getch();
if(ch==72 && dir!=down && dir!=up)
dir=up;
else if(ch==80 && dir!=up && dir!=down)
dir=down;
else if(ch==75 && dir!=right && dir!=left)
dir=left;
else if(ch==77 && dir!=left && dir!=right)
dir=right;
}
}
}
void logic()
{
int prevx=tailX[0];
int prevy=tailY[0];
int prev2x, prev2y;
tailX[0]=x;
tailY[0]=y;
for(i=1;i<=ntail;i++){
prev2x=tailX[i];
prev2y=tailY[i];
tailX[i]=prevx;
tailY[i]=prevy;
prevx=prev2x;
prevy=prev2y;
}
switch(dir)
{
case die:
gameover=start;
live=0;
break;
case up:
y--;
break;
case down:
y++;
break;
case left:
x--;
break;
case right:
x++;
break;
default:
break;
}
if(x==fruitX && y==fruitY){
score+=5;
ntail++;
fruitX=rand()%(width-1);
if(!fruitX){fruitX+=1;}
fruitY=rand()%(height-1);
if(!fruitY){fruitY+=1;}
}
for(int k=0;k<ntail;k++){
if(x==tailX[k] && y==tailY[k]){
live==0?gameover=start:(--live);
setup();
}
}
/*if(x<=0) x=width-1;
if(x>=width) x=1;
if(y<=0) y=height-1;
if(y>=height) y=1;*/
if(x==0 || y==0 || x==width-1 || y==height-1){
live==0?gameover=start:(--live);
setup2();
}
}
void delay()
{
int r, q;
for(r=0;r<10;r++){
for(q=0;q<10000000;q++);
}
}
void intro()
{
printf("\t\t\tWELCOME TO MINI SNAKE GAME\n");
printf("\t\t\t--------------------------");
getch();
system("cls");
printf("\n\nHOW TO PLAY :\n\n\n >use up, down, left, right arrow key to move the snake.\n");
printf(" >There is only one Live, if you hit the walls you will die\n");
printf(" >If you hit the body of snake with its head, then also you will die\n\n");
printf("\n\nGood Luck...");
getch();
}
int main()
{
int ch;
// intro();
//print();
main:
setup();
while(!gameover){
do{
display();
getKey();
logic();
Sleep(30);
}while(live);
if(live==0)
break;
}
printf("\n\nGAMEOVER... !!");
printf("\nEnter any key to continue.."); getch();
while(1){
system("cls");
printf("\nPLAY AGAIN [1] | EXIT [0]");
printf("\nEnter choice : ");
scanf("%d", &ch);
switch(ch){
case 0:
exit(0);
case 1:
goto main;
}
}
return 0;
}
No comments:
Post a Comment