Processing库如何绘制游戏地图?
How to draw a game map by Processing library?
我要为我正在制作的游戏制作一张地图,但我不知道该怎么做。谁能给我一些指导或提示吗?谢谢! (我使用的库是 Processing。)
这是我的想法:
写一个txt文件来表示地图,例如:
AAAAAAA
A A
A B A
A A
AAAAAAA
//A represents trees; B represents the player; space represents grass
每个字母代表一个20*20像素的图块(png图片)。我不知道如何实现这样的事情...
我试过用loadImage()
加载每个tile,但是我只能把它们一个一个放在特定的位置(编码很多...)效率很低...
编辑:
谢谢大家的评论!我有了一些想法,但仍然停留在如何获取每一行的字符索引上。
我在网上查了很多,发现indexOf()会找到索引,但只能找到第一个。
例如,对上面的txt文件使用index = line.indexOf("A");
,它只会找到每一行中第一个“A”的索引。有什么方法可以解决这个问题吗?
可以读入txt文件,使用当前读入的字符数
线乘以纹理的宽度作为 loadImage() 的 X 坐标
读取的行数乘以纹理的高度作为 Y
协调。所以遍历你的 txt 文件的所有字符你会做
像这样:
PImage imgTree = loadImage("treeTexture.jpg");
PImage imgPlayer = loadImage("playerTexture.jpg");
PImage imgGrass = loadImage("grassTexture.jpg");
PImage imgMissing = loadImage("missingTexture.jpg");
PImage currentTexture;
String[] lines = loadStrings("map.txt");
for (int i = 0 ; i < lines.length; i++) //Looping through all lines. i stores the current line index
{
for (int j = 0; j < lines[i].length; j++) //Looping through all characters. j stores the current character index
{
if (lines[i].charAt(j) == "A") //A switch statement would be more efficent but I am not sure how processing works so I just wrote this as an example
{
currentTexture = imgTree;
}
else if (lines[i].charAt(j) == "B")
{
currentTexture = imgPlayer;
}
else if (lines[i].charAt(j) == " ")
{
currentTexture = imgGrass;
}
else //For safety reasons
{
currentTexture = imgMissing;
}
image(currentTexture, j * currentTexture.width, i * currentTexture.height);
}
}
我不完全确定处理是如何工作的,我也没有测试这段代码,所以请相应地使用。另请记住,根据处理的工作方式,读取的数据还可能在末尾包含不可见的行结束字符 (\n)。如果是这种情况,请像这样更改您的内部循环:
for (int j = 0; j < lines[i].length - 1; j++)
我要为我正在制作的游戏制作一张地图,但我不知道该怎么做。谁能给我一些指导或提示吗?谢谢! (我使用的库是 Processing。)
这是我的想法:
写一个txt文件来表示地图,例如:
AAAAAAA
A A
A B A
A A
AAAAAAA
//A represents trees; B represents the player; space represents grass
每个字母代表一个20*20像素的图块(png图片)。我不知道如何实现这样的事情...
我试过用loadImage()
加载每个tile,但是我只能把它们一个一个放在特定的位置(编码很多...)效率很低...
编辑:
谢谢大家的评论!我有了一些想法,但仍然停留在如何获取每一行的字符索引上。
我在网上查了很多,发现indexOf()会找到索引,但只能找到第一个。
例如,对上面的txt文件使用index = line.indexOf("A");
,它只会找到每一行中第一个“A”的索引。有什么方法可以解决这个问题吗?
可以读入txt文件,使用当前读入的字符数 线乘以纹理的宽度作为 loadImage() 的 X 坐标 读取的行数乘以纹理的高度作为 Y 协调。所以遍历你的 txt 文件的所有字符你会做 像这样:
PImage imgTree = loadImage("treeTexture.jpg");
PImage imgPlayer = loadImage("playerTexture.jpg");
PImage imgGrass = loadImage("grassTexture.jpg");
PImage imgMissing = loadImage("missingTexture.jpg");
PImage currentTexture;
String[] lines = loadStrings("map.txt");
for (int i = 0 ; i < lines.length; i++) //Looping through all lines. i stores the current line index
{
for (int j = 0; j < lines[i].length; j++) //Looping through all characters. j stores the current character index
{
if (lines[i].charAt(j) == "A") //A switch statement would be more efficent but I am not sure how processing works so I just wrote this as an example
{
currentTexture = imgTree;
}
else if (lines[i].charAt(j) == "B")
{
currentTexture = imgPlayer;
}
else if (lines[i].charAt(j) == " ")
{
currentTexture = imgGrass;
}
else //For safety reasons
{
currentTexture = imgMissing;
}
image(currentTexture, j * currentTexture.width, i * currentTexture.height);
}
}
我不完全确定处理是如何工作的,我也没有测试这段代码,所以请相应地使用。另请记住,根据处理的工作方式,读取的数据还可能在末尾包含不可见的行结束字符 (\n)。如果是这种情况,请像这样更改您的内部循环:
for (int j = 0; j < lines[i].length - 1; j++)