Snake Game Using C++







"Snake Game Using C++" is a modified version of "Snake Game" which was developed using C programming platform. Unfortunately, I lost the file containing the source code. However, you can download and run the project directly. For file, chick here. For the demo of that project click here.


The concepts used in this project, somehow, is similar to the old project. Here, I have included a loading animation, menu for selecting the avatar and speed of the snake. Unfortunately, this project is also not a complete one. Adding levels can be a fruitful achievement for this project. As soon as I update this project, I'll surely add the source code. Most importantly, The orientation of this game has been set in portrait as I made this project for YouTube short category. I haven't encapsulated many function which is a very bad practice for object oriented programming. To know about the logic used in this project, scroll down.


How to download code for "Snake Game Using C++" ?

  • Click here for source code.
  • New window will open where there is a source code of this project. Download all the files.
  • Remember that this project runs in Dev C++.


Demo of this game :



For more source code and tutorial, SUBSCRBE to my YouTube channel. 


Development platform

Computer programming language has been used to develop this project.

 

Compiler

Dev C++ has been used as compiler in this project whose version is 5.11.


Header files used

The header files used in this project are as follows :

  1. Graphics
  2. Iostream
  3. Windows


Library functions

The following library functions has been used :

  1. Under graphics header file
    1. Arc
    2. Bar
    3. Circle
    4. Closegraph
    5. Floodfill
    6. Initwindow
    7. Outtextxy
    8. Rectangle
    9. Setcolor
    10. Setfillstyle
    11. Setlinestyle
    12. Settextstyle
  2. Under iostream header file
    1. Strcpy
  3. Under windows header file
    1. Delay
    2. GetAsyncKeyState

 

Concepts used

Topics/ concepts of this language that you need to know for making this type of game :

  1. Array
  2. For looping statement
  3. Graphics
  4. If else conditional statement
  5. Do while looping statement
  6. Switch case


Logic description

First of all, for loading effect, I made a bar with variable assigned as argument. Using the for loop, I increased the horizontal (x-coordinate) of the bar. 


When a menu appears, user/ player can select among the options. To track which key has been pressed, GetAsyncKeyState function has been used even for changing the direction of the snake. Before heading for the main concept, let's talk about changing the avatar of a snake. It is simply done by changing the bar size i.e. x-coordinate of the bar. 


For changing the speed of gameplay/ snake, simple, delay function has been used. The lesser the value of parameter for delay function, the faster becomes the gameplay.


While moving the snake, here, I have increased the respective parameter of bar by 20. Decreasing the parameter value, we can get smoother effect of movement.


When the head of the snake touches the food (i.e. each coordinate of snake head and food matches), using rand function a new coordinate is given for the food, and the size of snake also increases too.


Now, the main concept which is about how to increase the size of snake. The two arrays each of size 50 has been declared where each index is provided the value of (-20) except for head. The coordinate for head is (60,60). You might be thinking why negative value. Well, providing negative value and plotting the bar using those values makes the bar invisible to the game screen as they as located outside the screen.  


When the loop commences, based on the direction of snake, the bar (head moves). When the head touches the food, the value of length-variable increases by 1. And, the coordinate of smaller index of both arrays gets passed to consecutive higher index until the loop reaches the size of length.

No comments:

Post a Comment

Article Ad1