In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use C language to push boxes". In daily operation, I believe that many people have doubts about how to use C language to push boxes. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use C language to push boxes"! Next, please follow the editor to study!
Change the map code into a function
Note: every time you print a map, add a system ("cls") statement to clear and refresh the map. This method requires the header file of # include.
Encapsulate the map code of the push box in the previous article, and the results are as follows:
Void drawMap () {system ("CLS"); / / use a loop to traverse the array (graphing the game data) for (size_t I = 0; I < 10; iTunes +) {for (size_t j = 0; j < 10; jacks +) {/ * printf ("% 2d", map [I] [j]) * / switch (map [I] [j]) {case 0: printf (""); / / two spaces break; case 1: printf ("█"); break Case 2: printf ("♀"); break; case 3: printf ("●"); break; case 4: printf ("☆"); break Default: break;}} printf ("\ n");}}
The place where the map is stored can be declared outside the body of the main function.
After that, if you push to the success point and people go to the success point, you only need to add two conditions: case 2, 4 and case 3, 4. The specific code is shown in the final source code.
Character movement is modified into a function
We need to divide the function of character movement into two functions, one is to control the movement of the character, and the other is to find the coordinates of the position of the character.
1 looking for role function
In this function, we need to find the role, and determine whether the box can promote to do two functions encapsulated into a function, in this function, we need to find the position of the character, and then judge the location of the move.
/ / Mobile logic parameters: offset void move (int Xpenint Y) {/ / 1 find int posX = 0, posY = 0; for (size_t I = 0; I < 10; iSense +) {for (size_t j = 0; j < 10) PosX +) {if (2 = = map [I] [j] | | 2 + 4 = = map [I] [j]) {posX = I; posY = j; break } / / Open space or success point map [posX] [posY]: position of the protagonist if (0 = = map [posX + X] [posY + Y] | | 4 = = map [posX + X] [posY + Y]) {map [posX] [posY]-= 2; map [posX + X] [posY + Y] + = 2 } / / Box (push the box away from the point) else if (3 = = map [posX + X] [posY + Y] | | 3 + 4 = = map [posX + X] [posY + Y]) {/ / there is open space or success point map [posX-1] [posY]: box position if (0 = = map [posX + (X * 2)] [posY + (Y * 2)) | | 4 = = map [posX + (X * 2)] [posY + (Y * 2)]) {/ / the person in the current location leaves map [posX] [posY]-= 2 Map [posX + X] [posY + Y] + = 2; map [posX + X] [posY + Y]-= 3; map [posX + (X * 2)] [posY + (Y * 2)] + = 3;} 2 character movement function
This function processes the keyboard input and operates the movement at one time by passing in two parameters. There is no need to write the function again in all four directions. The following writing can prevent the user from turning on uppercase and not moving the role.
Void heroMove () {/ / controls the movement of characters / / 2 controls (keyboard: WSAD (up and down)) / / needs to get keys (characters) switch (getch ()) {case'wages: case 'characters: move (- 1,0); break Case's songs: case'slots: move (1,0); break; case'asides: case'Aids: move (0,-1); break; case'dflowers: case'Downships: move (0,1); break Default: break;}} judge the victory and modify it into a function
The bool type of C++ is used here, and the c language can also be used. If you don't want to use this function, you can change it to int.
Bool isWin () {for (size_t I = 0; I < 10; iTunes +) {for (size_t j = 0; j < 10; jaun +) {if (map [I] [j] = = 3) {return false;} return true;} body function
The idea here is to draw a map first, and then judge the movement until you decide that all the boxes are gone, and then determine the player's victory.
Int main () {while (! isWin ()) / / Game main cycle {drawMap (); heroMove ();} drawMap (); return 0 } push the source code # include # include # include char map [10] [10] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 4, 0, 0, 0, 0 1}, {1, 0, 0, 3, 0, 1, 0, 0, 0, 1}, {1, 0, 0, 2, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 0, 0, 3, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1} {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}} / * function declaration * / void drawMap (); void heroMove (); void move (int X, int Y); bool isWin (); int main () {while (! isWin ()) / / Game main loop {drawMap (); heroMove ();} drawMap (); return 0;} / * function definition * / / Mapping void drawMap () {system ("CLS") / / use a loop to iterate through the array (graphing the game data) for (size_t I = 0; I < 10; iSuppli +) {for (size_t j = 0; j < 10; Jake +) {/ * printf ("% 2d", map [I] [j])) * / switch (map [I] [j]) {case 0: printf (""); / / two spaces break; case 1: printf ("█"); break Case 2: printf ("♀"); break; case 3: printf ("●"); break; case 4: printf ("☆"); break Case 2 + 4: printf ("♀"); break; case 3 + 4: printf ("★"); break; default: break }} printf ("\ n") }} void heroMove () {/ / control character movement / / 2 control (keyboard: WSAD (top, bottom, left and right) / / need to get keys (characters) switch (getch ()) {case'winters: case 'characters: move (- 1,0); break Case's songs: case'slots: move (1,0); break; case'asides: case'Aids: move (0,-1); break; case'dflowers: case'Downships: move (0,1); break Default: break;}} / / Mobile logic parameters: offset in the X and Y directions of int Xreint Y void move (int Xreint Y) {/ / 1 find a person int posX = 0, posY = 0; for (size_t I = 0; I < 10; iTunes +) {for (size_t j = 0; j < 10) PosX +) {if (2 = = map [I] [j] | | 2 + 4 = = map [I] [j]) {posX = I; posY = j; break } / / Open space or success point map [posX] [posY]: position of the protagonist if (0 = = map [posX + X] [posY + Y] | | 4 = = map [posX + X] [posY + Y]) {map [posX] [posY]-= 2; map [posX + X] [posY + Y] + = 2 } / / Box (push the box away from the point) else if (3 = = map [posX + X] [posY + Y] | | 3 + 4 = = map [posX + X] [posY + Y]) {/ / there is open space or success point map [posX-1] [posY]: box position if (0 = = map [posX + (X * 2)] [posY + (Y * 2)) | | 4 = = map [posX + (X * 2)] [posY + (Y * 2)]) {/ / the person in the current location leaves map [posX] [posY]-= 2 Map [posX + X] [posY + Y] + = 2; map [posX + X] [posY + Y]-= 3; map [posX + (X * 2)] [posY + (Y * 2)] + = 3;} / / judge Shengli bool isWin () {for (size_t I = 0; I < 10; iBj +) {for (size_t j = 0; j < 10) ) {if (map [I] [j] = = 3) {return false;} return true;} so far, the study on "how to use C language to push the box" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.