前提引入简单的二维数组知识点:
package com.meiyvluo;
public class Array {
public static void main(String[] args) {
int [][] a ={
{1,2,3,4},
{3,4,5,6,7},
{1,2,5,7,3}
};
System.out.println(a.length);//二维数组的个数
for(int i = 0;i<a.length;i++){
int[] b = a[i];
for(int j=0;j<b.length;j++){
//System.out.print(b[j]+" ");
System.out.print(a[i][j]+" ");//效果同上一行
}
System.out.println();
}
}
}
自行了解实体类,还有set和get方法
package com.meilingluo.TXZ_2;
import java.util.Arrays;
public class LevelData {//游戏关卡数据
private String boxImgPath;//箱子图片
private int boxLocation[];//箱子初始位置
private String goalImgPath;//目的地图片
private int []goalLocation;//目的地位置
private String[] workerImgPaths;//柔柔四张图片
private int[] workerLocation;//柔柔地址
private int[][] wallsLocations;//墙的位置
private String wallImgPath;//墙图片
@Override
public String toString() {
return "LevelData{" +
"boxImgPath='" + boxImgPath + '\'' +
", boxLocation=" + Arrays.toString(boxLocation) +
", goalImgPath='" + goalImgPath + '\'' +
", goalLocation=" + Arrays.toString(goalLocation) +
", workerImgPaths=" + Arrays.toString(workerImgPaths) +
", workerLocation=" + Arrays.toString(workerLocation) +
", wallsLocations=" + Arrays.toString(wallsLocations) +
", wallImgPath='" + wallImgPath + '\'' +
'}';
}
public String getWallImgPath() {
return wallImgPath;
}
public void setWallImgPath(String wallImgPath) {
this.wallImgPath = wallImgPath;
}
public void setWallsLocations(int [][] wallsLocations){
this.wallsLocations = wallsLocations;
}
public int[][] getWallsLocations(){
return wallsLocations;
}
public int[] getWorkerLocation() {
return workerLocation;
}
public void setWorkerLocation(int[] workerLocation) {
this.workerLocation = workerLocation;
}
public void setWorkerImgPaths(String [] workerImgPaths){
this.workerImgPaths = workerImgPaths;
}
public String[] getWorkerImgPaths(){
return workerImgPaths;
}
public String getGoalImgPath() {
return goalImgPath;
}
public void setGoalImgPath(String goalImgPath) {
this.goalImgPath = goalImgPath;
}
public int[] getGoalLocation() {
return goalLocation;
}
public void setGoalLocation(int[] goalLocation) {
this.goalLocation = goalLocation;
}
public void setBoxImgPath(String boxImgPath){
this.boxImgPath = boxImgPath;
}
private String getBoxImgPath(){
return boxImgPath;
}
public void setBoxLocation(int []boxLocation){
this.boxLocation=boxLocation;
}
public int []getBoxLocation(){
return boxLocation;
}
}
package com.meilingluo.TXZ_2;
public class LevelDataManager {
public static LevelData getNowLevelData(int index){
LevelData levelData = new LevelData();
if(index == 1){
levelData.setBoxImgPath("box3.png");
int [] boxLocation = new int[]{3,3};
levelData.setBoxLocation(boxLocation);
levelData.setGoalImgPath("goal3.png");
levelData.setGoalLocation(new int[] {7,8});
String [] workerImgPaths = {"workerLeft4.png","workerUp4.png","workerDown4.png","workerRight4.png"};
levelData.setWorkerImgPaths(workerImgPaths);
levelData.setWallImgPath("wall3.png");
int [][] wallsLocations = {
{2, 3}, {3, 3}, {4, 3}, {5, 3},{6, 3},{7, 3},
{4, 5}, {5, 6}, {6, 7}, {7, 8},
{15, 3}, {15, 4}, {15, 5},{15, 6}
};
levelData.setWallsLocations(wallsLocations);
}
else if(index == 2){
levelData.setBoxImgPath("box2.png");
int [] boxLocation = new int[]{3,3};
levelData.setBoxLocation(boxLocation);
levelData.setGoalImgPath("goal2.png");
levelData.setGoalLocation(new int[] {7,8});
String [] workerImgPaths = {"workerLeft4.png","workerUp4.png","workerDown4.png","workerRight4.png"};
levelData.setWorkerImgPaths(workerImgPaths);
levelData.setWallImgPath("wall3.png");
int [][] wallsLocations = {
{2, 3}, {3, 3}, {4, 3}, {5, 3},{6, 3},{7, 3},
{4, 5}, {5, 6}, {6, 7}, {7, 8},
{15, 3}, {15, 4}, {15, 5},{15, 6}
};
levelData.setWallsLocations(wallsLocations);
}
else if(index == 3){}
else {
return null;
}
return levelData;
}
}
package com.meilingluo.TXZ_2;
public class Run {
public static void main(String[] args) {
LevelData levelData =LevelDataManager.getNowLevelData(2);
System.out.println(levelData);
}
}