public TileMap loadMap(String filename) throws IOException {
lines = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(filename));//读取地图文件
while (true) {
String line = reader.readLine();//(解析)读取一行
if (line == null) {
reader.close();
break;
}
if (!line.startsWith("#")) {//当这一行开头不是 #时,存入lines链表中,
lines.add(line);
width = Math.max(width, line.length());
}
}
height = lines.size();//地图的高度,即链表的size
newMap = new TileMap(width, height);//地图封装类
return newMap;//以对象方式返回
}
简单说下这行代码,我们先结合地图文件说明,如下
I
IIIIIIIIIIIII
ABBBBBBBBBBBBB
这些个字母代表了地图中不同的障碍物,根据游戏的特色自行设置障碍物,这种设计给画图线程占据了很大时间,效率慢。所以,要考虑一种画图的缓存机制,一种叫双缓存,另一种叫交换链 ,java中的 BufferStrategy,效率比双缓存高。。。。。。。
如上程序实例图