How to use C language to realize the effect of pushing boxes in game projects
This article mainly explains "how to use C language to achieve the effect of pushing boxes in game projects". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to use C language to achieve the game project push box effect" bar!
Let's get started.
First of all, we draw this map with specific numbers in a two-dimensional array.
Int cas = 0 Int map [3] [8] [8] = {1pyrone [3] [8] [8] = {1memorie [3] [8] [8] = {1memorie] [3] [8] [8] = {1pyrrone 1] [8] [8] = {1meme 1] [8] [8] = {1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,1, 1,0,1,3,0,1,0,1, 1,0,1,4,0,1,0,1, 1,3,4,5,0,0,0,1, 1,0,1,0,0,1,0,1, 1,3,4,0,0,4,3,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,3,4,0,0,4,3,1, 1,0,1,3,0,1,0,1, 1,0,1,4,0,1,0,1, 1,3,4,5,0,4 3,1, 1,0,1,0,0,1,0,1, 1,3,4,0,0,4,3,1, 1,1,1,1,1,1,1,1}
Then let's draw the map of our push box.
Void drawGraph () {for (int I = 0; I < 8; iSum +) {for (int j = 0; j < 8; Jake +) {/ / calculate the coordinates of the map int x = 50 * j; int y = 50 * I Switch (map [cas] [I] [j]) {case 0: / / one Chinese character symbol occupies two positions / / printf (") Putimage (x, y, img + 0); break; case 1: putimage (x, y, img + 1); / / printf ("■"); break Case 3: putimage (x, y, img + 2); / / printf ("☆"); break; case 4: putimage (x, y, img + 3) / / printf ("★"); break; case 5: case 8: putimage (x, y, img + 4); / / printf ("people") Break; case 7: putimage (x, y, img + 5); / / printf ("●"); break } / / printf ("\ n");}}
And then there's our game function, how to use what buttons to control our characters.
Void keyDown () {int userKey = _ getch (); / / invisible input / / location: find the location of the person int I = 0; int j = 0; for (I = 1; I < 8; iTunes +) {for (j = 1; j < 8) ) {if (map [cas] [I] [j] = = 5 | | map [cas] [I] [j] = = 8) {goto NEXT } NEXT: / / what button do we use to play switch (userKey) {case'Waring: case'wicked: case 72: if (map [cas] [I-1] [j] = 0 | map [cas) ] [I-1] [j] = 3) {map [cas] [I] [j]-= 5 Map [cas] [I-1] [j] + = 5 } if (map [cas] [I-1] [j] = = 4 | | map [cas] [I-1] [j] = = 7) {if (map [cas] [I-2] [j] = 0 | | map [cas] [I-2] [j] = = 3) { Map [cas] [I] [j]-= 5 Map [cas] [I-1] [j] + = 1; map [cas] [I-2] [j] + = 4;} break Case's accounts: case's servers: case 80: if (map [cas] [I + 1] [j] = 0 | | map [cas] [I + 1] [j] = 3) {map [cas] [I] [j]-= 5; map [cas] [I + 1] [j] + = 5 } if (map [cas] [I + 1] [j] = = 4 | | map [cas] [I + 1] [j] = = 7) {if (map [cas] [I + 2] [j] = 0 | | map [cas] [I + 2] [j] = = 3) { Map [cas] [I] [j]-= 5 Map [cas] [I + 1] [j] + = 1; map [cas] [I + 2] [j] + = 4;}} break Case'asides: case'Aids: case 75: if (map [cas] [I] [j-1] = 0 | | map [cas] [I] [j-1] = = 3) {/ / axiang1 a=a+1 compound assignment operator map [cas] [I] [j]-= 5 Map [cas] [I] [j-1] + = 5 } if (map [cas] [I] [j-1] = = 4 | | map [cas] [I] [j-1] = = 7) {if (map [cas] [I] [j-2] = 0 | | map [cas] [I] [j-2] = = 3) { Map [cas] [I] [j]-= 5 Map [cas] [I] [j-1] + = 1; map [cas] [I] [j-2] + = 4;}} break Case'dcards: case'Downs: case 77: if (map [cas] [I] [j + 1] = 0 | | map [cas] [I] [j + 1] = 3) {map [cas] [I] [j]-= 5; map [cas] [I] [j + 1] + = 5 } if (map [cas] [I] [j + 1] = = 4 | | map [cas] [I] [j + 1] = = 7) {if (map [cas] [I] [j + 2] = 0 | | map [cas] [I] [j + 2] = = 3) { Map [cas] [I] [j]-= 5 Map [cas] [I] [j + 1] + = 1; map [cas] [I] [j + 2] + = 4;}} break;}}
And then there is how we judge the outcome of the game.
/ / judgment of victory or defeat: int gameOver () {/ / for (int I = 0; I < 8; iTunes +) {for (int j = 0; j < 8) without boxes on the map Return +) {if (map [cas] [I] [j] = = 4) {return 0;} return 1;}
Finally, just run our main function.
Int main () {loadResource (); mciSendString ("open 1.mp3", 0,0,0); mciSendString ("play 1.mp3 repeat", 0,0,0); initgraph (50 * 8,50 * 8); while (1) {drawGraph () If (gameOver ()) {cas++; / / switch level if (cas==3) break;} keyDown (); / / system ("cls") } closegraph (); / / printf ("GameOver!\ n"); return 0;} at this point, I believe you have a deeper understanding of "how to use C language to achieve the effect of pushing boxes for game projects". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!