gpt4 book ai didi

java - 简单 map 读取程序中的子字符串索引关闭

转载 作者:行者123 更新时间:2023-12-02 08:28:33 25 4
gpt4 key购买 nike

好吧,我正在尝试创建一个简单的 RPG 游戏,但我的 map 读取代码似乎已关闭。这是 map 阅读线:

Integer.parseInt(map.data.substring(Map.MAP_START + ((playerPOS - map.width)/2) - 1, Map.MAP_START + ((((playerPOS - map.width)/2) - 1) + map.dataLen)));

现在 map 中唯一的图 block 是 01 和 00,所以当我看到 10 时,我知道出了问题:

(playerPOS - map.width) = 34 playerPOS = 50 player.x = 2 player.y = 3 blah = 0
(playerPOS - map.width) = 18 playerPOS = 34 player.x = 2 player.y = 2 blah = 10

这是 map 读取代码:

public void init(GameContainer gc) throws SlickException {
map = new Map();
player = new Player();

keys = new boolean[ALL_KEYS];
for(int i = 0; i < ALL_KEYS; i++){
keys[i] = false;
}

file = new File("testmap.txt");

try {
fin = new FileInputStream(file);

bin = new BufferedInputStream(fin);
StringBuilder sb = new StringBuilder();
int ch = 0;

while ((ch=bin.read())!=-1) {
sb.append((char)ch);
}

map.data = sb.toString().replace(" ", "").replace("\n", "").replace("\r", "");
System.out.print(map.data);

map.width = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
map.dataOffs += map.dataLen;

map.height = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
map.dataOffs += map.dataLen;
}
catch (Exception e) {
e.printStackTrace();
}

tiles = new Image("tiles.png");
hero = new Image("hero.png");
}

这是 map 文件:

16 12
01 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

因此,如果需要更多信息,请告诉我。

最佳答案

我建议重新考虑 map 的数据结构(可能是二维数组)。在你的init方法,逐个 token 读取文本文件(假设每个 token 都以空格分隔)。这与您现在所做的类似,但您可以避免用空字符串替换空格。请参阅http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html

第一个标记字符串(在您的示例中为“16”)可以转换为 map.width就像你正在做的那样。同样,第二个 token 变为 map.height 。然后您可以通过以下方式循环遍历剩余的 token (伪代码):

for(int y = 0; y < map.height; ++y) {
for (int x = 0; x < map.width; ++x) {
mapArray[x][y] = nextTokenAsInteger;
}
}

然后,当你想知道 (x, y) 位置的房间是什么样子时,您只需访问mapArray[x][y] .

关于java - 简单 map 读取程序中的子字符串索引关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3972002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com