初步設計代碼如下: 使用判斷精靈頭腳與每個數組中圖片牆上下左右分別判斷 如精靈的右側與牆的左側判斷大小,同時判斷精靈的頭部是否被遮擋,精靈的腳部是否被遮擋 依次再判斷向左的情況,向上的情況,向下的情況 用簡單思路代替: 通過距離判定的方法 ...
初步設計代碼如下:
使用判斷精靈頭腳與每個數組中圖片牆上下左右分別判斷
如精靈的右側與牆的左側判斷大小,同時判斷精靈的頭部是否被遮擋,精靈的腳部是否被遮擋
依次再判斷向左的情況,向上的情況,向下的情況
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("圖片迷宮游戲—二維數組製作"); GameCore.loadBgView("bg.png"); //二維數組賦值10行10列,每張圖片寬度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,1,0,0,0,0}, {4,4,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,4,0,0,0}, {1,1,2,0,0,0,0,1,0,0}, {0,4,0,0,0,1,0,0,0,0}, {0,0,1,0,1,0,0,0,2,2}}; //計數器 int num=0; //根據元素的值在游戲視窗中生成圖片牆 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++; } } } // 女精靈 int spriteGirl = 0; GameCore.createSprite(spriteGirl, "butterfly"); GameCore.playSpriteAnimate(spriteGirl, "fly", true); GameCore.setSpritePosition(spriteGirl, 100, 20); //得到女精靈的高度和寬度 Dimension dGirl=GameCore.getSpriteSize(spriteGirl); int girlHeight=dGirl.height; int girlWidth=dGirl.width; for (;;) { int codeNum = GameCore.getPressedKeyCode(); Point positionG = GameCore.getSpritePosition(spriteGirl); //得到女精靈頭和腳的位置 int girlUpY=positionG.y; int girlDownY=positionG.y+girlHeight; //得到女精靈左側和右側的位置 int girlLeftX=positionG.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; } //得到牆的左側和右側位置 int imageLeftX=80*j; int imageRightX=80*(j+1); //得到牆的上邊和下邊位置 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); } } } } }
用簡單思路代替:
通過距離判定的方法
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("圖片迷宮游戲—二維數組製作"); GameCore.loadBgView("bg.png"); //二維數組賦值10行10列,每張圖片寬度80*56 int[][] arr = {{0,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,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,2,0,0,0,4,0,0,0}, {0,0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,1,0,0,0,0}, {0,0,1,0,1,0,0,0,2,2}}; //計數器 int num=0; //根據元素的值在游戲視窗中生成圖片牆 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++; } } } // 女精靈 int spriteGirl = 0; GameCore.createSprite(spriteGirl, "butterfly"); GameCore.playSpriteAnimate(spriteGirl, "fly", true); GameCore.setSpritePosition(spriteGirl, 100, 20); //得到女精靈的高度和寬度 Dimension dGirl=GameCore.getSpriteSize(spriteGirl); int girlHeight=dGirl.height; int girlWidth=dGirl.width; for (;;) { int codeNum = GameCore.getPressedKeyCode(); Point positionG = GameCore.getSpritePosition(spriteGirl); //得到女精靈頭和腳的位置 int girlUpY=positionG.y; int girlDownY=positionG.y+girlHeight; int girlCenterY=(girlUpY+girlDownY)/2; //得到女精靈左側和右側的位置 int girlLeftX=positionG.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; } //得到牆的左側和右側位置 int imageLeftX=80*j; int imageRightX=80*(j+1); int imageCenterX=(imageLeftX+imageRightX)/2; //得到牆的上邊和下邊位置 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); } } } } }