Explanation of C-language greedy snakes 3. Let the snakes get up and get up
Explanation of C-language greedy snakes 3. Let snakes move
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.
I have taught you how to draw snakes. Now I will teach you how to make snakes move. In order to make everyone better understand, the simplest method is used for snake movement. Here, linked lists and ordered queues are not used. The snake moves its head forward, and the body behind it moves to the original position of the previous section.
Like this:
Int I; for (I = sLength-1; I <1; I --) // start with 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];}
What should I do?
To determine the position of the snake header, we define a variable that originally stores the current direction of the snake.
Int direction; // the direction of the snake
We can use the direction value to mark the direction of the current snake. For example, 0 indicates upper, 1 indicates lower...
For convenience, we define some constants
// Define the direction represented by each value of ction # define UP 0 # define DOWN 1 # define LEFT 2 # define RIGHT 3
In this way, we can write
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; }
The completed move function is as follows:
Void move () {int I; for (I = sLength-1; I> 0; I --) // start 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 ;}}
Then move is written, and we need to write a loop to keep the snake moving. After the map is painted once and the snake is painted, we need to move the cursor to the starting position. The simplest way is to move the mark to (0, 0) every time a map is drawn ).
Modify the drawMap function as follows. Add a set position statement at the beginning.
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 the line }}
Then create a loop and draw snakes and move continuously.
Int main () {init (); // initialization operation when the program starts drawMap (); // draw a map while (1) {drawSnake (); // draw the snake Sleep (WAIT_TIME); // wait for a period of time to move (); // move the snake (mainly to modify the data of the snake body array)} getchar (); return 0 ;}
Then, observe that the snake is indeed gone, but the traces of the snake are still there.
In this way, we can erase the last tail in the snake moving function.
The modified move function is as follows:
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 = sLength-1; I> 0; I --) // start 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 ;}}
Run it. The snake is moving.
Then try to modify the next direction. Write
direction=LEFT;
Try again
Good. It feels normal.
Now we can change the forward direction of a snake by assigning a value to the direction variable.
Next, let's try to use the keyboard to control it.
Write a new function key to process keyboard input.
We cannot input any trace on the screen, and we cannot enter a key and press Enter... So we use the getch function.
Add the conio. h header file. Then, getch will pause the program and wait for the input, while the snake will only need to input it when turning. So we need a function to judge whether there is any input.
Then, we can control the direction of the snake by entering the characters w, a, s, or d.
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 ;}}}
Now, we can use the four buttons wasd to control snakes everywhere. (Switch to the English input method before entering the game)
The complete code for this step is provided below.
# Include <stdio. h> # include <stdlib. 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 {lele_cursor_info cursor_info = {1, 0}; SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), & cursor_info); // hide Tibetan mark int I, j; for (I = 0; I <H; I ++) {a [I] [0] = 1; // Let the first column 1 a [I] [W-1] = 1; // Let the last column 1} for (j = 0; j <W; j ++) {a [0] [j] = 1; // Let the first Behavior 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 position = {j, I}; SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDL E), position);} 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 = sLength-1; I> 0; I --) // start 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 a snake {int I; for (I = 0; I <sLength; I ++) {gotoxy (s [I] [0], s [I] [1]); // move the coordinate printf ("@") to the snake; // draw the 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 a map while (1) {drawSnake (); // draw a snake Sleep (WAIT_TIME); // wait for a while key (); move (); // move the snake (mainly modifying the data in the snake body array)} getchar (); return 0 ;}