snake game

i am making snake game but i found difficulty in increasing the length of snake as snake eat the food...plz tell me what can i do...i m using complier turbo c++...i appreciate if i get any help from here....plz reply me soon


here is my code




#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<constream.h>
#include<stdio.h>
int x,y;
int i=5,j=5; // initial position of snake
int count=1;//sixe of snake


void food()
{
randomize();
int xAxis=0; //initial position of food
int yAxis=0;

// position of food

xAxis=random(30);
yAxis=random(30);


gotoxy(xAxis, yAxis);
cout<<"&";
x=xAxis;
y=yAxis;

}
int moveUp()
{

clrscr();
j++;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";
delay(200);
return 0;
}
int moveDown()
{
clrscr();
j--;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";

delay(200);
return 0;
}
void moveRight()
{
clrscr();
i++;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";
delay(200);

}
void moveLeft()
{
clrscr();
i--;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";
delay(200);
}

/* void printSnake()
{
if(ch=='i')
{
clrscr();
for(int i=1;i<=count;i++)
{
gotoxy(i,j);
cout<<"*";
j++;
}

}
else if(ch=='k')
{
clrscr();
for(int i=1;i<=count;i++)
{ `
gotoxy(i,j);
cout<<"*";
j--;
}


}
else if(ch=='j')
{
clrscr();
for(int i=1;i<=count;i++)
{
gotoxy(i,j);
cout<<"*";
i++;
}

}
else if(ch=='l')
{
clrscr();
for(int i=1;i<=count;i++)
{
gotoxy(i,j);
cout<<"*";
i--;
}

}
else if(ch==NULL)
{
gotoxy(i,j);
cout<<"*";
}

} */





void main()
{
clrscr();
//boundary();
food();

Start:
food();
gotoxy(i, j);
cout<<"*";
char ch;
ch=getch();

while(ch!='q'){
if(ch=='i')
{

while(!kbhit())
{

if(i==x &&j==y)
{

goto Start;

}
else

moveUp();
}


}
else if(ch=='k')
{

while(!kbhit())
{

if(i==x && j==y)
{
goto Start;
}
else

moveDown();
}
}


else if(ch=='l')
{

while(!kbhit())
{

if(i==x&&j==y)

{
goto Start;
}
else

moveRight();

}
}

else if(ch=='j')
{

while(!kbhit())
{
if(i==x&&j==y)
{
goto Start;
}
else

moveLeft();

}

}

ch=getch();


}


getch();
}
Last edited on
hello, please use code tags!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<constream.h>
#include<stdio.h>
int x,y;
int i=5,j=5; // initial position of snake
int count=1;//sixe of snake


void food()
{
randomize();
int xAxis=0; //initial position of food
int yAxis=0;

// position of food

xAxis=random(30);
yAxis=random(30);


gotoxy(xAxis, yAxis);
cout<<"&";
x=xAxis;
y=yAxis;

}
int moveUp()
{

clrscr();
j++;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";
delay(200);
return 0;
}
int moveDown()
{
clrscr();
j--;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";

delay(200);
return 0;
}
void moveRight()
{
clrscr();
i++;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";
delay(200);

}
void moveLeft()
{
clrscr();
i--;
gotoxy(x,y);
cout<<"&";
gotoxy(i,j);
cout<<"*";
delay(200);
}

/*	void printSnake()
{
if(ch=='i')
{
clrscr();
for(int i=1;i<=count;i++)
{
gotoxy(i,j);
cout<<"*";
j++;
}

}
else if(ch=='k')
{
clrscr();
for(int i=1;i<=count;i++)
{ `
gotoxy(i,j);
cout<<"*";
j--;
}


}
else if(ch=='j')
{
clrscr();
for(int i=1;i<=count;i++)
{
gotoxy(i,j);
cout<<"*";
i++;
}

}
else if(ch=='l')
{
clrscr();
for(int i=1;i<=count;i++)
{
gotoxy(i,j);
cout<<"*";
i--;
}

}
else if(ch==NULL)
{
gotoxy(i,j);
cout<<"*";
}

} */





void main()
{
clrscr();
//boundary();
food();

Start:
food();
gotoxy(i, j);
cout<<"*";
char ch;
ch=getch();

while(ch!='q'){
if(ch=='i')
{

while(!kbhit())
{

if(i==x &&j==y)
{

goto Start;

}
else

moveUp();
}


}
else if(ch=='k')
{

while(!kbhit())
{

if(i==x && j==y)
{
goto Start;
}
else

moveDown();
}
}


else if(ch=='l')
{

while(!kbhit())
{

if(i==x&&j==y)

{
goto Start;
}
else

moveRight();

}
}

else if(ch=='j')
{

while(!kbhit())
{
if(i==x&&j==y)
{
goto Start;
}
else

moveLeft();

}

}

ch=getch();


}


getch();
}
will u help me
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<constream.h>
#include<stdio.h>
int x,y;
int i=5,j=5; // initial position of snake
int count=1;//sixe of snake


void food() // generate random food
{
randomize();
int xAxis=0; //initial position of food
int yAxis=0;

// position of food

xAxis=random(30);
yAxis=random(30);


gotoxy(xAxis, yAxis);
cout<<"&";
x=xAxis;
y=yAxis;

}
int moveUp() // functionto move snake up
{

clrscr();
j++;
gotoxy(x,y);
cout<<"&"; // represent food
gotoxy(i,j);
cout<<"*"; // represent snake
delay(200);
return 0;
}
int moveDown() // function to move snake down
{
clrscr();
j--;
gotoxy(x,y); // position of food
cout<<"&"; // represent food
gotoxy(i,j); // position of snake
cout<<"*"; // represent snake

delay(200);
return 0;
}
void moveRight() // function to move snake right
{
clrscr();
i++;
gotoxy(x,y); // position of food
cout<<"&"; // represent food
gotoxy(i,j); // position of snake
cout<<"*"; // represent snake
delay(200);

}
void moveLeft() // function to move snake left
{
clrscr();
i--;
gotoxy(x,y); // position of food
cout<<"&"; // represent food
gotoxy(i,j); // position of snake
cout<<"*"; // represent snake
delay(200);
}

void main() // main function
{
clrscr();
food();

Start:
food();
gotoxy(i, j);
cout<<"*";
char ch;
ch=getch();

while(ch!='q'){
if(ch=='i')
{

while(!kbhit())
{

if(i==x &&j==y) // if position of snake and food is same then go to start
{

goto Start;

}
else // if position of snake and food is not same the move up

moveUp();
}


}
else if(ch=='k')
{

while(!kbhit())
{

if(i==x && j==y) // if position of snake and food is same then go to start
{
goto Start;
}
else // if position of snake and food is not same the move up

moveDown();
}
}


else if(ch=='l')
{

while(!kbhit())
{

if(i==x&&j==y) // if position of snake and food is same then go to start

{
goto Start;
}
else // if position of snake and food is not same the move up

moveRight();

}
}

else if(ch=='j')
{

while(!kbhit())
{
if(i==x&&j==y) // if position of snake and food is same then go to start
{
goto Start;
}
else // if position of snake and food is not same the move up

moveLeft();

}

}

ch=getch();


}


getch();
}
what are you doing to move your snake tail btw ?

i created my snake game in which i use condition that will print a ('*' i.e snake body) in the next coordinate of head where it is moving
and i am printing (' ' i.e space) on snake tail,
so that snake is moved one coordinate ahead,,
now if i will eat food i will not print (' ' i.e space) on snake tail,
i want to increase the length of snake as snake eat the food...but my program only generates random food and snake eat it
i have no idea what to do to increase the sixe of snake... how we know about the head and tail of snake and what condition should be applied..
You will have to redesign your complete code.
Make a double dimension array which will store the coordinates of the previous position of the parts of the snake.
At every step, when the snake move forward,
1. Print a space at the coordinates of the last entry i.e Length - 1.
2. Now overwrite the last element with the 2nd last one, 2nd last one with the 3rd last and so on.
3. Put the value of the current position in the 1 element i.e subscript 0 and print a * at that position.
Keep doing these 3 steps after every 50 ms or so.
4. Now when the snake gets the food then follow all the above steps but do not delete the last last coordinate from the array or print space at that place and wait for 2 times the time you waited for the previous steps.

If you want to increase the length by more than one repeat the 4th step as many times as you want.
You will have to keep a variable length to get the last element
It just sounds complex but it is not just try coding this and if you get any problem post it here. Also read the following code carefully.

1
2
3
cout<<"Always use the code tags on the right side of  the edit box"
                 <<"They look like '<>' "
                 <<"I did the same while writing this";

And why did you include constream.h
Last edited on
plz tell me the error in my code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<stdlib.h>
#include<stdio.h>
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<constream.h>

static int snake[300][2], food[30][2], screen[81][51], i, j, k;
static int headx, heady, foodx, foody, level, tailx, taily, tail;
char dir, key;

main()
{

	clrscr();
	randomize();
//start:

	crash:
	tail=0;
	dir='x';


	// initialize snakes first coordinates

	do
	{
		headx=random(80)+1;
		heady=random(48)+1;
		snake[0][0]=headx;
		snake[0][1]=heady;
	}
	while(screen[headx][heady]==1);



	// put food
	for(i=0; i<30; i++)
	do
	{
		foodx=random(78)+1;
		foody=random(47)+1;
		food[i][0]=foodx;
		food[i][1]=foody;
	}
	while(screen[foodx][foody]==1 || (foodx==headx && foody==heady));
	//display food
	textcolor(14);
	for(i=1; i<30; i++)
	{
		foodx=food[i][0];
		foody=food[i][1];
		gotoxy(foodx, foody);
		if(food[i][0]!=0)
		cout<<"$";
	}


main_loop:
	{
		tailx=snake[tail][0];
		taily=snake[tail][1];
	}
	if(tail!=0)
	{
		for(i=tail; i>0; i--)
		{
			snake[i][0]=snake[i-1][0];
			snake[i][1]=snake[i-1][1];
		}
	}

	//erase the last part of tail

	if(tail!=0 && taily!=0)
	{
		gotoxy(tailx, taily);
		cout<<" ";
	}
	//move snake
	switch(dir)
	{
		case 'u':
			if(heady>0)
			heady-=1;
			snake[0][1]=heady;
		break;
		case 'd':
			if(heady<49)
			heady+=1;
			snake[0][1]=heady;
		break;
		case 'l':
			if(headx>1)
			headx-=1;
			snake[0][0]=headx;
		break;
		case 'r':
			if(headx<80)
			headx+=1;
			snake[0][0]=headx;
		break;
		case 'p':
			dir='p';
			while(!kbhit());
		break;
	}// end of switch

		// control if snake crashed

		if(screen[headx][heady]==1)
		{
			gotoxy(25, 25);
			cout<<"CRASHED !...... ";
			while(getch()!=32);
			goto crash;
		}

		// control if snake crashed to himself
		for(i=1; i<=tail; i++)
		{
		if(headx==snake[i][0] && heady==snake[i][1])
		{
			textcolor(5);
			gotoxy(25, 25);
			cout<<"CRASHED ! ....... ";
			while(getch()!=32);
			goto crash;
		}
		}
		// control if snake ate food

		for(i=1; i<30; i++)
		{
			foodx=food[i][0];
			foody=food[i][1];
			if(headx==foodx && heady==foody && food[i][0]!=0)
			{
				food[i][0]=0;
				tail+=1;
			}
		}

		// draw snake

		for(i=0; i<=tail; i++)
		if(snake[i][0]!=0 && snake[i][1]!=0)
		{
			gotoxy(snake[i][0], snake[i][1]);
			cout<<"#";
		}
		delay(200);
	      	if(kbhit())
		{
			key=getch();
			switch(key)
				{
					case 72:
						if(dir!='d')
						dir='u';
					break;
					case 80:
						if(dir!='u')
						dir='d';
					break;
					case 75:
						if(dir!='r')
						dir='l';
					break;
					case 77:
						if(dir!='l')
						dir='r';
					break;
					// pause
					case 'p':
						dir='p';
					break;
					case 27:
						exit(0);
					break;
					}
				} 
			goto main_loop;
			getch();

		}



Last edited on
I have edited a code a bit and have added somethings.
The biggest mistake you made is that the screen size is 80x24 not 80x50.
Sorry but the code is sort of unformatted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<stdlib.h>
#include<stdio.h>
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<constream.h>

static int snake[300][2], food[30][2], screen[80][25], i, j, k;
static int headx, heady, foodx, foody, level, tailx, taily, tail;
char dir, key;

main()
{

	//clrscr();
	randomize();
	_setcursortype(_NOCURSOR);  //this wouldn't display the cursor.
//start:

	crash: clrscr();
	tail=0;
	dir='r';


	// initialize snakes first coordinates
	{
		headx=random(79)+1;
		heady=random(23)+1;
		snake[0][0]=headx;
		snake[0][1]=heady;
	}



	// put food
	for(i=0; i<30; i++)
	do{
		foodx=random(78)+1;
		foody=random(23)+1;
		food[i][0]=foodx;
		food[i][1]=foody;
	}while(foodx==0&&foody==0) ;
	//display food
	textcolor(14);
	for(i=0; i<30; i++)
	{
		foodx=food[i][0];
		foody=food[i][1];
		gotoxy(foodx, foody);
		if(food[i][0]!=0)
		cout<<"$";
	}


main_loop:
	{
		tailx=snake[tail][0];
		taily=snake[tail][1];
		headx=snake[0][0];
		heady=snake[0][1];
	}
	if(tail!=0)
	{
		for(i=tail; i>0; i--)
		{
			snake[i][0]=snake[i-1][0];
			snake[i][1]=snake[i-1][1];
		}
	}

	//erase the last part of tail

	if(tailx!=0 && taily!=0)
	{
		gotoxy(tailx, taily);
		cout<<" ";
	}
	//move snake
	chdir:
	switch(dir)
	{
		case 'u':
			if(heady>0)
			heady-=1;
			snake[0][1]=heady;
			//else; //snake dies
		break;
		case 'd':
			if(heady<24)
			heady+=1;
			snake[0][1]=heady;
			//else; //snake dies
			break;
		case 'l':
			if(headx>1)
			headx-=1;
			snake[0][0]=headx;
			//else; //snake dies
		break;
		case 'r':
			if(headx<80)
			headx+=1;
			snake[0][0]=headx;
			//else; // snake dies
		break;
		case 'p':
			dir='p';
			while(!kbhit());
		break;
	}// end of switch

		// control if snake crashed

		/*if(screen[headx][heady]==1)
		{
			gotoxy(25, 12);
			cout<<"CRASHED !...... ";
			while(getch()!=32);
			goto crash;
		}*/

		// control if snake crashed to himself
		for(i=1; i<tail; i++)
		{
		if(headx==snake[i][0] && heady==snake[i][1])
		{
			textcolor(5);
			gotoxy(25, 25);
			cout<<"CRASHED ! ....... ";
			while(getch()!=32);
			goto crash;
		}
		}
		// control if snake ate food
		// draw snake

		//for(i=0; i<=tail; i++)
		if(snake[0][0]!=0 && snake[0][1]!=0)
		{
			gotoxy(snake[0][0], snake[0][1]);
			cout<<"#";
		}

		for(i=0; i<30; i++)
		{
			foodx=food[i][0];
			foody=food[i][1];
			if(headx==foodx && heady==foody && food[i][0]!=0)
			{
				food[i][0]=0;
				tail+=1;
				//goto chdir;
			}
		}


		delay(100);
		if(kbhit())
		{
			asd:key=getch();
			switch(key)
				{
					case 72:
						if(dir!='d')
						dir='u';
					break;
					case 80:
						if(dir!='u')
						dir='d';
					break;
					case 75:
						if(dir!='r')
						dir='l';
					break;
					case 77:
						if(dir!='l')
						dir='r';
					break;
					// pause
					case 'p':
						dir='p';
					break;
					case 27:
						exit(0);
					break;
					case 0: goto asd;
					}
				}
			gotoxy(1,1);cout<<"Score"<<tail;
			goto main_loop;
			//getch();

		}
Last edited on
we use 2d array in snake game..but how we understand about index like when we initialize the snake first co-ordinate then what index should be given...
i use it in my snake game like as suggested

snake[0][0]=headx;
snake[0][1]=heady;

but i hve little confusion about what index should be given.... whether i given here another index,,, if i give another index or replace the index then i get unwanted result....
and alse i use
foodx=food[i][0];
foody=food[i][1];

sir if i replace it with this
foodx=food[0][i];
foody=food[1][i];

then there will no increase in length and food will appear next to eachother.........not generate in random place
i with my friend make this game and i use array in this game just because of her help.
You can think 2D arrays as tables when we say
int array[rows][cols];
, like in this case
int food[30][2];
, it will declare a table sort of thing with 30 rows and 2 columns. When we say food[0][0] it will point to the 1st cell. food[1][0] will point to the first col. of 2nd row, and food[2][1] will point to 2nd col. of third row and so on. Now when we are using coordinates we say that the 1st col. in our table is the x coordinate and 2nd col. is the y coordinate.
For more you can search the net for info on 2d arrays.
Topic archived. No new replies allowed.