Explanation of C-language greedy snakes 4. Introduction of food, explanation of greedy snakes

Source: Internet
Author: User
Tags random seed

Explanation of C-language greedy snakes 4. Introduction of food, explanation of greedy snakes
Explanation of C language snake 4. Food delivery

After my lab training class a few days ago, I set up a snake. Today I have time to explain my questions. I will teach you how to write a Snake Game in several steps. Since the C language has not been completed, this tutorial only involves knowledge points such as arrays and functions.

 

Through the previous tutorials, we have made a snake that can run up, down, and down. Now let's start with food serving.

The basic idea of food serving is to randomly find a place without snakes or obstacles on the map, and mark the map array value of this place as-1 (we set the open space to 0 in front, obstacle: 1 ).

Let's first write a function to determine whether a vertex meets the above conditions.
Int check (int ii, int jj) // determines whether the vertex can put food. If so, 1 is returned, 0 {if (a [ii] [jj] = 1) cannot be returned. // if there is an obstacle, 0 return 0; int I; for (I = 0; I <sLength; I ++) {if (ii = s [I] 0 & jj = s [I] [1]) // if it overlaps with one of the snakes, 0 return 0;} is returned ;} if (ii = 0 | ii = H-1 | jj = 0 | jj = W-1) // if it is above the boundary, return 0 return 0; return 1; // The final filtered vertex is a qualified vertex}

 

To use a random number, first

#include <stdlib.h>

Then, we need to set the Random Seed according to the time.

#include<time.h>

Write this code in the init function.

Srand (unsigned) time (NULL); // you can specify the current time as the seed of the random number.

Then write a food function to deliver a food

Void food () {int I, j; do {I = rand () % H; // generate 0 ~ A number between H-1 j = rand () % W;} while (check (I, j) = 0 ); // generate the vertex until the condition a [I] [j] =-1 is met; // mark it as food gotoxy (I, j); printf ("$ "); // draw food}

Then we call food once before the loop in main to start the game. Then, check whether the sequence of the snake is coincident with the food. If the sequence is coincident, call the "food" command once to put a food.

Int main () {init (); // The initialization operation drawMap () when the program starts; // draw the map food (); while (1) {drawSnake (); // draw the snake Sleep (WAIT_TIME); // wait for a period of time key (); move (); // move the snake (mainly to modify the data of the snake body array) if (a [s [0] [0] [s [0] [1] =-1) // if the snakeskin encounters a food, feed the food again, and reset the food point to 0 {food (); a [s [0] [0] [s [0] [1] = 0 ;}} getchar (); return 0 ;}

 

Let's take a look at the effect.

Code until now:

# Include <stdio. h> # include <stdlib. h> # include <time. h> # include <windows. h> # define H 23 # define W 75 # define WAIT_TIME 500 // define the direction of each value of ction # define UP 0 # define DOWN 1 # define LEFT 2 # define RIGHT 3int a [H] [W]; // map array int s [H * W] [2]; // snake body coordinate array int sLength; // snake length int direction; // snake direction void init () // initialization operation at the beginning of the program {srand (unsigned) time (NULL); // set the random number seed to the current time lele_cursor_info cursor_info = {1, 0}; SetConsoleC UrsorInfo (GetStdHandle (STD_OUTPUT_HANDLE), & cursor_info); // hide the mark int I, j; for (I = 0; I <H; I ++) {a [I] [0] = 1; // make the first column 1 a [I] [W-1] = 1; // Let the last column be 1} for (j = 0; j <W; j ++) {a [0] [j] = 1; // Let the first act 1 a [H-1] [j] = 1; // Let the last row be 1} sLength = 4; // set the initial length of the snake to 4 s [0] [0] = H/2; s [0] [1] = W/2; // assign the coordinates of the snake header to (I = 1; I <4; I ++) {s [I] [0] = s [0] [0] + I; s [I] [1] = s [0] [1]; // give the initial coordinates of the starting snake body} direction = UP;} void gotoxy (int I, int j) // move the cursor {COORD positio N = {j, I}; SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), position);} int check (int ii, int jj) // you can check whether food can be put at this point, the return value can be 1, and the return value cannot be 0 {if (a [ii] [jj] = 1). // if there is an obstacle, the return value is 0 return 0; int I; for (I = 0; I <sLength; I ++) {if (ii = s [I] [0] & jj = s [I] [1]) // if it overlaps with one of the snakes, 0 return 0;} is returned ;} if (ii = 0 | ii = H-1 | jj = 0 | jj = W-1) // if it is above the boundary, return 0 return 0; return 1; // The final filtered point is the qualified point} void food () {int I, j; do {I = rand () % H; // generate 0 ~ A number between H-1 j = rand () % W;} while (check (I, j) = 0 ); // generate the vertex until the condition a [I] [j] =-1 is met; // mark it as food gotoxy (I, j); printf ("$ "); // draw food} void drawMap () // draw a map {gotoxy (); int I, j; for (I = 0; I <H; I ++) {for (j = 0; j <W; j ++) // two-for Loop traversal Array {if (a [I] [j] = 0) // 0 to output the space printf (""); else // 1 to output # printf ("#");} printf ("\ n "); // don't forget to wrap} void move () {int I; gotoxy (s [sLength-1] [0], s [sLength-1] [1]); printf (""); // draw a space on the tail to erase the tail for (I = sLeng TH-1; I> 0; I --) // starting from the tail, the position of each vertex is equal to the position of the previous vertex {s [I] [0] = s [I-1] [0]; s [I] [1] = s [I-1] [1];} switch (direction) {case UP: s [0] [0] --; break; case DOWN: s [0] [0] ++; break; case LEFT: s [0] [1] --; break; case RIGHT: s [0] [1] ++; break ;}} void drawSnake () // draw snake {int I; for (I = 0; I <sLength; I ++) {gotoxy (s [I] [0], s [I] [1]); // move the coordinate printf ("@") to the snake ("@"); // draw a snake in this position} void key () {if (kbhit ()! = 0) // if there is a keyboard input {char in; while (! Kbhit () = 0) // if the player inputs multiple buttons, the last button prevails in = getch (); switch (in) {case 'W ': case 'W': if (direction! = DOWN) // you cannot scale DOWN the header .... Direction = UP; break; case's ': if (direction! = UP) direction = DOWN; break; case 'A': if (direction! = RIGHT) direction = LEFT; break; case 'D': if (direction! = LEFT) direction = RIGHT; break ;}} int main () {init (); // initialization operation at program start drawMap (); // draw the map food (); while (1) {drawSnake (); // draw the snake Sleep (WAIT_TIME); // wait for a while key (); move (); // move the snake (mainly modifying the data in the snake body array) if (a [s [0] [0] [s [0] [1] =-1) // If the snakeskin encounters food, place the food again and set the food point to 0 {food (); a [s [0] [0] [s [0] [1] = 0 ;}} getchar (); return 0 ;}

 

Okay, now the food is serving normally.

Next time, let's implement the variable length function for snakes to eat food.

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.