banner



How To Code Snake In C++

Serpent Lawmaking in C++

Snake is a popular game that can be played on near whatever device and runs on any operating system. In this game, snakes tin move in any direction, including left, right, up, and down; after consuming the food, the snake's length increases. Ophidian food will exist generated at a predetermined interval.

With the aid of C++ and graphics functions, I'll make a snake game. The concept of C++ classes and estimator graphics functions will be applied to this.

Estimator Graphics

Data displayed on drawings, graphs, photographs, and visual symbols or on a typewriter called computer graphics.

C++ Graphics Primitives

A graphics primitive is a non-divisible graphical chemical element used in a computer graphics organisation for input or output. To return and create any images on the screen, we'll need a header file called < graphics.h >. It can too be defined as the way a computer visualizes and manipulates data. All blitheness and multimedia piece of work best in a photo studio, without having to draw figures of diverse shapes.

In the snake game, the following activities are used to create graphics:

  • The initgraph () function is used to initialize the graphics function.
  • Syntax: void Intergraph(int *graph driver, int *graph fashion, char *path);
  • The graphics commuter is represented by gd.
  • The graphics mode is represented by gm.
  • path: This indicates the location of the graphic file.
  • closegraph(): Information technology'due south used to turn off the graphics feature.
  • Syntax: void closegraph ();
  • Outputting text: The functions outtext () and outtextxy in C graphics tin can exist used to output text().
  • outtext(): It'southward used to show the current location of the text.
  • Syntax: void outtext (char *str);
  • outtexttxy(): Information technology's used to show text in a certain location.
  • Syntax: void outtextxy (int x, int y, char *str);

Example of a code showing ophidian game in C++:

          #include<iostream.h>   #include<conio.h>   #include<graphics.h>   #include<dos.h>   #include<stdlib.h>   #include<stdio.h>   #include<time.h>   #include<string.h>   grade Ophidian   {    int p1,p2,v1,v2,v3, e1, e2,prev,at present,northward,colr,dsp,cnt,dly,one thousand;    int stp, egGen;    int xr, year;    void caught();    public:     long scr;    int strtX,strtY,endX,endY;    int pos[100][2];    void evidence();    void init();    void egg();    void transpose();    void gnrtCond();    void gnrtUnCond();    void check();    void checkegg();    void move();    void chngDir();    void sndet();    void sndCgt();    int test();    void score();    Snake();    Serpent(Ophidian*);    ~Snake();    };   Snake::Ophidian()    {    }   Snake::~Ophidian()   {   }   void Ophidian::checkegg()   {    if((e1 == p1) && (e2 == p2))    { sndet();     egg();     dly--;     score();     northward++;     }   }   void Snake::sndet()   { nosound();    sound(2500);    delay(2);    nosound();   }   void Snake::sndCgt()   { nosound();    for(int x=1000;10>0;x--)    { sound(x);     delay(one);     }    nosound();   }   void Serpent::score()   { char *p;     ltoa(scr,p,10);     settextstyle(8,0,i);     setcolor(0);     outtextxy(585,40,p);     if(egGen != 1){     scr = scr + dly / 10;     }     ltoa(scr,p,x);     setcolor(10);     outtextxy(585,forty,p);   }   void Snake::gnrtCond()   { if(north < 367)    { if(now == 8 && (prev != viii && prev != 2))     { pos[0][0] = p1;      pos[0][i] = p2 - dsp;      prev = now;      }     if(now == four && (prev != 4 && prev != 1))     { pos[0][0] = p1 + dsp;      pos[0][1] = p2;      prev = now;      }     if(now == 2 && (prev != eight && prev != 2))     { pos[0][0] = p1;      pos[0][one] = p2 + dsp;      prev = at present;      }     if(now == 1 && (prev != one && prev != 4))     {pos[0][0] = p1 - dsp;      pos[0][one] = p2;      prev = now;      }   }   }   void Snake::gnrtUnCond()   {     if( prev == 8 )     { pos[0][0] = p1;      pos[0][one] = p2 - dsp;      }    if( prev == four )     {pos[0][0] = p1 + dsp;      pos[0][ane] = p2;      }    if( prev == 2 )     { pos[0][0] = p1;      pos[0][one] = p2 + dsp;      }    if( prev == i )     {pos[0][0] = p1 - dsp;      pos[0][1] = p2;      }    p1 = pos[0][0];    p2 = pos[0][1];   }   void Snake::bank check()   {    if(p1 > endX)     {p1 = strtX;}    else if(p1 < strtX)     { p1 = endX;}    if(p2 > endY)     { p2 = strtY;}    else if(p2 < strtY)     { p2 = endY;}    pos[0][0] = p1;    pos[0][one] = p2;   for(int i = 1;i < n;i++)     { if(p1 == pos[i][0] && p2 == pos[i][1])     { caught();      intermission;     }    }   }   void Snake::show()   {     int x = getcolor();     if(egGen != ane)     {     setcolor(getbkcolor());     setfillstyle(1,getbkcolor());     fillellipse(v1,v2,yr,yr);      }     else      egGen = 0;     if (egGen == 2)      egGen--;     setcolor(colr);     setfillstyle(1,9);     if(now == 8 || now == 2)      fillellipse(pos[0][0],pos[0][one],xr,yr);     else if(at present == 4 || at present == 1)      fillellipse(pos[0][0],pos[0][1],yr,xr);     setcolor(x);   }   void Snake::transpose()   { int i,j,x,y;      p1 = pos[0][0];      p2 = pos[0][1];      if(!egGen){      v1 = pos[n-1][0];      v2 = pos[n-1][1];      }      else       egGen = 0;      for(i = n-1;i >= i;i--)      {pos[i][0] = pos[i-1][0];       pos[i][1] = pos[i-1][1];      }   }   void Ophidian::move()   { int st = 0;     do{      if(!kbhit())      { checkegg();       if(!st)        bear witness();       else        st = 0;       delay(dly/4);       transpose();       delay(dly/4);       gnrtUnCond();       delay(dly/4);       bank check();      filibuster(dly/4);       }      else if(stp){      chngDir();      gnrtCond();      check();      testify();      st = 1;      }      } while(stp);   }   void Snake::init()   {time_t tm;    srand(time(&tm));    dsp = xx;    n = 5;    prev = 4;    for(int i = 4;i >= 0;i--)    { pos[i][0] = 201 + (n - i - one) * dsp;     pos[i][i] = 301;     }     strtX = 21;     strtY = 21;     endX = 481;     endY = 361;     colr = fourteen;     now = prev;     dsp = 20;     stp = 111;                  

OUTPUT:

Snake Code in C++

Source: https://www.tutorialandexample.com/snake-code-in-cpp

0 Response to "How To Code Snake In C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel