error in snake game

//i write program of snake mania game but it has one problem
//i have no idea how snake is finish behind head like tail is remain same place //please help me
//my program is there

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

if (counter ==0){drawfood();}

if (kbhit ()){
ch = getch (); //detect cheracter from keyboard
switch (ch){
case 'd':
{if (ch!='a')
ch='d';
break;}
case 'w':
{if (ch!='s')
ch='w';
break;}
case 'a':
{if (ch!='d')
ch='a';
break;}
case 's':
{if (ch!='w')
ch='s';
break;}}
}
if (ch=='d'){dx+=1;snakeprogress (dy,dx);}
else if (ch=='w'){dy-=1;snakeprogress (dy,dx);}
else if (ch=='a'){dx-=1;snakeprogress (dy,dx);}
else if (ch=='s'){dy+=1;snakeprogress (dy,dx);}
detectcollision (dy,dx);

bountary();





counter+=1;
Sleep (200);
system ("cls");
}
displayscoreboard ();
system ("pause");
}
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 snakeprogress (int y,int x)
{if (map [dy][dx]==42)
{life=0;running=false;} //end game when colliode snake with itself
//if (counter /3==1)
// {map [18][4]= 32;}
map [y][x] = 42;
//if (counter==0)
// {x1=x;y1=y;}



}
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 displayscoreboard ()
{
cout<<"\n\t\tScore : "<<score;

cout<<"\t\t\tLife : "<<life;
cout<<"\n\n";




}
void bountary ()
{
for (int i=0;i<20;i++)
{for (int j=0;j<40;j++)
{cout <<map [i][j];}
cout<<endl;}
}
Topic archived. No new replies allowed.