The realization of the greedy snake game based on C language complete instance Code _c language

Source: Internet
Author: User
Tags define null int size

This article in the form of an example of the implementation of the C-language based on the greedy Snake game code, this is a more common game, the code with a more detailed comments for the reader to understand a certain degree of help.

Greedy snake complete implementation code is as follows:

#include <graphics.h> #include <conio.h> #include <stdlib.h> #include <dos.h> #define NULL 0 #def
ine up 18432 #define DOWN 20480 #define LEFT 19200 #define RIGHT 19712 #define ESC 283 #define ENTER 7181 struct snake
 {int CenterX;
 int centery;
 int newx;
 int newy;
struct Snake *next;
};

struct Snake *head;                     int grade=60;                        /* Control speed of the *******/int a,b;             * * Back static cover position/void *FAR1,*FAR2,*FAR3,*FAR4;              * * Snake body pointer back to hide the pointer worm/int size1,size2,size3,size4;                     * * * * * global variable **/int ch=right; 
/************** the key to start the snake in the direction of the right***********/int chy=right;                       int flag=0;                     /********* judge whether to exit the game **************/int control=4;                     /*********** to judge the last direction and the next direction does not conflict ***/int nextshow=1;                      /******* controls whether the next snake body shows ***************/int scenterx;
/*************** the rectangular Center coordinate ***************/int scentery;                         int SX; /******* get their value before a B is changed to ensure that theThe machine rectangle also does not appear here *******/int sy;
 /************************ is initialized **************************/void Snakede () {struct Snake *p1,*p2; 
 head=p1=p2= (struct snake *) malloc (sizeof (struct snake));
 p1->centerx=80;
 p1->newx=80;
 p1->centery=58;
 p1->newy=58;
 p1= (struct snake *) malloc (sizeof (struct snake));
 p2->next=p1;
 p1->centerx=58;
 p1->newx=58;
 p1->centery=58;
 p1->newy=58;
p1->next=null;                
/*******************end*******************/void Welcome ()/************* Game start interface, you can choose Speed **********/
 {int key;
 int size;
 int x=240;
 int y=300;
 int F; 
 void *buf;
 Setfillstyle (Solid_fill,blue);
 Bar (98,100,112,125);
 Setfillstyle (solid_fill,red);
 Bar (98,112,112,114);
 Setfillstyle (Solid_fill,green);
 Bar (100,100,110,125);
 Size=imagesize (98,100,112,125);
 Buf=malloc (size);
 GetImage (98,100,112,125,BUF);

 Cleardevice ();
 Setfillstyle (Solid_fill,blue);
 Bar (240,300,390,325);
 Outtextxy (193,310, "Speed:"); Setfillstyle (SOLid_fill,red);
 Bar (240,312,390,314);
 SetColor (yellow);
 Outtextxy (240,330, "down");
 Outtextxy (390,330, "up"); 
 Outtextxy (240,360, "ENTER to start ...");
 Outtextxy (270,200, "SNAKE");
 Fei (220,220);
 Feiyang (280,220); 
 Yang (340,220); 
 Putimage (X,y,buf,copy_put);
 SetColor (RED);
 Rectangle (170,190,410,410);
  while (1) {if (Bioskey (1))/********8 Select the speed portion ************/key=bioskey (0);
   Switch (key) {case enter:f=1;
    Break
   Case Down:if (x>=240) {putimage (x-=2,y,buf,copy_put);
   grade++;
   key=0;
    Break
   Case Up:if (x<=375) {putimage (x+=2,y,buf,copy_put);
    grade--;
   key=0;
    Break 
 } if (f==1) break;
 }/********** End ****************/free (BUF);
 /************************* the rectangle *****************//*********** Call this function when Nextshow is 1 **********/void Ran () {int NX;
 int NY;         int show;
 /********** controls whether the ***********/int jump=0 is displayed;
 struct Snake *p; p=Head
  if (nextshow==1)/*********** whether to start randomly generating ***************/while (1) {show=1;
  Randomize ();
  Nx=random (14);
  Ny=random (14);
  scenterx=nx*22+58;
   scentery=ny*22+58; while (P!=null) {if (scenterx==p->centerx&&scentery==p->centery| |
  Scenterx==sx&&scentery==sy) {show=0;
  Jump=1;
  Break 
   else p=p->next;
  if (jump==1) break;
  } if (show==1) {putimage (scenterx-11,scentery-11,far3,copy_put);
  nextshow=0;
  Break
 }}/*********** Clearance animation **************/void Donghua () {int i;
 Cleardevice ();
 SetBkColor (black);
 Randomize (); 
  while (1) {for (i=0;i<=5;i++) {putpixel (random (640), random (80), 13); 
  Putpixel (Random (640), random (80) +80,2); 
  Putpixel (Random (640), random (80) +160,3);
  Putpixel (Random (640), random (80) +240,4);
  Putpixel (Random (640), random (80) +320,1);
 Putpixel (Random (640), random (80) +400,14);
  } setcolor (yellow);
  Settextstyle (0,0,4);
  Outtextxy (130,200, "wonderful!!");
  Setfillstyle (solid_fill,10); Bar(240,398,375,420);
  Feiyang (300,400);
  Fei (250,400);
  Yang (350,400);
  if (Bioskey (1)) if (Bioskey (0) ==esc) {flag=1;
 Break
/*************************end************************//*********************** Initialization Graphics system *********************/
 void init () {int a=detect,b;
 int i,j;
Initgraph (&a,&b, ""); /***************************end****************************//*** draw a solid border effect function ******/void tline (int x1,int y1,int,
 int Y2,int White,int black) {setcolor (white);
 Line (X1,Y1,X2,Y1);
 Line (X1,Y1,X1,Y2);
 SetColor (black);
 Line (X2,Y1,X2,Y2);
 Line (X1,Y2,X2,Y2); /****end*********//************* flying Ocean Flag **********/int Feiyang (int x,int y) {int feiyang[18][18]={{0,0,0,0,0,0,1,1,1, 1,1,1,0,1,1,0,0,0}, {0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,0}, {0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0}, { 0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0}, {0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0}, { 0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0}, {0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0}, {0,0,1,1,1,1,1,0,0,1,0,0,1,1,0,0,0,0}, {0,0,1,1,1,0,0,0,0,1,0,1,1,1,0,0,0,0}, {0,0,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0}, { 0,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}, {0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,1,0,0}, { 0,0,1,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0}, {0,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0}, { 0,0,0,1,1,1,0,1,1,1,1,1,0,0,1,0,0,0}, {0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0}, {
 0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0}};
int i,j;
 for (i=0;i<=17;i++) for (j=0;j<=17;j++) {if (feiyang[i][j]==1) Putpixel (j+x,i+y,red); }/******** "Fly" *************/int fei (int x,int y) {int fei[18][18]={{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1, 1,1,1,1,1,1,1,0,0,0,0,0,0,0}, {0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0}, { 0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0}, {0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0}, { 0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0}, { 0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0}, {0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0}, {0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0}, { 0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1}, { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0}, {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}};
int i,j;
  for (i=0;i<=17;i++) for (j=0;j<=17;j++) {if (fei[i][j]==1) Putpixel (J+x,i+y,blue); }/********* the "Ocean" character **************/int yang (int x,int y) {int yang[18][18]={{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, { 1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0}, {0,1,1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0}, { 0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0}, {0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, { 0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, { 0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0}, {0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0}, { 0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0}, { 0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0}, {0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0}, {1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, { 0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, { 
0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}};
  int i,j;
       for (i=0;i<=17;i++) for (j=0;j<=17;j++) {if (yang[i][j]==1) Putpixel (J+x,i+y,blue);
 }/****************** Home View **********************/int bort () {int A;
 Setfillstyle (solid_fill,15);
 Bar (49,49,71,71);
 Setfillstyle (Solid_fill,blue);
 Bar (50,50,70,70);
 Size1=imagesize (49,49,71,71);
 far1= (void *) malloc (SIZE1);
 GetImage (49,49,71,71,FAR1); 
 Cleardevice ();
 Setfillstyle (solid_fill,12);
 Bar (49,49,71,71);
 Size2=imagesize (49,49,71,71);
 far2= (void *) malloc (SIZE2);
 GetImage (49,49,71,71,FAR2);
 Setfillstyle (solid_fill,12);
 Bar (49,49,71,71);
 Setfillstyle (Solid_fill,green);
 Bar (50,50,70,70);
 Size3=imagesize (49,49,71,71);
 far3= (void *) malloc (SIZE3);
 GetImage (49,49,71,71,FAR3);                    Cleardevice (); /* Take snake body node background node worm node end*/SetbKcolor (8);
 Setfillstyle (Solid_fill,green);
 Bar (21,23,600,450);              Tline (21,23,600,450,15,8);
 /*** start Game scene frame stereo effect *******/tline (23,25,598,448,15,8);
 Tline (45,45,379,379,8,15);
 Tline (43,43,381,381,8,15);
 Tline (390,43,580,430,8,15);
 Tline (392,45,578,428,8,15);
 Tline (412,65,462,85,15,8);
 Tline (410,63,464,87,15,8);
 Tline (410,92,555,390,15,8);
 Tline (412,94,553,388,15,8);
 Tline (431,397,540,420,15,8);
 Tline (429,395,542,422,15,8);
 Tline (46,386,377,428,8,15); 
 Tline (44,384,379,430,8,15);
 SetColor (8);
 Outtextxy (429,109, "press ENTER");     Outtextxy (429,129, "---to start");
 /* Keyboard Control instructions * * OUTTEXTXY (429,169, "press ESC");
 Outtextxy (429,189, "---to quiet");
 Outtextxy (469,249, "up");
 Outtextxy (429,289, "left");
 Outtextxy (465,329, "down");
 Outtextxy (509,289, "right");
 SetColor (15);
 Outtextxy (425,105, "press ENTER");
 Outtextxy (425,125, "---to start");
 Outtextxy (425,165, "press ESC");
 Outtextxy (425,185, "---to quiet");
 Outtextxy (465,245, "up");
 Outtextxy (425,285, "left"); OutTextxy (461,325, "down");       Outtextxy (505,285, "right");
 /*******end*************/SetColor (8);
 Outtextxy (411,52, "score");
 Outtextxy (514,52, "left");
 SetColor (15);
 Outtextxy (407,48, "score");

 Outtextxy (510,48, "left");       Size4=imagesize (409,62,465,88);
 The/**** fraction box is placed in memory ********/far4= (void *) malloc (SIZE4);
 GetImage (409,62,465,88,FAR4);      Putimage (500,62,far4,copy_put);
 /******* Output Life frame ***********/Setfillstyle (solid_fill,12);
 SetColor (RED);         Outtextxy (415,70, "0");          /*************** input score is zero **********/outtextxy (512,70, "20");
  /************* shows the number of bugs to eat *********/bar (46,46,378,378);
  Feiyang (475,400);
  Fei (450,400);
  Yang (500,400);
  Outtextxy (58,390, "mailto:jiangzhiliang002@tom.com");
  Outtextxy (58,410, "Snake Game");
 Outtextxy (200,410, "made by Yefeng");
  while (1) {if (Bioskey (1)) A=bioskey (0);
 if (a==enter) break;
 }/******************gameover () ******************/void Gameover () {char *p= "GAME over"; 
 int cha;
 SetColor (yellow); SettexTstyle (0,0,6);

  Outtextxy (100,200,P);
  while (1) {if (Bioskey (1)) Cha=bioskey (0);
  if (CHA==ESC) {flag=1;
  Break 
 }}/*********** shows the **********************/void Snakepaint () {struct Snake *p1 of the snake body;
 P1=head;
 Putimage (A-11,b-11,far2,copy_put);
 while (P1!=null) {putimage (p1->newx-11,p1->newy-11,far1,copy_put);
 p1=p1->next; }/****************end**********************//********************* Snake body Refresh change game key part *******************/void
 Snakechange () {struct Snake *p1,*p2,*p3,*p4,*p5; 
 int i,j;
 static int n=0;
 static int score;
 static int left=20;
 Char sscore[5];
 Char sleft[1];
 P2=p1=head;
  while (p1!=null) {p1=p1->next;
  if (p1->next==null) {a=p1->newx;           b=p1->newy;
  /************ records the coordinates of the last node ************/sx=a;
  Sy=b;
  } p1->newx=p2->centerx;
  p1->newy=p2->centery;
 P2=P1;
} P1=head;
 while (P1!=null) {p1->centerx=p1->newx;
 p1->centery=p1->newy;
p1=p1->next; /******** to determine the key direction *******/if (BioskeY (1)) {ch=bioskey (0); if (CH!=RIGHT&AMP;&AMP;CH!=LEFT&AMP;&AMP;CH!=UP&AMP;&AMP;CH!=DOWN&AMP;&AMP;CH!=ESC)/********chy for the previous direction *********
 /Ch=chy;
         Switch (CH) {case Left:if (control!=4) {head->newx=head->newx-22;
  head->centerx=head->newx;
  control=2; 
     if (head->newx<47) Gameover ();
          else {head->newx=head->newx+22;
   head->centerx=head->newx;
   control=4;
     if (head->newx>377) Gameover ();
  } chy=ch; 
  Break
          Case Down:if (control!=1) {head->newy=head->newy+22;
   head->centery=head->newy;
   control=3;
         if (head->newy>377) Gameover ();
    else {head->newy=head->newy-22;
           head->centery=head->newy;
    control=1;
          if (head->newy<47) Gameover ();
         } chy=ch; 
  Break
          Case Right:if (control!=2) {head->newx=head->newx+22; Head->centerx=head->newx;
   control=4;
         if (head->newx>377) Gameover ();
          else {head->newx=head->newx-22;
          head->centerx=head->newx;
   control=2;
  if (head->newx<47) Gameover ();
         } chy=ch;
  Break
        Case Up:if (control!=3) {head->newy=head->newy-22;
        head->centery=head->newy;
    control=1;
       if (head->newy<47) Gameover ();
        else {head->newy=head->newy+22;
        head->centery=head->newy;
 control=3;
        if (head->newy>377) Gameover ();
        } chy=ch; 
 

  Break
       Case esc:flag=1;
 Break
   }/* If to determine whether to eat snakes/if (flag!=1) {if (head->newx==scenterx&&head->newy==scentery) {p3=head;
 while (p3!=null) {p4=p3;
   p3=p3->next;
  } p3= (struct snake *) malloc (sizeof (struct snake));
  p4->next=p3;
  p3->centerx=a;
  p3->newx=a;
  p3->centery=b; P3->newy=b; 
  p3->next=null;
  a=500;
  b=500;  Putimage (409,62,far4,copy_put);  /********** Fractional Frame blocks **************/putimage (500,62,far4,copy_put);
  /********* The former bug box in the ********/score= (++n) *100;
  left--;
  Itoa (score,sscore,10);
  Itoa (left,sleft,10);
  SetColor (RED);
  Outtextxy (415,70,sscore);
  Outtextxy (512,70,sleft);
  Nextshow=1;             if (left==0)/************ judge whether the clearance **********/Donghua ();       /******* if pass, play the Customs animation *********************/} P5=head;
 /********************* judge whether suicide ***************************/p5=p5->next;
 p5=p5->next;
 p5=p5->next;      p5=p5->next; /**** the fifth node to determine whether suicide ************/while (p5!=null) {if (head->newx==p5->centerx&&head->newy==p5->
    CenterY) {Gameover ();
  Break
 else p5=p5->next; The/************snakechange () function ends the *******************//***************************** main function **********************
 /int main () {int i;       Init (); /********** Initialization Graphics System/welcome ();       /*********8 Welcome Interface **************/Bort ();      /********* Home View ***************/Snakede ();
  The/********** table initializes **********/while (1) {snakechange ();
  if (flag==1) break;
  Snakepaint ();
  Ran ();
  for (i=0;i<=grade;i++) delay (3000);
 Free (FAR1);
 Free (FAR2);
 Free (FAR3);
 Free (FAR4);
 Closegraph (); 
return 0; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.