C Language Console Snake 4

Source: Internet
Author: User

snake death judgment, eating food, scoring

One, the death of the Snake judgment: To install the border, hit their own death

Definition: Issnakedie ()//Snake death judgment function, return type bool type

Ideas:

// the next of the Serpent's Head, is the square is death     if(0 = = strncmp (&g_strgameback[g_arrsnake[0] [0]][g_arrsnake[0 [1] + g_arrsnake[0[2""2) )    {        returnfalse;    }

Second, randomly produce food and eat things to become longer

1, (1) produce random coordinates for food

// Generate random coordinates int  = rand ()%1= rand ()%1;

(2) Draw food on the resulting position:

strncpy (&g_strgameback[g_nrow][g_ncol*2"★"2);

At this point, the call function results in the display of food, but there is no judgment as to whether new food should be produced, so food is randomly generated.

(3) Deciding whether to produce new food

BOOL true ; // decide whether to produce new food if (false = = G_bisprofood)    {return  ;}

The randomly generated food coordinates and the ergodic snake array are placed in a loop to realize that there is only one food before the snake eats the food.

2, the snake eats the matter to become longer

When the head coordinates are equal to the coordinates of the food, the snake body becomes longer. First define a snake body length: int g_nsnakelen = 2;

//The snake Head cursor is equal to the coordinatesif(G_nrow = = g_arrsnake[0][0] && g_nlie*2= = g_arrsnake[0][1]){    //Snakes grow Up    if(To_east = = g_arrsnake[g_nsnakelen][2]) {G_arrsnake[g_nsnakelen+1][0] = g_arrsnake[g_nsnakelen][0]; G_arrsnake[g_nsnakelen+1][1] = g_arrsnake[g_nsnakelen][0] -2; G_arrsnake[g_nsnakelen+1][2] = g_arrsnake[g_nsnakelen][0]; }    Else if(To_west = = g_arrsnake[g_nsnakelen][2]) {G_arrsnake[g_nsnakelen+1][0] = g_arrsnake[g_nsnakelen][0]; G_arrsnake[g_nsnakelen+1][1] = g_arrsnake[g_nsnakelen][0] +2; G_arrsnake[g_nsnakelen+1][2] = g_arrsnake[g_nsnakelen][0]; }    Else if(To_north = = g_arrsnake[g_nsnakelen][2]) {G_arrsnake[g_nsnakelen+1][0] = g_arrsnake[g_nsnakelen][0]; G_arrsnake[g_nsnakelen+1][1] = g_arrsnake[g_nsnakelen][0] +1; G_arrsnake[g_nsnakelen+1][2] = g_arrsnake[g_nsnakelen][0]; }    Else if(To_south = = g_arrsnake[g_nsnakelen][2]) {G_arrsnake[g_nsnakelen+1][0] = g_arrsnake[g_nsnakelen][0]; G_arrsnake[g_nsnakelen+1][1] = g_arrsnake[g_nsnakelen][0] -1; G_arrsnake[g_nsnakelen+1][2] = g_arrsnake[g_nsnakelen][0]; } G_nsnakelen++; G_bisprofood=true;}

When the scoring function is written, it needs to be called in the variable length function of the snake, and the score is increased as well.

Third, scoring

Set cursor Position: SetConsoleCursorPosition ();

 8; // Set cursor Position setconsolecursorposition (GetStdHandle (std_output_handle), RD);p rintf ("score is:   "  9;    SetConsoleCursorPosition (GetStdHandle (std_output_handle), RD);p rintf ("%d" , g_nscore*);

The process is over.

C Language Console Snake 4

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.