The realization of Snake C language

Source: Internet
Author: User

The realization of the method of/*   /*   and Kinsoku translation   By black4yl   principle:   in an array of size 100, Position the current head and tail using the head,rear labeled Head and tail coordinate values.    while walking, wipe off the tail, mark the new head in the direction, wipe the old head,  to realize the snake walking.    when food is eaten, only changes the head, does not change */#include "windows.h" #include "conio.h" #include "iostream" #include <time.h>using  namespace std; #define  up      72#define down     80#define left    75#define right   77void  Gotoxy (int x,int y) {    coord c;    c.x=x-1; c.y= Y-1;    setconsolecursorposition (GetStdHandle (Std_output_handle), c);} Void display (char img[][22],int score,int speed)    //0 for empty  1 for body  2 for head  3 for food {    system ("CLS");    int i,j;     Gotoxy (    for) (i=0;i<24;i++)   cout<< "-";     cout<<endl;    for (i=0;i<22;i++)      {        cout<< "-";         for (j=0;j<22;j++)         {                 if (img[i][j]==0)                       cout<< "  ";                 if (img[i][j]==1)                      cout<< "0";                 if (img[i][j]==2)                      cout<< "0";                 if (img[i][j]==3)                      cout<< "0";         }                 cout<< "-" <<endl;    }    for (i=0;i <24;i++)   cout<< "-";         gotoxy (50,10);  printf ("score:  %d ", score);         gotoxy (50,11);  printf ("Speed=  %d ms", speed);} Void welcome () {    long time=clock ();     gotoxy (3,8);     cout<< "Welcome to the Snake Game" <<endl;    while (Clock ()-time<2000);     system ("CLS");     gotoxy (3,8);     time=clock ();    cout<< "action Method:  press   up   down   left   right   keys to control snakes, In order to eat more food, do not starve Oh ~  "<<endl;    gotoxy (6,13);    int i=4;     do    {        time=clock ( );         while (Clock ()-time<1000);             gotoxy (6,13);         cout << "The game will be in--" <<i<< "-Seconds" << "after the start";     }while (i--);     system ("CLS");} Int main () {    int x, y;    int fx,fy;   Food     srand (Time (0));     fx=rand ()%20+1;    fy= Rand()%20+1;    int len=0;    int i,j;     int tcspos[2][150];    char img[22][22]={0};    char  arr=77;    long time;//Time     int timeover;     int speed=500,score=0;    int head,rear;    head=3;     rear=0;    for (i=0; i<4; i++) {         //Initialize Snake body         tcspos[0][i] =  1;        tcspos[1][i] = i + 1;     }     img[fy][fx]=3;           welcome ();     display (img,score,speed);     while (1)      {            timeover=1;         time=clock ();         while ((Timeover=clock ()-time<=speed) &&!kbhit ());         if (timeover)   //timeover  non 0 , which indicates that the key is interrupted loop         {             getch ();             arr= Getch ();        }         Switch (arr)         {                 //Complete Operation              case up: y=tcspos[1][head]-1; x=tcspos[0][head]; break;                              //on             case down: y= tcspos[1][head]+1; x=tcspos[0][head];  break;           under                 //            case left: y=tcspos[1][head];  x=tcspos[0][head]-1;  break;                         //left              case right: y=tcspos[1][head]; x=tcspos[0][head ]+1; break;                         //Right         }         if (img[y][x]!=0&&! ( (X==FX) && (y==fy))   //conflict         {                  break; //End          }        if (x<0| | x>21| | y<0| | Y&GT;21)         {                 break; //End          }        if (X==fx && (y==fy))   //eat          {                 len++;            score+=2;   //every one plus 2 points              if (len>9)   //Snake grew up in 10,             {                 len%=9;  //recalculation                  speed-=20;  //each speed increase 20ms                 score+=10;  //not complete a 10, then add 10 points              }        img[tcspos[1][head] ][tcspos[0][head]]=2; //marks the walking head         head= (head+1)%150; // head  move to second position;         tcspos[0][head]=x;  //set Snake head coordinates          tcspos[1][head]=y;        do                          //Produce Food         {             fx=rand ()%21+1;             fy=rand ()%21+1;        }while (img[fy][fx]!=0);         img[fy][fx]=3;        display (IMG, Score,speed);        }         else   //not to eat         {                 img[tcspos[1][head]][tcspos[0][head]]=1;  //head into Body &NBSP;&NBSP;&NBSp;         head= (head+1)%150;             //Head Shift              img[tcspos[1][rear]][tcspos[0][rear]]=0; //Tail              rear= (rear+1)%150;          // Tail             tcspos[1][head]=y;   Front Shift           tcspos[0][head]=x;             img[tcspos[1][head]][tcspos[0][head]]=2;  //tagged new head              display (img,score,speed);         }    }  //while ()              system ("CLS");             gotoxy (20,12);             cout<< "Score:" <<score< <endl;            gotoxy (20,13);             cout<< "---game over---" <<endl;             gotoxy (20,14);             cout<< "Please enter your name:";             getchar ();}


This article is from the "Black4yl" blog, make sure to keep this source http://black4yl.blog.51cto.com/4222963/1579265

The realization of Snake C language

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.