Attacking snakes (1) and attacking snakes (
Many of my friends who have studied C language or C ++ may have written snake-Greedy homework. I am also one of them. At the beginning of the writing, I had a poor level, and the writing effect was not as good as that of many students.
This series of blogs are written in C language, which gradually increases the difficulty. Of course, you have not started it for a long time. You can also review the C language yourself.
The first greedy snake, because many people who have just finished learning C don't know much about it, so we only use the most basic method to complete the most basic functions. Of course, our results may not be very good.
1 # include <stdio. h> 2 3 // Random Number Generation 4 # include <stdlib. h> 5 # include <time. h> 6 7 // Windows API 8 # include <Windows. h> 9 10 // game constant settings 11 # define MAP_H 10 12 # define MAP_W 20 13 # define INIT_LEN 2 14 # define GAME_SPEED 200 15 16 # define CH_SNAKE 'O' 17 # define CH_FOOD '* '18 # define CH_WALL 'H' 19 # define CH_SPACE ''20 21 char map [MAP_H] [MAP_W]; 22 struct _ point {23 int y; 24 int x; 25} snake [MAP_H * MA P_W]; 26 int snkelength; 27 enum _ direction {28 UP, LEFT, RIGHT, DOWN 29} direction; 30 31 void initialize (); // initialize 32 int update (); // update image 33 void generateFood (); // generate food 34 void changeDirection (); // direction control 35 void showMap (); // display screen 36 37/************** 38 * basic game logic: Display, control, update 39 */40 int main () {41 42 initialize (); 43 do {44 showMap (); 45 changeDirection (); 46} while (update (); 47 48 return 0; 49} 50 51/ ******************* 52 initialize the Image array, the coordinate array of the snake body, the length of the snake, and finally generate food 53 */54 void initialize () {55 int I, j; 56 snkelength = INIT_LEN; 57 direction = RIGHT; 58 59 for (I = 0; I <MAP_H; I ++) {60 for (j = 0; j <MAP_W; j ++) {61 map [I] [j] = CH_SPACE; 62} 63} 64 65 for (I = 0; I <INIT_LEN; I ++) {66 snake [I]. x = INIT_LEN-i-1; 67 snake [I]. y = 0; 68} 69 70 for (I = 0; I <snkelength; I ++) {71 map [snake [I]. y] [snake [I]. x] = CH_SNAKE; 72} 73 74 ge NerateFood (); 75} 76 77/********************** 78 * Sleep controls the game speed, the system function calls the console command cls to clear the screen. The final loop output screen 79 */80 void showMap () {81 system ("cls"); 82 int I, j; 83 84 for (I = 0; I <MAP_W + 2; I ++) {85 putchar (CH_WALL); 86} 87 putchar ('\ n '); 88 89 for (I = 0; I <MAP_H; I ++) {90 putchar (CH_WALL); 91 for (j = 0; j <MAP_W; j ++) {92 putchar (map [I] [j]); 93} 94 putchar (CH_WALL); 95 putchar ('\ n'); 96} 97 98 for (I = 0; I <MAP_W + 2; I ++) {99 putchar (CH_WALL ); 100} 101} 102 103/********************* 104 use the random number to generate the coordinates of food 105 */106 void generateFood () {107 int x, y; 108 srand (time (0); 109 while (1) {110 x = rand () % MAP_W; 111 y = rand () % MAP_H; 112 if (map [y] [x] = CH_SPACE) 113 break; 114} 115 map [y] [x] = CH_FOOD; 116} 117 118/******************** 119 * update the screen (part of game logic processing) 120 * use direction to calculate the position of the next image's snake header, check the object at this position, and process the 121 */122 int update () {123 Sleep (GAME_SPEED ); 124 int I, head_y, head_x; 125 126 switch (direction) {127 case UP: 128 head_y = snake [0]. y-1; 129 head_x = snake [0]. x; 130 break; 131 case LEFT: 132 head_y = snake [0]. y; 133 head_x = snake [0]. x-1; 134 break; 135 case RIGHT: 136 head_y = snake [0]. y; 137 head_x = snake [0]. x + 1; 138 break; 139 case DOWN: 140 head_y = snake [0]. y + 1; 141 head_x = snake [0]. x; 142 break; 143} 144 if (head_x <0 | head_x> MAP_W | head_y <0 | head_y> MAP_H) {145 retur N 0; // hit the wall 146} 147 148 if (map [head_y] [head_x] = CH_SNAKE) {149 return 0; // hit yourself 150} 151 if (map [head_y] [head_x] = CH_FOOD) {152 for (I = snkelength; I> 0; I --) {153 snake [I] = snake [I-1]; 154} 155 snake [0]. x = head_x; 156 snake [0]. y = head_y; 157 map [head_y] [head_x] = CH_SNAKE; 158 snkelength + = 1; 159 generateFood (); 160 return 1; // eat food 161} 162 if (map [head_y] [head_x] = CH_SPACE) {163 map [snake [snakeLength-1]. y] [snake [snkelength -1]. x] = CH_SPACE; 164 for (I = snakeLength-1; I> 0; I --) {165 snake [I] = snake [I-1]; 166} 167 snake [0]. x = head_x; 168 snake [0]. y = head_y; 169 map [head_y] [head_x] = CH_SNAKE; 170 return 1; // continue forward 171} 172 173 return 1; 174} 175 176/******************** 177 * use GetKeyState to obtain keyboard input, change the direction variable to control the travel direction 178 */179 void changeDirection () {180 if (GetKeyState (VK_UP) <0 & direction! = DOWN) 181 direction = UP; 182 else if (GetKeyState (VK_LEFT) <0 & direction! = RIGHT) 183 direction = LEFT; 184 else if (GetKeyState (VK_DOWN) <0 & direction! = UP) 185 direction = DOWN; 186 else if (GetKeyState (VK_RIGHT) <0 & direction! = LEFT) 187 direction = RIGHT; 188}
This is almost the first time I wrote it. It is very simple and has a poor user experience. However, apart from Sleep, system, and GetKeyState, there is no C language textbook that has never been taught.
SleepThe function suspends (Stops) the console to control the game speed.
SystemThe function is used to enter console commands, and cls is the console command for Screen Cleaning.
GetKeyStateThe function is used to read the status of a specific button on the keyboard. It is used to read user input (control direction)
Of course, the above Code has nothing but basic game logic. In addition, the screen will pop up. The most pitfall is the Sleep function, which sometimes cannot be read by keys. This is because Sleep suspends the entire program, and reading input is part of the program.