Introduction :
Snake game is a popular classic arcade game. Players of any ages enjoy this game. Building a game is the best way to learn programming. Snake game is an easy and addictive game which you can create with C or C++ graphics programming language.
For beginners, creating a snake game using the C or C++ programming language is a fun and challenging project. In this article, I shall explore how to create a simple snake game in C or C++ graphics programming language. Here, I build the snake game using functions of graphics.h library in Turbo C++ IDE.
About the program :
In this snake game program, you can move a snake (red circle) around the screen. On the game screen I give the instruction how you control the snake. In this program, “8” use for move the snake to upward, “2” use for move the snake to downward, “6” use to move the snake to the right, “4” use for move the snake to the left and “5” use for stopping the program.
Here, you have to eat the food (blue circle) which randomly creates on the game screen. When you eat the food, the snake (red circle) growing in size.
Logic for creating a snake game with C or C++ graphics programming language :
The logic for creating a snake game is very simple. First create a red color circle which called snake. Then blue circle is generated randomly anywhere on the game screen. When the snake and food come closer the food vanishes and create a new food. The whole process is in an infinite loop.
Explanation of the program :
Here, I discuss about the explanation of how to build snake game in C or C++ graphics programming language. First, you have to include graphics.h, stdlib.h, stdio.h, conio.h, dos.h library in your program. Then, initialize graphics mode using initgraph() function. Now, you can use setbkcolor() function to set the background of the graphics screen. The setcolor() function use to set the color of different shapes.
Using settextstyle() and outtextxy() function, you can display text on the screen. After that, setfillstyle() and fillellipse() functions use for creating the food of snake. Using getch() function, you can take input from the user. Now, you can create snake using setfillstyle(), circle() and floodfill() functions. Next, using delay() function, you can control the speed of the snake.
The kbhit() function use to check that user presses any key or not. Now, you can use rand() function to create food randomly. Lastly, you have to use closegraph() function for close the program.
How run the program :
To run the snake game, you have to install Turbo C++ on your system. In this program, I use the graphics.h library which is included in the Turbo C++. Then, open Turbo C++ and create a C or C++ file in it. Now, copy the below source code and paste in your C or C++ file in Turbo C++. Then, save the file and compile the code. When you run the program,you will be seeing the snake game on your PC. Do you know how use graphics.h in Turbo C++.
Source code of the program :
You can copy the below source code for creating a snake game using the C or C++ graphics programming language.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
int main(void)
{
int xmax, ymax, x, y, k = 100, l = 100, p, m, a = 0;
char c;
int i = 0, j = 0;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
xmax = getmaxx();
ymax = getmaxy();
x = xmax / 2;
y = ymax / 2;
setbkcolor(10);
setcolor(4);
while (1)
{
settextstyle(1, 0, 4);
outtextxy(x - 130, 0, "SNAKE GAME");
outtextxy(0, y + y - 50, "PUSKAR JASU");
outtextxy(x + 130, 0, "HELP");
settextstyle(1, 0, 3);
outtextxy(x + 130, 30, "8 FOR UP");
outtextxy(x + 130, 50, "2 FOR DOWN");
outtextxy(x + 130, 70, "6 FOR RIGHT");
outtextxy(x + 130, 90, "4 FOR LEFT");
outtextxy(x + 130, 110, "5 FOR STOP");
setfillstyle(SOLID_FILL, 9);
fillellipse(k, l, 5, 5);
c = getch();
if (c == '8')
{
while (1)
{
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
delay(10);
setfillstyle(EMPTY_FILL, 0);
circle(i, j + 1, 8 + a);
floodfill(i, j + 1, 0);
if (j == 0)
j = ymax;
j--;
if (kbhit())
break;
}
}
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
if (c == '2')
{
while (1)
{
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
delay(10);
setfillstyle(EMPTY_FILL, 0);
circle(i, j - 1, 8 + a);
floodfill(i, j - 1, 0);
if (j == ymax)
j = 0;
j++;
if (kbhit())
break;
}
}
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
if (c == '4')
{
while (1)
{
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
delay(10);
setfillstyle(EMPTY_FILL, 0);
circle(i + 1, j, 8 + a);
floodfill(i + 1, j, 0);
if (i == 0)
i = xmax;
i--;
if (kbhit())
break;
}
}
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
if (c == '6')
{
while (1)
{
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
delay(10);
setfillstyle(EMPTY_FILL, 0);
circle(i - 1, j, 8 + a);
floodfill(i - 1, j, 0);
if (i == xmax)
i = 0;
i++;
if (kbhit())
break;
}
}
setfillstyle(SOLID_FILL, 4);
circle(i, j, 8 + a);
floodfill(i, j, 4);
if (c == '5')
break;
if (i >= k - 5 && j >= l - 5)
{
k = rand() % xmax;
l = rand() % ymax;
a++;
}
}
closegraph();
return 0;
}
Output :
You can also see the output of the snake game by C or C++ graphics programming language in my YouTube channel as shown in below.
Conclusion :
After follow this article, I think you have learned how to create snake game with C or C++ programming language which can play on your computer. By following this article, you can create a basic snake game with simple graphics and game logic.
You can make the game more challenging and engaging by adding more features like display scores, sound effects, multiplayer functionality, different levels of difficulty etc. You can create more complex games with the help of my source code. Thank you for visiting my site.