It is recommended to copy the code and run it again to see the idea!!!
Easy to understand, please read it carefully.
It is worth noting that the code I give does not add walls. If necessary, add it yourself.
No difficulty design, ibid.
-
Map size (the wall is designed here, which is not implemented in the code)
Set an integer array map with a size of 1600, corresponding to the size of 1600. Initialize the array so that all values in the array are 0, and 0 represents empty space.
By setting the width of the window to 80 and the characters corresponding to each map[i] occupy two spaces during printing, we can realize the effect of wrapping every 40 map[i].
In this way, the length and width of the map is 40x40
2. wall
The wall is at the periphery of the map, which means that when I in map[i] is the map boundary, it corresponds to the wall. Use the if statement to judge the value of I and print the wall according to the conditions at the boundary.
According to the size of the map, when 0 < = I < = 39, the map[i] corresponds to the first line, that is, the boundary of the map. Similarly, the I of the left boundary of the map satisfies i%40==0, and the I of the right boundary satisfies (i+1)% 40 = = 0. Similarly, the lower boundary. The function of printing the wall can be realized by putting the above conditions into the if statement.
3. snake
How do snakes move? And keep a certain length. In fact, we only need one snake head to do it. Because the snake body is only the position of the old snake head, we always only need to update the snake head coordinates to achieve the goal. How to maintain the length?, It is obvious that we update the new snake head and remove the old snake tail. In the map size, we mentioned that if map[i] is equal to 0, it is an open space. Just change the value of snake tail to 0. Because the snake is moving all the time, the snake body in the previous section of the current snake tail will be the snake tail of the next moment. It is natural to associate with a group of numbers 1234 with a tolerance of 1, If we want to make the snake move continuously, the value of the new snake head is 4. Before adding the new snake head, the snake tail becomes 0, the previous snake body of the snake tail becomes 1, and the corresponding value of the snake tail at this time is 1. Subtract 1 from the whole snake, and the value of the snake at this time is 0123. Add the snake head 401234, the original Snake tail becomes an empty space, and the previous snake body of the original Snake tail becomes the current snake tail. The continuous movement of the greedy snake is completed.
4. food
Naturally, in the above three sections, when map[i] is 0 and positive integers have corresponding meanings, we make it map[i]=-1 Because the food generation requirements in the snake game are random, but it is not mentioned in our c + + course, I found the random number generation function srand on the Internet (the same is true for the window size set above). The generated random number I may exceed the size of the array, and take the remainder of 1600 for I. food can only be generated in the open space. When map[i] is equal to 0, make map[i]=-1. New food is generated. It is easy to ignore that we do not give map[i] to the wall The value of, that is, the value of the wall in the array corresponds to the open space, but whether it is a wall is judged by the position of I. here, the position of the wall should also be added to the judgment conditions to avoid food being generated in the wall. New food is produced immediately after the old food is eaten.
5. Death judgment
We judge whether the snake is dead by the value corresponding to the new snake head
(1) map [headx + heady * 40] > 0. From 3, we can know that the map [i] value corresponding to the snake body is greater than 0, and the map value corresponding to the new snake head is greater than 0, which means that the new snake head is generated in the snake body. Press
According to the rules of greedy snake, the game is over
(2) (headx+heady*40) when the printing conditions of the wall are met, it means that the snake head is generated on the wall and the game is over
Implementation code
#include<bits/stdc++.h> #include<windows.h> #include<iostream> #include<conio.h> int main() { int headx = 0, heady = 0, len = 5, map[1600] = { 0 }, i = 0; char c = 'd', cl = 'd',body=3,head=2; /*generation of random number*/ srand((unsigned)time(0)); system("mode con:cols=80 lines=40"); map[rand() % 1600] = -1; while (true) { if (_kbhit())//Judge keyboard input { cl =_getch(); if (cl < 97) cl += 32; if (cl == 'a' && c != 'd' || cl == 'd' && c != 'a' ||cl == 'w' && c != 's' || cl == 's' && c != 'w')//Judge whether the input direction conflicts with the original direction c = cl; } /*Change snake head coordinates*/ if (c == 'a') headx--; if (c == 'd') headx++; if (c == 'w') heady--; if (c == 's') heady++; /*Judge whether the snake head hit the wall*/ if(headx==-1||headx==40||heady==-1||heady==40) break; /*Determine whether the snakehead is an open space*/ if (map[heady * 40 + headx]) { if (map[heady * 40+headx] > 0) break; /*Eat food, snake body + 1, and generate new food in the open space*/ if (map[heady * 40 + headx] < 0) { len++; for (i = rand() % 1600; map[i] || !(map[i] = -1); i = rand() % 1600); } } /*If it is an open space, the snake's body value will be reduced by 1 as a whole, and the snake's tail will become an open space from 1 to 0*/ else for (i = 0; i < 1600; i++) { if (map[i] > 0) map[i] -= 1; } system("cls"); /*Assign a value to the new snake head and traverse the map. If the value is greater than 0, it is the snake body, if it is equal to 0, it is the open space, and if it is less than 0, it is the food*/ for (map[heady * 40 + headx] = len, i = 0; i < 1600; i++) { if (map[i] == len) { printf("%d ", map[i]); continue; } if (map[i] > 0) printf("%d ", map[i]); if (map[i] == 0) printf("%d ", map[i]); if (map[i] == -1) printf("%d", map[i]);//Print } Sleep(100); } }
effect
If you read the code carefully, it's easy to replace the number with the symbol you want (two characters in size, plus a space if it's a character)
If you want the game to cycle, it's also easy to do.
This is just a demo. You can rewrite it according to your needs.