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