Mini project Snake game in C is console application created without graphic. We draw game window, and snake only using Asterix character and zero character to draw food. User can play game using up, left, right, and down arrow key. The score is calculated based on the number of foods eaten by snake. The main objective of this project is to provide knowledge and technique about creating snake game in C programming. User can use any graphics library to draw window, snake and food to make it similar to commercial games. However, the code base and algorithm of the game is same in all snake game available in market. I have separated game drawing source code and snake game paly source code so that you can use any graphics library Easily and quickly.
In this article, I will explain the following items:
- Flowchart of the game
- Data Structure and Algorithm
- Techniques to move snake direction
- Calculating snake next coordinate
- Collision detection
- Techniques to display moving snake and game play
Also, Download the final version of game Download Project from Github.
I have explained the game in following two articles:
"windows.h header file" is missing in my c software and c software is emulated c++ which is compatible with window7
ReplyDeleteany one could please help me to solve this problem
Thank you in advance
no u cant do it..........lol
Deleteu r a fucking asshole
Deletemeow.
Deletewoof
DeleteWtf! Amazing!!!!
Deletenice..i'm trying using code block..
DeleteUse Dev c++
DeleteMadarChoodh saala
DeleteSorry I am can't understand which compiler you use this code run only in gcc compiler.I think you are using turbo c then don't use it is old download codeblocks which support latest standard c language
ReplyDeletei got 4 errors and 1 warning.i m using C Free5 to run .
ReplyDeleteerrors are:
--------------------Configuration: mingw5 - CUI Debug, Builder Type: MinGW--------------------
Checking file dependency...
Compiling C:\Users\RAJEEVRAMAN\Documents\C-Free\Projects\snake game\main.c...
[Error] C:\Users\RAJEEVRAMAN\Documents\C-Free\Projects\snake game\main.c:206: error: conflicting types for 'Food'
[Error] C:\Users\RAJEEVRAMAN\Documents\C-Free\Projects\snake game\main.c:51: error: previous implicit declaration of 'Food' was here
[Error] C:\Users\RAJEEVRAMAN\Documents\C-Free\Projects\snake game\main.c:364: error: conflicting types for 'Print'
[Error] C:\Users\RAJEEVRAMAN\Documents\C-Free\Projects\snake game\main.c:43: error: previous implicit declaration of 'Print' was here
[Warning] C:\Users\RAJEEVRAMAN\Documents\C-Free\Projects\snake game\main.c:454:2: warning: no newline at end of file
Complete Make main: 4 error(s), 1 warning(s)
use Code::Block... better compiler
DeleteI use Code:Blocks and it's better than C Free5 because Code:Blocks has more options like i don't know etc etc and it's a free compiler.I'm in the 7th class (sorry for my english) and my Computer teacher is impressed.My skills are so high,so i teach my collagues programming language.For me it's simple because i study a lot of programming language and i remember all the tricks of the programming language.If you want to ask me something about programming language i will leave the link of my facebook to talk.
Deletehttps://www.facebook.com/valcescu.eduard.3
Use Code Blocks is Best Compiler Ever
DeleteThis program is compile in Code::Blocks IDE with MinGw I don't know about
ReplyDeleteC Free5 so compile it in codeblocks if you don't have then download it form codeblocks.org
hey nice piece of code... but can any 1 plz tell me in this project do u use any data structure...if any plz tell me which one...
ReplyDeleteyes....without data structure nothin can be formed into form.....itx before main....check it....
DeleteAre you a fucking retard?? "formed into form"???
Deletethanks bro... it's working in codeblocks latest version.
ReplyDeleteplz help me bro...
Deleteany 1 help me plzzz....i got 10 errors nd 5 warnings..
ReplyDeleteplz specify which compiler you use
DeleteHello boss...can u give algorithm for this project..
Deletei got 17 error and 5 warning
ReplyDeleteusing turbo c++ 4.5
use code::blocks IDE with MinGW compiler.
Deletei use codeblocks but it have 4 errors , help me soon, thanks
ReplyDeleteshow me error message
Deleteconio.h is not a standard C library. That's the reason it triggers errors.
ReplyDeleteso how can we replace this library?
Deleteyr i got more than 17erros nd 8warnings
ReplyDeleteWorks perfectly for me in code blocks
ReplyDeleteReally fun :)
ReplyDeleteI get this error compiling with COdeBLocks
ReplyDeletex86)\CodeBlocks\ghd\Untitled2.cpp:43:11: error: 'Print' was not declared in this scope
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp:51:10: error: 'Food' was not declared in this scope
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp:42:10: warning: unused variable 'key' [-Wunused-variable]
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp: In function 'void Move()':
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp:62:14: error: 'Food' was not declared in this scope
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp: In function 'void load()':
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp:137:9: warning: unused variable 'row' [-Wunused-variable]
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp:137:13: warning: unused variable 'col' [-Wunused-variable]
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp:137:19: warning: unused variable 'c' [-Wunused-variable]
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp: In function 'void Delay(long double)':
C:\Program Files (x86)\CodeBlocks\ghd\Untitled2.cpp:169:11: error: 'Score' was not declared in this scope
Please help. Thanks
EDIT : I solve the problem. IT WORKS GREAT.
DeleteTHANKS !!
how to solve that problem?
ReplyDeleteThanks! Had some errors but i fixed them!
ReplyDeletewhat is the coding for loading?
ReplyDeletewhat is the use of COORD coord ???
ReplyDeleteCOORD coord is the structure which is used to move the cursor so that the snake move on the screen by changing cursor with the help of this.
DeleteHow can i change the speed and the word " F " which is for food .
DeleteCan any body explain this whole code line by line please .. -thanks in advance
ReplyDeleteWho have the time to explain the code line by line. Just start learning from int main() function and step by step each function.
Deletehi there,thanks to this coding..it can run perfectly..although it has a little bit of error,but after fix it,i can play the game..5star 4 u..
ReplyDeleteReally great share! Thanks:) Compiled in Dev C++ and it works grand.
ReplyDelete|206|error: conflicting types for 'Food'|
ReplyDelete|51|error: previous implicit declaration of 'Food' was here|
|364|error: conflicting types for 'Print'|
|43|error: previous implicit declaration of 'Print' was here|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 4 seconds) ===|
What's going on?
write void in line 43 before Print and it should work
ReplyDeleteError-1 error C3861: 'Food': identifier not found
ReplyDeleteError-2 error C3861: 'Food': identifier not found
Error-3 error C3861: 'Score': identifier not found
Warning-4 warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
Error 1 error C3861: 'Print': identifier not found c:\users\dell\documents\visual studio 2010\projects\snack\snack\snack.cpp 43 1 snack
ReplyDeleteError 2 error C3861: 'Food': identifier not found c:\users\dell\documents\visual studio 2010\projects\snack\snack\snack.cpp 51 1 snack
Error 3 error C3861: 'Food': identifier not found c:\users\dell\documents\visual studio 2010\projects\snack\snack\snack.cpp 62 1 snack
Error 4 error C3861: 'Score': identifier not found c:\users\dell\documents\visual studio 2010\projects\snack\snack\snack.cpp 169 1 snack
Warning 5 warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data c:\users\dell\documents\visual studio 2010\projects\snack\snack\snack.cpp 212 1 snack
Can I have a coding on TYPING TUTOR please?
ReplyDeleteTo fix the problem
ReplyDeleteError-1 error C3861: 'Food': identifier not found
Error-2 error C3861: 'Food': identifier not found
Error-3 error C3861: 'Score': identifier not found
Add:
void Food();
void Print();
int Score();
at the top with the other declared functions this should fix the issue.
is not well explanatory please kindly explain the source code.
DeleteFor those getting errors for food and print function, simply give a function prototype at the top where other function prototypes have been placed .....
ReplyDeletepls help us....
ReplyDeleteour errors are:
[Error] D:\c\Untitled3.cpp:87:11: error: 'Print' was not declared in this scope
[Error] D:\c\Untitled3.cpp:103:10: error: 'Food' was not declared in this scope
[Error] D:\c\Untitled3.cpp:126:14: error: 'Food' was not declared in this scope
[Error] D:\c\Untitled3.cpp:344:11: error: 'Score' was not declared in this scope
[Error] D:\c\Untitled3.cpp:805:5: error: expected primary-expression before '<' token
[Error] D:\c\Untitled3.cpp:805:6: error: 'span' was not declared in this scope
[Error] D:\c\Untitled3.cpp:805:11: error: expected ';' before 'class'
[Error] D:\c\Untitled3.cpp:807:5: error: expected primary-expression before '<' token
[Error] D:\c\Untitled3.cpp:807:11: error: expected ';' before 'class'
[Error] D:\c\Untitled3.cpp:809:5: error: expected primary-expression before '<' token
[Error] D:\c\Untitled3.cpp:809:11: error: expected ';' before 'class'
hi....can u give algorithm for this project
ReplyDeleteIt's very big code. Thanks for sharing.
ReplyDeletei have got the output but i wasunable to play. The program quits immediately after pressing a key.
ReplyDeletei use c-free 4.0 and it displays these errors
ReplyDelete[Error] C:\Users\DAVLEX\Desktop\game.c:339: parse error before `long'
[Error] C:\Users\DAVLEX\Desktop\game.c:341: `i' undeclared (first use in this function)
[Error] C:\Users\DAVLEX\Desktop\game.c:341: (Each undeclared identifier is reported only once
[Error] C:\Users\DAVLEX\Desktop\game.c:341: for each function it appears in.)
[Warning] C:\Users\DAVLEX\Desktop\game.c:411: warning: type mismatch with previous implicit declaration
[Warning] C:\Users\DAVLEX\Desktop\game.c:123: warning: previous implicit declaration of `Food'
[Warning] C:\Users\DAVLEX\Desktop\game.c:411: warning: `Food' was previously implicitly declared to return `int'
[Error] C:\Users\DAVLEX\Desktop\game.c:419: parse error before `a'
[Error] C:\Users\DAVLEX\Desktop\game.c:421: `a' undeclared (first use in this function)
[Error] C:\Users\DAVLEX\Desktop\game.c:689: parse error before `int'
[Error] C:\Users\DAVLEX\Desktop\game.c:695: `i' undeclared (first use in this function)
[Warning] C:\Users\DAVLEX\Desktop\game.c:727: warning: type mismatch with previous implicit declaration
[Warning] C:\Users\DAVLEX\Desktop\game.c:85: warning: previous implicit declaration of `Print'
[Warning] C:\Users\DAVLEX\Desktop\game.c:727: warning: `Print' was previously implicitly declared to return `int'
[Error] C:\Users\DAVLEX\Desktop\game.c:792: parse error before `mytime'
[Error] C:\Users\DAVLEX\Desktop\game.c:794: `mytime' undeclared (first use in this function)
i m making program in embed c.same as this progrm.so help me.if any one knw something.pls mail me
ReplyDeletemanindersingh640@gmail.com
Use microsoft visual c++ 6.0 and run it it should support
ReplyDeletesome things to ask....first how did u figure it out to use coordinates.....like gotoxy(10,i).......how?? where they came from....? i hav become a progammer too n now headin towardx a project...but i need sum help..how should i begin with it?.......like you set the border..which i hav got it somehow.....
ReplyDeleteYou know ma project is entirely different n very less on internet...it is archery game project...and i m alone to work on it....otherwise itx a team work.....since that time i have been workin on it madly and testin and takin out each element to figure out what's a function's purpose is......till now i have successfully been able to figure out the frame of game, the loading function, and now finally the snake.........well...that's for sure that i will definitely get screw up and fucked up in developin another project.......but still i will do...and that's the thrill......... no one tells you how to get started on a project....just give you the code.....i finally have also solved this problem.....first download the already done project of somebody.....then try to figure out the functions if it is being developed by functions..each and every.....take out each function on a separate place and run it.....also collect all related elements from other functions which permit this function to run......do this with each one.....you will see what each function is doing......then take out the infomation that your own project needs......that's all....helping otherx is gud but dont know y it is not available on net....... buddy thankx for your code....its undoubtedly a helpful tool......
ReplyDeleteCompiled fine. After I got to a score of 26 the game starting freaking out and flashing fragmented parts of the snake on the screen. Strange...
ReplyDeleteThese are the error i get after building this program. Can anybody help me solve them? Thanks!
ReplyDeleteCompiling: main.cpp
C:\CPP_Programs\Minigame\Minigame\main.cpp: In function 'int main()':
C:\CPP_Programs\Minigame\Minigame\main.cpp:85: error: 'Print' was not declared in this scope
C:\CPP_Programs\Minigame\Minigame\main.cpp:101: error: 'Food' was not declared in this scope
C:\CPP_Programs\Minigame\Minigame\main.cpp:83: warning: unused variable 'key'
C:\CPP_Programs\Minigame\Minigame\main.cpp: In function 'void Move()':
C:\CPP_Programs\Minigame\Minigame\main.cpp:123: error: 'Food' was not declared in this scope
C:\CPP_Programs\Minigame\Minigame\main.cpp: In function 'void load()':
C:\CPP_Programs\Minigame\Minigame\main.cpp:273: warning: unused variable 'row'
C:\CPP_Programs\Minigame\Minigame\main.cpp:273: warning: unused variable 'col'
C:\CPP_Programs\Minigame\Minigame\main.cpp:273: warning: unused variable 'c'
C:\CPP_Programs\Minigame\Minigame\main.cpp: In function 'void Delay(long double)':
C:\CPP_Programs\Minigame\Minigame\main.cpp:337: error: 'Score' was not declared in this scope
Process terminated with status 1 (0 minutes, 1 seconds)
4 errors, 4 warnings
hai am gng to try today...
ReplyDeletecould u plase tolde me how 2 make the snake move sloly how i can control of its movement ?? ^_^
ReplyDeletehey can we use tc for this
ReplyDeletehey i want celebration animation project in computer graphics please help me
ReplyDeleteHow to playing what controls??
ReplyDeletethe loading status bar keeps on saying that it is loading,although it has loaded to its maximum width and when i press any key it tells that all your three lives are completed and two more things,please tell me how to start the game after loading status bar has completely loaded in dev c++
ReplyDeletethe loading status bar keeps on saying that it is loading,although it has loaded to its maximum width and when i press any key it tells that all your three lives are completed and two more things,please tell me how to start the game after loading status bar has completely loaded in dev c++.
ReplyDeleteplease anyone help me out
Good Job, its half baked actually. But its a good game :)
ReplyDelete@ umang just click on enter button
ReplyDeletethis is running properly in my pc but the problem is that the border keeps shaking!! how to stop this? please reply as soon as you can !
ReplyDeletenice program
ReplyDeletethats great!!
ReplyDeleteMini project snake game is really good. thanks for sharing.
ReplyDeletebackup software for windows free
thanks for sharing information for Mini project snake game.
ReplyDeletefree download latest pc software 2013
Mini project snake game is really awesome and very useful.
ReplyDeletefull version of windows
new updated software also latest updates.
ReplyDeletei phone 6 plus by only 7500 taka master king copy
ReplyDeletefor details : 01711952849
I didn't understood please help me
ReplyDeleteThanks a lot for sharing about Mini project, it's a very useful post.
ReplyDeleteSoftware Download
Very Good
ReplyDeleteThanks a lot for sharing all codes. I'm new in this stuff and more educational like these mini projects. I compiled in Dev-C++ 4.9.9.2 and works fine. But I need to figure out keep window on the screen after press "y". :)
ReplyDeletethis has a trojan horse in the code
ReplyDeleteguys use Dev C++
ReplyDeleteTry this one with no errors.
ReplyDeleteWavepad sound editor master's edition portable | Download Free Software
Wow amazing blog.I always love to read these kind of blogs.
ReplyDeleteadobe after effects product key
Free Download
Running Perfectly.
ReplyDeletefull version software | avast 2015 activation key
16 error
ReplyDeletecan anyone post code without error
ReplyDeletei am using turbo++
ReplyDeletenice post.very good information.
ReplyDeletedownload software for pc
I am getting windows.h related error since there is no windows.h header file in include folder. So, how can I obtain the same?
ReplyDeleteworks for me !!
ReplyDeleteon dev c++ an C::B
bnzxv dghvcb df
ReplyDeleteReally like the code. I hope you are still active. I had a few doubts like what is
ReplyDelete1)COORD coord?
2)bend_no?
3)gotoxy and GotoXY?
4) How is cursor mapped to the movement of the snake?
Any reply would be great help.
Hey there
ReplyDeleteI use codeblocks 10.05 and when I run this code there the notification appears- "Drawing operation was attempted when there was no current window"-What's the solution.
ReplyDeletenice work
ReplyDeleteError: 'Drawing operation was attempted when there was no current window' after displaying: 'Welcome to the mini Snake game.(press any key to continue)'. Code::Blocks 13.12 (Windows)
ReplyDeletehello sir.... i am shubham deepankar and m new in programming field....awesome coding sir.... really m fan of yours.... i copy that code and run in code blocks it works awesome.... i wants to learn something like that ... plzzz sir help mee....
ReplyDeletei am getting a error which says drawing opration attempted when there was no window . how can i solve this error.
ReplyDeleteHope to get good output!! :)
ReplyDeleteowsam programing,thanks alot for this ,,
ReplyDeleteglad to read this, great works..!!
ReplyDeleteMicrosoft Office 2007 Keygen Activation | Bitdefender Mobile Security Premium Key
IT IS NOT WORKING WHEN I SRART THE GAME I AM DIEING !!!!!!!!!!!!!!!!!!! :( :(:(:(:(:(:(:(:(:(:(:(:(:(:(
ReplyDeleteIT IS NOT WORKING WHEN I SRART THE GAME I AM DIEING !!!!!!!!!!!!!!!!!!! :( :(:(:(:(:(:(:(:(:(:(:(:(:(:(
ReplyDeleteInt bend_no
ReplyDeleteWhat do you used for?
int len; why you use it ?!(for example:(len<legth))
ReplyDeleteAnyone can help me !
What is the use of this part of code...
ReplyDeletelen=0;
for(i=0;i<30;i++)
{
body[i].x=0;
body[i].y=0;
if(i==length)
break;
can we use this code in turbo c++
ReplyDeletelot of errors and also windows.h not working in turbo0 .......plz help
ReplyDeletethis code is perfect but unfortunatly i want c++ code of snake game . any one can help me in this reguard?
ReplyDeleteThank you so much.... It's working
ReplyDeleteguyss run this program in Dev-C++ compiler ...
ReplyDeletedoes this work in turbo c
ReplyDeleteI am beginner anyone help to enter world of codes
ReplyDeleteI only know somethings on console in c++
I got some errors in the above code so i used the below given :
ReplyDeletehttp://www.infoopoint.com/snake-game-using-c-complete-code/
very well explained .. thanks
ReplyDeletei tried this program using code blocks and worked perfectly. But the problem is if i make any changes in program like in printf i change the statement it doesnt reflects in the output ,it gives output of the previous code not the changed one . And yes i saved program and executed . How is that possible?
ReplyDeletethis is good, work very well, this site good for reference, thanks dude...
ReplyDeleteWhat is the algorithm
ReplyDeletewhat is the algorithm
ReplyDeleteCan any one tell me the algorithm and some data structures used in this code
ReplyDeletehey bro i also got 17 errors and 8 warning using c++ 4.0
ReplyDelete