Monday, June 20, 2011

Mini project snake game in c

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:

126 comments:

  1. "windows.h header file" is missing in my c software and c software is emulated c++ which is compatible with window7
    any one could please help me to solve this problem
    Thank you in advance

    ReplyDelete
  2. Sorry 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

    ReplyDelete
  3. i got 4 errors and 1 warning.i m using C Free5 to run .
    errors 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)

    ReplyDelete
    Replies
    1. use Code::Block... better compiler

      Delete
    2. I 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.

      https://www.facebook.com/valcescu.eduard.3

      Delete
    3. Use Code Blocks is Best Compiler Ever

      Delete
  4. This program is compile in Code::Blocks IDE with MinGw I don't know about
    C Free5 so compile it in codeblocks if you don't have then download it form codeblocks.org

    ReplyDelete
  5. 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...

    ReplyDelete
    Replies
    1. yes....without data structure nothin can be formed into form.....itx before main....check it....

      Delete
    2. Are you a fucking retard?? "formed into form"???

      Delete
  6. thanks bro... it's working in codeblocks latest version.

    ReplyDelete
  7. any 1 help me plzzz....i got 10 errors nd 5 warnings..

    ReplyDelete
    Replies
    1. plz specify which compiler you use

      Delete
    2. Hello boss...can u give algorithm for this project..

      Delete
  8. i got 17 error and 5 warning
    using turbo c++ 4.5

    ReplyDelete
  9. i use codeblocks but it have 4 errors , help me soon, thanks

    ReplyDelete
  10. conio.h is not a standard C library. That's the reason it triggers errors.

    ReplyDelete
    Replies
    1. so how can we replace this library?

      Delete
  11. yr i got more than 17erros nd 8warnings

    ReplyDelete
  12. Works perfectly for me in code blocks

    ReplyDelete
  13. I get this error compiling with COdeBLocks

    x86)\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

    ReplyDelete
    Replies
    1. EDIT : I solve the problem. IT WORKS GREAT.

      THANKS !!

      Delete
  14. Thanks! Had some errors but i fixed them!

    ReplyDelete
  15. what is the coding for loading?

    ReplyDelete
  16. what is the use of COORD coord ???

    ReplyDelete
    Replies
    1. COORD 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.

      Delete
    2. How can i change the speed and the word " F " which is for food .

      Delete
  17. Can any body explain this whole code line by line please .. -thanks in advance

    ReplyDelete
    Replies
    1. Who have the time to explain the code line by line. Just start learning from int main() function and step by step each function.

      Delete
  18. hi 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..

    ReplyDelete
  19. Really great share! Thanks:) Compiled in Dev C++ and it works grand.

    ReplyDelete
  20. |206|error: conflicting types for 'Food'|
    |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?

    ReplyDelete
  21. write void in line 43 before Print and it should work

    ReplyDelete
  22. Error-1 error C3861: 'Food': identifier not found

    Error-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

    ReplyDelete
  23. Error 1 error C3861: 'Print': identifier not found c:\users\dell\documents\visual studio 2010\projects\snack\snack\snack.cpp 43 1 snack
    Error 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

    ReplyDelete
  24. Can I have a coding on TYPING TUTOR please?

    ReplyDelete
  25. To fix the problem

    Error-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.

    ReplyDelete
    Replies
    1. is not well explanatory please kindly explain the source code.

      Delete
  26. For those getting errors for food and print function, simply give a function prototype at the top where other function prototypes have been placed .....



    ReplyDelete
  27. pls help us....
    our 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'

    ReplyDelete
  28. hi....can u give algorithm for this project

    ReplyDelete
  29. It's very big code. Thanks for sharing.

    ReplyDelete
  30. i have got the output but i wasunable to play. The program quits immediately after pressing a key.

    ReplyDelete
  31. i use c-free 4.0 and it displays these errors
    [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)

    ReplyDelete
  32. i m making program in embed c.same as this progrm.so help me.if any one knw something.pls mail me
    manindersingh640@gmail.com

    ReplyDelete
  33. Use microsoft visual c++ 6.0 and run it it should support

    ReplyDelete
  34. some 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.....

    ReplyDelete
  35. You 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......

    ReplyDelete
  36. Compiled 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...

    ReplyDelete
  37. These are the error i get after building this program. Can anybody help me solve them? Thanks!
    Compiling: 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

    ReplyDelete
  38. could u plase tolde me how 2 make the snake move sloly how i can control of its movement ?? ^_^

    ReplyDelete
  39. hey can we use tc for this

    ReplyDelete
  40. hey i want celebration animation project in computer graphics please help me

    ReplyDelete
  41. How to playing what controls??

    ReplyDelete
  42. the 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++

    ReplyDelete
  43. the 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++.

    please anyone help me out

    ReplyDelete
  44. Good Job, its half baked actually. But its a good game :)

    ReplyDelete
  45. @ umang just click on enter button

    ReplyDelete
  46. this 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 !

    ReplyDelete
  47. Mini project snake game is really good. thanks for sharing.
    backup software for windows free

    ReplyDelete
  48. Mini project snake game is really awesome and very useful.
    full version of windows

    ReplyDelete
  49. i phone 6 plus by only 7500 taka master king copy
    for details : 01711952849

    ReplyDelete
  50. I didn't understood please help me

    ReplyDelete
  51. Thanks a lot for sharing about Mini project, it's a very useful post.
    Software Download

    ReplyDelete
  52. Thanks 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". :)

    ReplyDelete
  53. this has a trojan horse in the code

    ReplyDelete
  54. Wow amazing blog.I always love to read these kind of blogs.
    adobe after effects product key
    Free Download

    ReplyDelete
  55. can anyone post code without error

    ReplyDelete
  56. 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?

    ReplyDelete
  57. works for me !!
    on dev c++ an C::B

    ReplyDelete
  58. Really like the code. I hope you are still active. I had a few doubts like what is
    1)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.

    ReplyDelete
  59. I 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.

    ReplyDelete
  60. Error: '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)

    ReplyDelete
  61. hello 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....

    ReplyDelete
  62. i am getting a error which says drawing opration attempted when there was no window . how can i solve this error.

    ReplyDelete
  63. owsam programing,thanks alot for this ,,

    ReplyDelete
  64. IT IS NOT WORKING WHEN I SRART THE GAME I AM DIEING !!!!!!!!!!!!!!!!!!! :( :(:(:(:(:(:(:(:(:(:(:(:(:(:(

    ReplyDelete
  65. IT IS NOT WORKING WHEN I SRART THE GAME I AM DIEING !!!!!!!!!!!!!!!!!!! :( :(:(:(:(:(:(:(:(:(:(:(:(:(:(

    ReplyDelete
  66. Int bend_no
    What do you used for?

    ReplyDelete
  67. int len; why you use it ?!(for example:(len<legth))
    Anyone can help me !

    ReplyDelete
  68. What is the use of this part of code...


    len=0;

    for(i=0;i<30;i++)

    {

    body[i].x=0;

    body[i].y=0;

    if(i==length)

    break;

    ReplyDelete
  69. can we use this code in turbo c++

    ReplyDelete
  70. lot of errors and also windows.h not working in turbo0 .......plz help

    ReplyDelete
  71. this code is perfect but unfortunatly i want c++ code of snake game . any one can help me in this reguard?

    ReplyDelete
  72. Thank you so much.... It's working

    ReplyDelete
  73. guyss run this program in Dev-C++ compiler ...

    ReplyDelete
  74. does this work in turbo c

    ReplyDelete
  75. I am beginner anyone help to enter world of codes
    I only know somethings on console in c++

    ReplyDelete
  76. I got some errors in the above code so i used the below given :
    http://www.infoopoint.com/snake-game-using-c-complete-code/

    ReplyDelete
  77. i 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?

    ReplyDelete
  78. this is good, work very well, this site good for reference, thanks dude...

    ReplyDelete
  79. What is the algorithm

    ReplyDelete
  80. Can any one tell me the algorithm and some data structures used in this code

    ReplyDelete
  81. hey bro i also got 17 errors and 8 warning using c++ 4.0

    ReplyDelete