Snake game using C++

Published on . Written by

Snake game using C++

We all have played the vintage snake game in our devices in our childhood. Accept it, the game may be basic but it was quite addicting and we all used to get so engrossed and played it to increase the tail until we hit our head with it and BOOM game over! If you have ever wondered how this game can be built then here is small support that I could provide.


Skyfi Labs Projects
Let me introduce you to the game if you are new to it. There is a snakehead, a snake tail, and a fruit in the game. You can move your snake up, down, left as well as right. And, as you swallow the fruit the tail size increases. If you collide with the tail or cross the bounds of the wall then the game is over. 

Read more..

SLNOTE
In this game development project, we are going to use C++ to create the game. You don’t need to have a vast knowledge of C++ or the OOP concepts to build this game. All you need to know is the basic language and the logic part very well. You can use a visual studio or any other IDE to start building.

Project Implementation

  1. To start with, build your code skeleton in such a way that it has four sections namely setup(), input(), draw(), and logic(). Also, declare a global variable GameOver and set it to false. 
  2. Create global variables for the map dimensions as well such as width, height, and set their values. Then, create variables for the head position of the snake and the fruit.
  3. Now let us come to creating our wall. In the draw function, clear the console window. Set the top and bottom walls in your code. 
  4. Printing a wall is just like printing a matrix, hence using loop into loop create a wall using the character ‘#’.
  5. Next, to print the snakehead and the fruit on the map use the draw function. You can use big O as the head of the snake and F as the fruit.

SLLATEST
  1. Let us come to the control part. Here you will build the control over your snake or track it with the keyboard keys.
  2. To achieve this, you need to use the kbhit () function for which you must necessarily include the conio.h library. This function returns a non-zero value if the key is pressed and zero if not. 
  3. Also, use getch() which will return the ASCII value of the character pressed on the keyboard.
  4. Coming to the logic part, add the logic that if you go left then the X coordinate of the map decreases. Similarly, if right then increase the x coordinate, if up then decrease the Y coordinate, and if down then increase the Y coordinate.
  5. Last but not the least, work on the increment of the snake’s tail. Print the tail as small o’s. Use two arrays, the first array will contain the x co-ordinates of the tail and the second will contain the Y coordinates. Now if the snakehead moves in any direction then the tail will follow therefore if the snake has four segments then the first will follow the head, second follows the first, third will follow the second and the fourth will follow the third. 
  6. Make a variable named sTail which will be used to vary the size of the tail and therefore if the snake swallows a fruit then increment the tail by one.
  7. If you want you can customize the game in your way, though the major part is all about the logic. 

SLDYK
Kit required to develop Snake game using C++:
Technologies you will learn by working on Snake game using C++:


Any Questions?


Subscribe for more project ideas