Javatpoint Logo
Javatpoint Logo

Snake Code in C++

In this article, we will create a snake game with the help of C++ and graphics functions. In this, we will use the concept of c++ classes and computer graphics functions.

What do you mean by the Snake game?

Snake game is one of the most famous games available on all types of device and works on every operating system. Snakes can move in every direction in this game, e.g., left, right, up, and down; after taking the food, the length of the snake increases. Food of the Snake will be generated at a given interval of time.

What do you mean by C++?

C++ is an object-oriented programming language. It is also called c++ with classes. It is a cross-platform language that can be used to create high-level applications. It gives programmers a high level of control over system resources and memory.

What do you mean by Computer Graphics?

The term computer graphics is the information displayed on a visual display unit or a computer printout in diagrams, graphics, pictures, and symbols.

Graphics primitives in C++

A graphics primitive is an essential non divisible graphical element for input or output within a computer graphics system. We need a header file called <graphics.h> to draw and create any graphics on the screen. It can also be defined as how a computer displays the data pictorially and manipulates it. Apart from drawing figures of various shapes, all animations and multimedia mainly work in the graphics platform.

The following functions are used to create graphics in the snake game:

Initgraph():

To initialize the graphics function, we must use the initgraph () function.

Syntax:

void Intergraph(int *graph driver, int *graph mode, char *path);

Initgraph function uses three-parameter:

  • gd: It is used for the graphics driver.
  • gm: It is used for the graphics mode.
  • path: It specifies the path where the graphic file is located.

closegraph():

It is used to close the graphics function.

Syntax:

void closegraph();

Outputting text:

In C graphics, text can be outputted using the function outtext() and outtextxy().

outtext():

It is used to display the text at the current position.

Syntax:

void outtext(char *str);

outtextxy():

It is used to display the text at the specified position.

Syntax:

void outtextxy(int x, int y, char *str);

Let's take the example of the snake game in C++.

Example 1:

Explanation:

In the above example, we have implemented a basic Snake Game. In this, we have used the concept of classes and some computer graphics functions.

The basic functionalities of this game are given below.

  • The Snake is created with the help of a graphics function.
  • The fruit of the Snake is generated by the rand() function of computer graphics.
  • The Snake can be moved in any direction with the help of the keyboard ( Right, up, and down keys).
  • When the Snake eats a fruit, the score will increase by 14 points.
  • In this game, we can create basic controls:
    • p control is used to pause the game. We can click a p character from the keyword to pause the game.
    • g control is used to resume the game. We can click a g character from the keyword to resume the game.
    • e control is used to exit from the game. We can click an e character from the keyword to exit from the game.

Output:

Following is the output of this example:

Snake Code in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA