snake game help

I use 2D arrays to display its all things like bountary,food,snake but snake is going to infinity once it start it has no tail
so anyone can help me plzzz
I write a simple program of snake mania game in c++
Last edited on
code?
//SNAKE MANIA GAME
//This is my first game in c++

#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
int counter=0,dx=20,dy=9,xf,yf,score=0,life=1;
char ch ='w';
bool running=true;
char map [20][40] ={ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"! !",
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",};
void displayscoreboard ();
void drawfood ();
void bountary ();
void drawfood ();
int detectcollision (int dy,int dx);

void main ()
{
while (running)
{displayscoreboard();
if (counter ==0){drawfood();}
if (kbhit ()){
ch = getch (); //detect cheracter from keyboard
switch (ch){
case 'd':
{ch='d';
break;}
case 'w':
{ch='w';
break;}
case 'a':
{ch='a';
break;}
case 's':
{ch='s';
break;}}
}
if (ch=='d'){map [dy][dx+1]='*'; //if no key press then continuous snake progress
dx+=1;}
else if (ch=='w'){map [dy-1][dx] ='*';
dy -=1;}
else if (ch=='a'){map [dy][dx-1] = '*';
dx-=1;}
else if (ch=='s'){map [dy+1][dx]='*';
dy+=1;}
detectcollision (dy,dx); //check for collision
bountary(); //draw bountary
counter+=1;
Sleep (500);
system ("cls");
}
displayscoreboard ();
system ("pause");
}
void displayscoreboard ()
{cout<<"\n\t\tScore : "<<score; //display score
cout<<"\t\t\tLife : "<<life; //display life
cout<<"\n\n";}
int detectcollision (int dy,int dx)
{if (dx==0||dy==0||dx==38||dy==19) //collision with walls
{life =0;running = false;}
if (xf==dy&&yf==dx) //collision with food
{score +=20;drawfood ();}
return 0;
}
void drawfood ()
{ do {yf = rand() % (40 - 2) +1 ; // Generate random x and y values within the map
xf = rand() % (20 - 2) + 1;
}while (map [xf][yf] != 32); // If location is not free try again
map[xf][yf]='@'; }
void bountary ()
{
for (int i=0;i<20;i++)
{for (int j=0;j<40;j++)
{cout <<map [i][j];} //draw bountary of map [][]
cout<<endl;}
}
anyone help please
Topic archived. No new replies allowed.