Need help with my snake game

Hi guys I'm new here and I need help with my code.
The code i'm using is already used so I want to make it look mine.

I have 4 question
1.How Can I put a sound on my game? (I know there are already some topic's about this but I need the answer to where I will put the sound code to make the game have sounds.
2.I want my snake to look like something else
3.How can I change my apple texture too?
4.The backround color is green but I want it too look like something else.Where should I find the code to the backround?

The 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
 //My First game moded and turned into my own game.Hope you like it
# include <iostream>
# include <cstdlib>
# include <string>
# include <Windows.h>
# include <ctime>
# include <cstdio>
# include <conio.h>

using namespace std;

bool Array[24][79] = {false};
int HeadX = 0;
int HeadY = 0;
int TailX = 0;
int TailY = 0;
int direction = 1;
int SnakeXCoordinates[200]={0};
int SnakeYCoordinates[200]={0};
int SnakeSize=0;

bool egg = false;
int eggX = 0;
int eggY = 0;
int Score = 0;

void GotoXY(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}

void Clear_Box(int x1, int y1, int x2, int y2)
{
GotoXY(10,1);
cout << "Score: " << Score << " Left:a Right:d Up:w Down:s " << endl;
GotoXY(10,2);
cout << "Press ESC to exit" << " Made By Xavier108 " << endl;

int x,y;
for (y=y1+1;y<y2;y++)
{
for (x=x1+1;x<x2;x++)
{
GotoXY(x,y);
cout.put(' ');
cout.flush();
}
}
}
void Make_Box()
{
GotoXY(0,0);
int j=4;
for (int i=5; i <= 75;i++)
{
GotoXY(i,j);
cout.put((char) 205);
cout.flush();
}

j=22;
for (int i=5; i <= 75;i++)
{
GotoXY(i,j);
cout.put((char) 205);
cout.flush();
}

int i=5;
for (j=4; j <= 22;j++)
{
GotoXY(i,j);
cout.put((char) 186);
cout.flush();
}

i=75;
for (j=4; j <= 22;j++)
{
GotoXY(i,j);
cout.put((char) 186);
cout.flush();
}
return;
}
void Initialize_Snake()
{
Score = 0;
egg = false;
int x,y;
for (y=0;y<24;y++)
for (x=0;x<79;x++)
Array[y][x]=false;

Array[10][50] = true;
Array[10][51] = true;
Array[10][52] = true;
TailX = 50;
TailY = 10;
HeadX = 52;
HeadY = 10;
direction = 1;

SnakeSize=3;
SnakeXCoordinates[0]=50;
SnakeXCoordinates[1]=51;
SnakeXCoordinates[2]=52;
SnakeYCoordinates[0]=10;
SnakeYCoordinates[1]=10;
SnakeYCoordinates[2]=10;
}
void Key_Pressed()
{
char ch;
if ( kbhit() )
{
ch = getch();
if ( (ch == 'd') && (direction != 1 ) && (direction != 3) )
{
direction = 1;

}
else if ( (ch == 'a') && (direction != 1 ) && (direction != 3) )
{
direction = 3;

}
if ( (ch == 'w') && (direction != 2 ) && (direction != 4) )
{
direction = 2;

}
if ( (ch == 's') && (direction != 2 ) && (direction != 4) )
{
direction = 4;

}
if (ch == (char) 27) // Escape key to exit
exit(1);
}
}
void Update_Snake_Location()
{
Array[TailY][TailX] = false;
for (int i=0;i<SnakeSize-1;i++)
{
SnakeXCoordinates[i]= SnakeXCoordinates[i+1];
SnakeYCoordinates[i]= SnakeYCoordinates[i+1];
}

if (direction == 1)
HeadX++;
else if (direction == 2)
HeadY--;
else if (direction == 3)
HeadX--;
else
HeadY++;

SnakeXCoordinates[SnakeSize-1]=HeadX;
SnakeYCoordinates[SnakeSize-1]=HeadY;

TailX=SnakeXCoordinates[0];
TailY=SnakeYCoordinates[0];

Array[HeadY][HeadX] = true;

if (HeadX == eggX && HeadY == eggY)
{
SnakeSize++;
for (int i=SnakeSize-1;i>=1;i--)
{
SnakeXCoordinates[i]= SnakeXCoordinates[i-1];
SnakeYCoordinates[i]= SnakeYCoordinates[i-1];
}
Score = Score + 10;

GotoXY(10,2);
cout << " ";
cout.flush();
GotoXY(10,2);
cout << "Score: " << Score << " Left:a Right:d Up:w Down:s " << endl;
Sleep(200);
egg = false;
}
}
void Display_Snake()
{
int x,y;
for (y=4;y<=22;y++)
{
for (x=5;x<=75;x++)
{
if (Array[y][x])
{
GotoXY(x,y);

if (x==TailX && y==TailY)
{
cout.put((char) 177);
cout.flush();
}
else if (x == HeadX && y ==HeadY)
{
cout.put((char) 178);
cout.flush();
}
else
{
cout.put((char) 219);
cout.flush();
}
}
}
}
}
void Check_Collision()
{
if (HeadX == 5 || HeadX == 75 || HeadY == 4 || HeadY == 22)
{
GotoXY(15,10);
cout << "You lose,THANKS FOR PLAYING Snakes By Xavier108" << endl;
GotoXY(10,15);
cout << "Want to play again, Press y key to continue, any other to exit" << endl;
char ch = getch();
if (ch == 'y')
{
Clear_Box(5,4,75,22);
Initialize_Snake();
return;
}
else
exit(1);
}

for (int i=0;i<SnakeSize-1;i++)
{
if (( SnakeXCoordinates[i] == HeadX ) &&
(SnakeYCoordinates[i] == HeadY ))
{
GotoXY(15,10);
cout << "You lose,THANKS FOR PLAYING Snakes By Xavier108" << endl;
GotoXY(10,15);
cout << "Want to play again, Press y key to continue, any other to exit" << endl;
char ch = getch();
if (ch == 'y')
{
Clear_Box(5,4,75,22);
Initialize_Snake();
return;
}
else
exit(1);
}
}

}
void Clear_from_Tail()
{
GotoXY(TailX,TailY);
cout.put(' ');
cout.flush();

}
void Create_Egg()
{
bool overlapping = false;
do
{
eggX = (rand()%66) + 7;
eggY = (rand()%15) + 6;
for (int i=0;i<SnakeSize;i++)
{
if (( SnakeXCoordinates[i] == eggX)&& (SnakeYCoordinates[i] == eggY ))
{
overlapping = true;
break;
}
}
} while (overlapping);
}
void Check_Egg_Update_Score()
{
if (!egg)
{
Create_Egg();
egg = true;
}
GotoXY(eggX,eggY);
cout.put( (char) 232 );
cout.flush();

}
int main()
{
srand( time(NULL));
system("color A0");
Initialize_Snake();
system("cls");
Make_Box();
Clear_Box(5,4,75,22);

for ( ; ; )
{
Display_Snake();
Key_Pressed();
Sleep(150);
Clear_from_Tail();
Update_Snake_Location();
Check_Collision();

Check_Egg_Update_Score();
}
return 0;
}
so what you are saying is you took someones code and changed a few lines and are calling it your code so you can turn in at school? and I would NEVER suggest system but hey its up to you and if you "wrote" this code you would know how to change the color easily especially if you took the two seconds to read the code try line 300 setConsoleTextAttribute
and if you googled "change color color c++" first thing to come up would be http://www.cplusplus.com/forum/beginner/5830/
Ps you should try using
1
2
3
#include <windows.h>

SetConsoleTextAttribute( GetStdHandle( STD_INPUT_HANDLE ), 0x0A );
Last edited on
Not really when I saw the code.The guy just also get the code from someone too.He also said it was ok to get this code.
How do you add sounds by the way?
try this:
1
2
#includ e<windows.h>
PlaySound(TEXT("recycle.wav"), NULL, SND_FILENAME);

I haven't used sounds on windows before but it should work
http://msdn.microsoft.com/en-us/library/ms712879.aspx
Thanks for the tip giblit.
About the sounds.All I know the only music type you can use is wav file.
Last edited on
I seem to be having a problem when I put
1
2
3
#include <windows.h>

SetConsoleTextAttribute( GetStdHandle( STD_INPUT_HANDLE ), 0x0A );

Can you put it on the code so I can just copy it and put it on Dev C++
put it where you want to play the sound =p.
and btw if you want to change how the snake looks just change the characters that make up the snake I think thats on line 207, 208, and 213
EDIT: going to eat supper so if the console text attribute isn't working just use the system color I guess but I wouldn't suggest that
Last edited on
Check out this link also maybe
http://cplusplus.com/forum/articles/28558/
I'll be waiting for you.
P.S what the char number for ">"
Cause I want my snake to look like this
When moving right --->
When moving left <---
When moving up ^
|
|
|


When moving down |
|
|
V
How can I do it.
Can you do that to my code?
you don't need to use the numbers you can just put '>' he put the numbers because he was too lazy to use the ascii code and put the char itself in, or even to copy/paste it from the character map.
closed account (jyU4izwU)
I Think What You Are Turning in is agenst the law. P.S. giblit has a point. :) B.T.W. If You Want a good>Great mark for your levle... try using DirectX.
Last edited on
Technically speaking, plagiarism is not against the law. However, it is unethical and breaks pretty much any institution's code. As a licensed professional, one would destroy his or her career by plagiarizing. However... Xavier108 never hints at the possibility that this is for a school project. Using someone's basic design as a model to get started on one's own complex project is a good idea. But it is for school and it is going to be submitted like this... Xavier108, you better think carefully about your next choices....


Not really when I saw the code.The guy just also get the code from someone too.He also said it was ok to get this code.

[Start angry rant.]
So if someone kidnapped a child and told you that he/she won't mind you taking the child if you use the child for your own devilish ways, would you still do it? The original author must be the one to give permission to someone for using the code without giving credit. And even then, the author also has to give special permission to someone for distributing the code.
In short, document the source of the code in your program and give due credit to the original author(s) and make sure the secondary source you are receiving the code from has been given permission to distribute the code.
[End angry rant.]
Topic archived. No new replies allowed.