In the process of moving the maze game genie, they cannot pass through but are blocked. How to Design the maze?
The preliminary design code is as follows:
Use the identification tool to determine the top, bottom, and left sides of the image wall in each array.
For example, you can determine the size between the right side of the genie and the left side of the wall. You can also determine whether the genie's head is blocked or whether the genie's feet are blocked.
Judge the situation on the left, the situation on the top, and the situation on the bottom.
Package com. swift; import java. awt. dimension; import java. awt. point; import java. awt. event. keyEvent; import com. rupeng. game. gameCore; public class Migong implements Runnable {public static void main (String [] args) {GameCore. start (new Migong () ;}@ Override public void run () {GameCore. setGameSize (805,590); GameCore. setGameTitle ("image maze game-2D array creation"); GameCore. loadBgView ("bg.png"); // a two-dimensional array is assigned with 10 rows and 10 columns. The width of each image is 80*56 int [] [] arr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, {0, 0, 0, 0, 0, 0, 0, 0}, {, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }}; // counter int num = 0; // generate an image wall for (int I = 0; I <arr. length; I ++) {for (int j = 0; j <arr [I]. length; j ++) {if (arr [I] [j] = 1) {GameCore. createImage (num); GameCore. setImageSource (num, "1.png"); GameCore. setImagePosition (num, j * 80, I * 56); num ++;} if (arr [I] [j] = 2) {GameCore. createImage (num); GameCore. setImageSource (num, "2.png"); GameCore. setImagePosition (num, j * 80, I * 56); num ++;} if (arr [I] [j] = 4) {GameCore. createImage (num); GameCore. setImageSource (num, "4.png"); GameCore. setImagePosition (num, j * 80, I * 56); num ++ ;}}// sprite int spriteGirl = 0; GameCore. createSprite (spriteGirl, "butterfly"); GameCore. playSpriteAnimate (spriteGirl, "fly", true); GameCore. setSpritePosition (spriteGirl, 100, 20); // get the height and width of the female genie Dimension dGirl = GameCore. getSpriteSize (spriteGirl); int girlHeight = dGirl. height; int girlWidth = dGirl. width; for (;) {int codeNum = GameCore. getPressedKeyCode (); Point positionG = GameCore. getSpritePosition (spriteGirl); // you can specify the int girlUpY = positionG. y; int girlDownY = positionG. y + girlHeight; // you can obtain the int girlLeftX = positionG on the left and right of the female. x; int girlRightX = positionG. x + girlWidth; if (codeNum = KeyEvent. VK_UP | codeNum = KeyEvent. VK_W) {if (positionG. y> 30) {GameCore. setSpritePosition (spriteGirl, positionG. x, -- positionG. y); GameCore. pause (11) ;}} if (codeNum = KeyEvent. VK_DOWN | codeNum = KeyEvent. VK_S) {if (positionG. y< 550) {GameCore. setSpritePosition (spriteGirl, positionG. x, ++ positionG. y); GameCore. pause (11) ;}} if (codeNum = KeyEvent. VK_LEFT | codeNum = KeyEvent. VK_A) {if (positionG. x> 0) {GameCore. setSpriteFlipX (spriteGirl, true); GameCore. setSpritePosition (spriteGirl, -- positionG. x, positionG. y); GameCore. pause (2) ;}} if (codeNum = KeyEvent. VK_RIGHT | codeNum = KeyEvent. VK_D) {boolean hasRightWall = false; for (int I = 0; I <arr. length; I ++) {for (int j = 0; j <arr [I]. length; j ++) {if (arr [I] [j] = 0) {continue;} // you can obtain the left and right positions of the wall, int imageLeftX = 80 * j; int imageRightX = 80 * (j + 1); // obtain the top and bottom positions of the wall. int imageUpY = 56 * I; int imageDownY = 56 * (I + 1 ); if (girlRightX> = imageLeftX & (girlUpY <= imageDownY & girlUpY> = imageUpY | girlDownY> = imageUpY & girlDownY <= imageDownY )) {hasRightWall = true ;}} if (positionG. x <800 & hasRightWall = false) {GameCore. setSpriteFlipX (spriteGirl, false); GameCore. setSpritePosition (spriteGirl, ++ positionG. x, positionG. y); GameCore. pause (1 );}}}}}
Replace it with a simple idea:
Method of distance determination
Package com. swift; import java. awt. dimension; import java. awt. point; import java. awt. event. keyEvent; import com. rupeng. game. gameCore; public class Migong implements Runnable {public static void main (String [] args) {GameCore. start (new Migong () ;}@ Override public void run () {GameCore. setGameSize (805,590); GameCore. setGameTitle ("image maze game-2D array creation"); GameCore. loadBgView ("bg.png"); // a two-dimensional array is assigned with 10 rows and 10 columns. The width of each image is 80*56 int [] [] arr = {0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0 }}; // counter int num = 0; // generate an image wall for (int I = 0; I <arr. length; I ++) {for (int j = 0; j <arr [I]. length; j ++) {if (arr [I] [j] = 1) {GameCore. createImage (num); GameCore. setImageSource (num, "1.png"); GameCore. setImagePosition (num, j * 80, I * 56); num ++;} if (arr [I] [j] = 2) {GameCore. createImage (num); GameCore. setImageSource (num, "2.png"); GameCore. setImagePosition (num, j * 80, I * 56); num ++;} if (arr [I] [j] = 4) {GameCore. createImage (num); GameCore. setImageSource (num, "4.png"); GameCore. setImagePosition (num, j * 80, I * 56); num ++ ;}}// sprite int spriteGirl = 0; GameCore. createSprite (spriteGirl, "butterfly"); GameCore. playSpriteAnimate (spriteGirl, "fly", true); GameCore. setSpritePosition (spriteGirl, 100, 20); // get the height and width of the female genie Dimension dGirl = GameCore. getSpriteSize (spriteGirl); int girlHeight = dGirl. height; int girlWidth = dGirl. width; for (;) {int codeNum = GameCore. getPressedKeyCode (); Point positionG = GameCore. getSpritePosition (spriteGirl); // you can specify the int girlUpY = positionG. y; int girlDownY = positionG. y + girlHeight; int girlCenterY = (girlUpY + girlDownY)/2; // you can obtain the position int girlLeftX = positionG on the left and right of the female. x; int girlRightX = positionG. x + girlWidth; int girlCenterX = (girlLeftX + girlRightX)/2; if (codeNum = KeyEvent. VK_UP | codeNum = KeyEvent. VK_W) {if (positionG. y> 30) {GameCore. setSpritePosition (spriteGirl, positionG. x, -- positionG. y); GameCore. pause (11) ;}} if (codeNum = KeyEvent. VK_DOWN | codeNum = KeyEvent. VK_S) {if (positionG. y< 550) {GameCore. setSpritePosition (spriteGirl, positionG. x, ++ positionG. y); GameCore. pause (11) ;}} if (codeNum = KeyEvent. VK_LEFT | codeNum = KeyEvent. VK_A) {if (positionG. x> 0) {GameCore. setSpriteFlipX (spriteGirl, true); GameCore. setSpritePosition (spriteGirl, -- positionG. x, positionG. y); GameCore. pause (2) ;}} if (codeNum = KeyEvent. VK_RIGHT | codeNum = KeyEvent. VK_D) {boolean hasRightWall = false; for (int I = 0; I <arr. length; I ++) {for (int j = 0; j <arr [I]. length; j ++) {if (arr [I] [j] = 0) {continue;} // you can obtain the left and right positions of the wall, int imageLeftX = 80 * j; int imageRightX = 80 * (j + 1); int imageCenterX = (imageLeftX + imageRightX)/2; // obtain the top and bottom positions of the wall. int imageUpY = 56 * I; int imageDownY = 56 * (I + 1); int imageCenterY = (imageUpY + imageDownY)/2; int distance = (int) (Math. sqrt (Math. pow (girlCenterX-imageCenterX), 2) + Math. pow (girlCenterY-imageCenterY), 2); if (distance <100) {hasRightWall = true ;}} if (positionG. x <800 & hasRightWall = false) {GameCore. setSpriteFlipX (spriteGirl, false); GameCore. setSpritePosition (spriteGirl, ++ positionG. x, positionG. y); GameCore. pause (1 );}}}}}