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 shows you "how to use C language to achieve push box game", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use C language to push box game" this article.
1. Preface
Pushing box is a classic project of c language.
two。 Game effect display
3. Project analysis
Game elements:
Open space 0
Wall 1
Role 2
Box 3
Victory point 4
Game purpose:
The player controls the character to move in the open space of the map, pushing the boxes to avoid obstacles and pushing all the boxes to the victory point.
4. Map implementation 4.1 storing maps
First of all, we use arrays to store maps. Why do we use char instead of int? Because int is generally 4 bytes and char is 1 byte, we can greatly reduce the memory needed to store maps. If you want to write multiple maps, just expand the array to three dimensions, which the author will explain in a later article.
Char map [10] [10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 1} 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}} 4.2 print a map
Corresponding to the printing of the map, we need to traverse the map and use the switch statement to print the map on the console.
For (size_t I = 0; I < 10; iTunes +) {for (size_t j = 0; j < 10; jacks +) {switch (map [I] [j]) {case 0: printf (""); / / Note that there are two spaces. If it is a space, it will cause some problems in map printing break. Case 1: printf ("█"); break; case 2: printf ("♀"); break; case 3: printf ("●"); break; case 4: printf ("☆") Break; default: break;}} printf ("\ n");} 5 Control character movement 5.1 find the controlled character int posX = 0, posY = 0; for (size_t I = 0; I < 10; iSum +) {for (size_t j = 0; j < 10; jake +) {if (2 = = map [I] [j] | 2 + 4 = = map [I] [j]) {posX = I; posY = j; break / / find the role Record the coordinates of its x-axis and y-axis} 5.2 to achieve movement / / control (keyboard: WSAD (top, bottom, left and right) / / need to get keys (characters) switch (getch ()) / / getch () this method requires importing # include {case'walled: case'worth: printf ("up\ n") at the beginning. / * upward logic: change the value of array elements for human reference above may be: open space move wall motionless success point move box to determine what is on top of the box. The wall does not move, the other box does not move * / / above is an open space or success point map [posX] [posY]: the position of the protagonist if (0 = = map [posX-1] [posY] | | 4 = = map [posX-1] [posY]) {/ / the current position person leaves map [posX] [posY]-= 2 / / people from above come to map [posX-1] [posY] + = 2 } / / above is the box (push the box away from the point) else if (3 = = map [posX-1] [posY] | | 3 + 4 = = map [posX-1] [posY]) {/ / there is open space or success point map [posX-1] [posY]: box position if (0 = = map [posX-2]] [ PosY] | | 4 = = map [posX-2] [posY]) {/ / the current location person leaves map [posX] [posY]-= 2 / / people from the upper position come to map [posX-1] [posY] + = 2; / / the upper box leaves map [posX-1] [posY]-= 3; / / the upper box above comes over map [posX-2] [posY] + = 3 }} break; case's songs: case'slots: printf ("downward\ n"); break; case'asides: case'Aids: printf ("left\ n"); break; case'dflowers: case'Downs: printf ("right\ n"); break; default: break } 6. Judge victory
Traversing the map, if there is no box, it can be judged to be a victory. (size_t is something in C++. Size_t is equivalent to unsigned int. You can only understand it here. You can use int instead of size_t.)
Bool isWin () {for (size_t I = 0; I < 10; iTunes +) {for (size_t j = 0; j < 10; jacks +) {if (map [I] [j] = = 3) {return false;} return true } the above is all the contents of the article "how to use C language to realize the game of pushing boxes". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.