地图实现

和一的部分差不多,我们需要新建一个面板内部类,用于构造地图。

其中的几个值在接口MapConfig中

 

/**
* 自定义内部游戏面板类
*
* @author 莫言情难忘
*
*/
class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < MapHeight / eleHeight; i++) {
for (int j = 0; j < MapWidth / eleWidth; j++) {
g.drawImage(GetGameImage(map1[i][j][0]), getDrawX(j), getDrawY(i), eleWidth, eleHeight, null);
}
}
g.drawImage(icon106.getImage(), getDrawX(playey), getDrawY(playex), eleWidth, eleHeight, null);
}

// 将数组下标转化成对应的图片左上角坐标
public int getDrawX(int j) {
int x = j * 50;
return x;
}

// 将数组下标转化成对应的图片左上角坐标
public int getDrawY(int i) {
int y = i * 50;
return y;
}
}

 

posted @ 2019-09-09 14:15  李艳艳665  阅读(90)  评论(0编辑  收藏  举报