How to code snake game

Question:


Repl link:

code snippet
```colourfull

Welcome @GideonAppiah!

Making a game heavily depends on what language you want to use to create it, which can vary greatly. Which language are you most familiar with? If you are not familiar with any, I’d recommend learning Python or JavaScript, which are easier languages to learn and have many good uses. If you do choose to learn Python, a module called Pygame or Turtle is a nice start to developing games.

2 Likes

Hello @GideonAppiah welcome to the forums!

First of all could you please format your post like how it is laid out? If your question is the title then I would recommend looking up on YouTube how to make a snake game. I hope this helps :grinning: ! If you find this helpful you can mark it as the solution.

2 Likes

Use c++

1. #include<iostream.h>
2. #include<conio.h>
3. #include<graphics.h>
4. #include<dos.h>
5. #include<stdlib.h>
6. #include<stdio.h>
7. #include<time.h>
8. #include<string.h>
9. class Snake
10. {
11. int p1,p2,v1,v2,v3,e1,e2,prev,now,n,colr,dsp,cnt,dly,m;
12. int stp, egGen;
13. int xr, yr;
14. void caught();
15. public:
16. long scr;
17. int strtX,strtY,endX,endY;
18. int pos[100][2];
19. void show();
20. void init();
21. void egg();
22. void transpose();
23. void gnrtCond();
24. void gnrtUnCond();
25. void check();
26. void checkEgg();
27. void move();
28. void chngDir();
29. void sndEt();
30. void sndCgt();
31. int test();
32. void score();
33. Snake();
34. Snake(Snake*);
35. ~Snake();
36. };
37. Snake::Snake()
38. {
39. }
40. Snake::~Snake()
41. {
42. }
43. void Snake::checkEgg()
44. {
45. if((e1 == p1) && (e2 == p2))
46. { sndEt();
47. egg();
48. dly--;
49. score();
50. n++;
51. }
52. }
53. void Snake::sndEt()
54. { nosound();
55. sound(2500);
56. delay(2);
57. nosound();
58. }
59. void Snake::sndCgt()
60. { nosound();
61. for(int x=1000;x>0;x--)
62. { sound(x);
63. delay(1);
64. }
65. nosound();
66. }
67. void Snake::score()
68. { char *p;
69. ltoa(scr,p,10);
70. settextstyle(8,0,1);
71. setcolor(0);
72. outtextxy(585,40,p);
73. if(egGen != 1){
74. scr = scr + dly / 10;
75. }
76. ltoa(scr,p,10);
77. setcolor(10);
78. outtextxy(585,40,p);
79. }
80. void Snake::gnrtCond()
81. { if(n < 367)
82. { if(now == 8 && (prev != 8 && prev != 2))
83. { pos[0][0] = p1;
84. pos[0][1] = p2 - dsp;
85. prev = now;
86. }
87. if(now == 4 && (prev != 4 && prev != 1))
88. { pos[0][0] = p1 + dsp;
89. pos[0][1] = p2;
90. prev = now;
91. }
92. if(now == 2 && (prev != 8 && prev != 2))
93. { pos[0][0] = p1;
94. pos[0][1] = p2 + dsp;
95. prev = now;
96. }
97. if(now == 1 && (prev != 1 && prev != 4))
98. {pos[0][0] = p1 - dsp;
99. pos[0][1] = p2;
100. prev = now;
101. }
102. }
103. }
104. void Snake::gnrtUnCond()
105. {
106. if( prev == 8 )
107. { pos[0][0] = p1;
108. pos[0][1] = p2 - dsp;
109. }
110. if( prev == 4 )
111. {pos[0][0] = p1 + dsp;
112. pos[0][1] = p2;
113. }
114. if( prev == 2 )
115. { pos[0][0] = p1;
116. pos[0][1] = p2 + dsp;
117. }
118. if( prev == 1 )
119. {pos[0][0] = p1 - dsp;
120. pos[0][1] = p2;
121. }
122. p1 = pos[0][0];
123. p2 = pos[0][1];
124. }
125. void Snake::check()
126. {
127. if(p1 > endX)
128. {p1 = strtX;}
129. else if(p1 < strtX)
130. { p1 = endX;}
131. if(p2 > endY)
132. { p2 = strtY;}
133. else if(p2 < strtY)
134. { p2 = endY;}
135. pos[0][0] = p1;
136. pos[0][1] = p2;
137. for(int i = 1;i < n;i++)
138. { if(p1 == pos[i][0] && p2 == pos[i][1])
139. { caught();
140. break;
141. }
142. }
143. }
144. void Snake::show()
145. {
146. int x = getcolor();
147. if(egGen != 1)
148. {
149. setcolor(getbkcolor());
150. setfillstyle(1,getbkcolor());
151. fillellipse(v1,v2,yr,yr);
152. }
153. else
154. egGen = 0;
155. if(egGen == 2)
156. egGen--;
157. setcolor(colr);
158. setfillstyle(1,9);
159. if(now == 8 || now == 2)
160. fillellipse(pos[0][0],pos[0][1],xr,yr);
161. else if(now == 4 || now == 1)
162. fillellipse(pos[0][0],pos[0][1],yr,xr);
163. setcolor(x);
164. }
165. void Snake::transpose()
166. { int i,j,x,y;
167. p1 = pos[0][0];
168. p2 = pos[0][1];
169. if(!egGen){
170. v1 = pos[n-1][0];
171. v2 = pos[n-1][1];
172. }
173. else
174. egGen = 0;
175. for(i = n-1;i >= 1;i--)
176. {pos[i][0] = pos[i-1][0];
177. pos[i][1] = pos[i-1][1];
178. }
179. }
180. void Snake::move()
181. { int st = 0;
182. do{
183. if(!kbhit())
184. { checkEgg();
185. if(!st)
186. show();
187. else
188. st = 0;
189. delay(dly/4);
190. transpose();
191. delay(dly/4);
192. gnrtUnCond();
193. delay(dly/4);
194. check();
195. delay(dly/4);
196. }
197. else if(stp){
198. chngDir();
199. gnrtCond();
200. check();
201. show();
202. st = 1;
203. }
204. } while(stp);
205. }
206. void Snake::init()
207. {time_t tm;
208. srand(time(&tm));
209. dsp = 20;
210. n = 5;
211. prev = 4;
212. for(int i = 4;i >= 0;i--)
213. { pos[i][0] = 201 + (n - i - 1) * dsp;
214. pos[i][1] = 301;
215. }
216. strtX = 21;
217. strtY = 21;
218. endX = 481;
219. endY = 361;
220. colr = 14;
221. now = prev;
222. dsp = 20;
223. stp = 111;
224. cnt = -1;
225. scr = 0;
226. dly = 150;
227. xr = 3;
228. yr = 9;
229. egg();
230. egGen = 1;
231. score();
232. int x = getcolor();
233. setlinestyle(0,1,3);
234. setcolor(15);
235. rectangle(strtX-15,strtY-15,endX+15,endY+15);
236. rectangle(endX+25,strtY-15,getmaxx()-15,endY+15);
237. rectangle(strtX-15,endY+25,getmaxx()-15,getmaxy()-5);
238. line(endX+25,strtY+75,getmaxx()-15,strtY+75);
239. line(endX+25,strtY+200,getmaxx()-15,strtY+200);
240. line(endX+25,strtY+275,getmaxx()-15,strtY+275);
241. setlinestyle(0,1,1);
242. settextstyle(8,0,1);
243. setcolor(11);
244. outtextxy(514,40,"SCORE");
245. setcolor(14);
246. settextstyle(11,0,5);
247. outtextxy(524,110," CONTROLS ");
248. outtextxy(522,135,"p = PAUSE");
249. outtextxy(522,155,"g = RESUME");
250. outtextxy(522,175,"e = EXIT");
251. outtextxy(513,195,"ARROWS");
252. outtextxy(512,205," -MOVEMENT");
253. setcolor(14);
254. settextstyle(4,0,9);
255. outtextxy(getmaxx()-500,getmaxy()-110,"SNAKE");
256. settextstyle(8,0,1);
257. setcolor(x);
258. }
259. void Snake::caught()
260. {
261. stp = 0;
262. sndCgt();
263. for(int i=0;i<=7;i++)
264. { if(i%2)
265. { setcolor(10);
266. outtextxy(512,250,"GAME OVER");
267. delay(900);
268. }
269. else
270. {setcolor(0);
271. outtextxy(512,250,"GAME OVER");
272. delay(500);
273. }
274. }
275. sleep(1);
276. }
277. void Snake::chngDir()
278. { int clr;
279. fillsettingstype *p;
280. char x = getch();
281. if(x == 72)
282. now = 8;
283. else if(x == 77)
284. now = 4;
285. else if(x == 80)
286. now = 2;
287. else if(x == 75)
288. now = 1;
289. else if(x == 'e')
290. caught();
291. else if(x == 'p')
292. { //int y = getcolor();
293. int twnkl = 1;
294. settextstyle(11,0,9);
295. while(1)
296. {if(kbhit())
297. { int c = getch();
298. if(c == 'g')
299. { clr = getcolor();
300. setcolor(0);
301. rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
302. outtextxy(endX+60,endY-29,"PAUSE");
303. break;
304. }
305. }
306. else
307. { if(twnkl%2)
308. { clr = getcolor();
309. setcolor(10);
310. rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
311. outtextxy(endX+60,endY-29,"PAUSE");
312. setcolor(clr);
313. delay(1000);
314. }
315. else
316. {
317. clr = getcolor();
318. setcolor(0);
319. rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
320. outtextxy(endX+60,endY-29,"PAUSE");
321. delay(1000);
322. }
323. }
324. twnkl++;
325. }
326. settextstyle(8,0,1);
327. }
328. }
329. Snake::Snake(Snake *p)
330. {
331. *p=NULL;
332. }
333. void Snake::egg()
334. { do
335. { e1 = (rand() % 100) * dsp + strtX;
336. e2 = (rand() % 100) * dsp + strtY;
337. } while(test());
338. int x = getcolor();
339. setcolor(7);
340. setfillstyle(1,random(15)+1);
341. fillellipse(e1,e2,xr+2,xr+2);
342. setcolor(x);
343. egGen = 2;
344. }
345. int Snake::test()
346. {
347. for(int i=0;i<n;i++)
348. { if(e1 == pos[i][0] && e2 == pos[i][1])
349. break;
350. if(v1 == e1 && v2 == e2)
351. break;
352. if((e1 >= endX+1) || (e2 >= endY+1))
353. break;
354. }
355. if(i != n)
356. return 1;
357. else
358. return 0;
359. }
360. void main()
361. {
362. Snake snk;
363. int gd=DETECT,gm,i,j,k,x,y;
364. clrscr();
365. initgraph(&gd,&gm,"C:\\Turboc3\\bgi");
366. snk.init();
367. snk.move();
368. closegraph();
369. restorecrtmode();
370. }

@Darkoknight I wouldn’t recommend C++ necessarily especially if @GideonAppiah is a beginner, my teacher taught us how to code snake in Python 2 it was simple enough to understand as well.

Please, before sharing full solutions make sure this is not a school assignment.

@GideonAppiah is this snake game a school assignment or not? We need to know in order to help you in the right way.

okay sorry keep forgeting

Can you put that into a drop-down list? It kind of clogs up the page.
If you don’t know how to do it, this is how

<details>
<summary>what ever you want the drop-down to be named</summary>
Stuff that you want to say inside
</details>

The result would be this:

what ever you want the drop-down to be named Stuff that you want to say inside
Thank you!

Or you could use discourse’s one:

[details="Name of divider"]
Text in divider
[/details]

Becomes:

Name of divider

Text in divider

Yeah, that works too.

Hi @Darkoknight, like some other people have said, ensure that this is not for a school assignment to avoid giving all of the answers away. I’d also put this in a dropdown bar and use the preformatted text button to make your reply a lot nicer to read.

It’s called BBCode, but I think we’re getting a bit off-topic.

I’ll edit the post to format it with three backticks as that takes up much less space.