Snake Game in C++ using Graphics

In this Article, I will share code about creating a Snake game in C++ using Graphics. I have used Dev C++ and Code Blocks compiler for testing this code. If you want to know how to setup code blocks for graphics watch this video

Description of Snake Game in C++

In this snake game in c++ using graphics code;

  • I have declared nessacery header files
  • then initialized graphic screen, and used rand function to generate food at random locations.
  • After that I have declared some variables for boundry, food generation and food counter.
  • After that I have drawn boundry using Rectangle Function
  • then Main logic Inside loop, loop condition is untill user eats 10 foods or touches the rectangle boundry. 10 Foods is winning Condition and touch boundry is Losing Condition.
  • Inside loop, I am printing the Buttons (A – Up, Z – Right, dot . – bottom, L – Left), User have to use these buttons to move his eating ball left right.
  • Food is displayed with white circle, and user is displayed with green circle.
  • Everytime User makes a move, I have to check if both circles overlap, it means user eats food, in that case remove the existing food, create new food at new location, and increment in food variable.

Code

//Snake game :)
#include<iostream>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<ctime>
using namespace std;
int main()
{
    srand(time(0));
    int gdriver=DETECT,gmode;
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
    cleardevice();
    //x and y are for the circle
    int x=300,y=250;
    //x1 and y1 are fo generating foot at random place
     int x1=200,y1=230,food=0;
  //   key is used to take input from KEYBOARD
    char key;
   // a boundry line
    rectangle(100,100,500,400);
   // if x or y exceeds the boundry limit game is out
    while(x>100 && x<500 && y>100 && y<400 && food<10)
    {
        //these are for generating food at randm pleaces..
       // these 4 conditions are if the circle that is in your ccontrol reaches the food then
       // generate new foot at new place within the boundry
        if(x+1==x1 && y+1==y1 || x-1==x1 && y+1==y1 || x+1==x1 && y-1==y1 || x-1==x1 && y-1==y1 || x==x1 && x==y1)
        {
          //  food counts number of dots accessed
            food++;
            againx:
            x1=rand();
         //   these conditions are for generating food within the boundry
            if(x1<100 || x1>500)
                goto againx;
                againy:
            y1=rand();
            if(y1<100 || y1>400)
                goto againy;
           // this draws a dot for food
            circle(x1,y1,3);
        }
      //  KEYS INFORMATION
        key=getch();
        if(key=='A' || key=='a')//move up
            y--;
        else if(key=='Z'||key=='z')//move right
            x++;
        else if(key=='L' || key=='l')//for left
            x--;
        else if(key=='.')//for downward
            y++;
        cleardevice();
        outtextxy(100,0,"SNAKE GAME BY JAVERIA");

        outtextxy(100,20,"Z to move right");
        outtextxy(100,40,"L to move left");
        outtextxy(100,60,". to move down");
         outtextxy(100,80,"A to move up");
       // boundry line and the circle
         rectangle(100,100,500,400);
         circle(x1,y1,3);
        // you can control the circle of green color to reach to food
         setcolor(2);

        circle(x,y,4);
        setcolor(WHITE);

    }
    if(food==10)
    {
        outtextxy(100,420,"You Won" );

    }
    outtextxy(100,420,"You are out");
    cout<<"\n\n\n\tYOUR TOTAL SCORES ARE "<<food<<endl<<endl;
//conditions so that the game exit when you press space
  char exitt;
  again:
    exitt=getch();
    if(exitt==32)
         closegraph();
    else
        goto again;

return 0;

}

Output

Snake game in c++ using graphics

If you want to learn about creating path travelling game in C++ using Graphics, Read this Post: Path Travelling Game in C++ using Graphics – Code Seekers (code-seekers.com)

1 thought on “Snake Game in C++ using Graphics”

Leave a Comment