迷宫课程:https://gist.github.com/gamblore/5945328
for(MazeEdge edge : mazeEdges) {
shapeRenderer.line(edge.sX, edge.sY, edge.eX, edge.eY);
}
收到效果:
期望效果:
Maze: _ _ _ _ _ _ Maze: |_ |___ _| Maze: | | ___| Maze: |_|_|_ _ | Maze: |_ ___|_ | Maze: | _| |_ | Maze: |_|___|_____|
虽然以图形方式获得所需的效果。
答案 0 :(得分:0)
免责声明,我没有很好地研究代码,而且我对bit运算符(&)不是很熟悉。
sX
,sY
,eX
和eY
似乎不是我的墙坐标。它们代表边缘,但不代表树木边缘或图形的边缘。您可以将它们视为连接。在显示方法中,您可以看到他附加" |"," ",或" _"基于N,E,S,W。
他使用位操作符使得这个非常令人困惑的imho。每个N,E,S,W得到一个比特值作为int。我不太了解二进制,但&运算符比较两个位值并返回一个位,其中两个位于同一位置都是1.所以1001& 1100 = 1000,因为在两个位中只有第一个是1。无论如何,我认为他在这里检查是否存在从网格位置到E(ast)或S(outh)的连接。
由于我不知道具体是什么,我们可以使用他的显示方法正确绘制它。但由于他只建造东西瓦,我们可以为此创建一个简单的方法。我们还需要一个tileSize。
int tileSize = 10;
//Method for drawing a east wall tile position x, y
private void drawEastWall(int x, int y)
{
shapeRenderer.line(
x * tileSize + tileSize,
y * tileSize,
x * tileSize + tileSize,
y * tileSize + tileSize
);
}
//Method for drawing a south wall on tile position x, y
private void drawSouthWall(int x, int y)
{
shapeRenderer.line(
x * tileSize,
y * tileSize,
x * tileSize + tileSize,
y * tileSize
);
}
现在替换" _" =南墙和" |" =东墙用我们的新方法。
for (int y = 0; y < mHeight; y++) {
StringBuilder sb = new StringBuilder();
sb.append('|');
drawEastWall(0, y);
//We did not loop x yet so we just filled 0 here.
for (int x = 0; x < mWidth; x++) {
int cell = mGrid[y][x];
if (cell == 0)
sb.append(' ');
//This represents no walls so we leave it
if ((cell & S) != 0) {
sb.append(' ');
} else {
sb.append('_'); //South wall
drawSouthWall(x,y);
}
if ((cell & E) != 0) {
if (((cell | mGrid[y][x+1]) & S) != 0) {
sb.append(' ');
} else {
sb.append('_'); //south wall
drawSouthWall(x, y);
}
} else {
sb.append('|'); //East wall
drawEastWall(x, y);
}
}
Gdx.app.log(TAG, sb.toString());
}
这就是全部!如果你实际上是在draw方法中调用它,你应该删除所有字符串构建器的东西,而不是每帧输出它。 Double for循环经常用于绘制2d tileset。这是一些让它更清晰的例子。
//The following represents a 10 x 10 grid. We traverse the grid from left to right and top to bottom.
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 10; x++)
{
//Here we could draw or change the grid or tile map. I prefer using the word tile map.
//if x = 4 and our tileWidth = 10 we now we should start drawing at x: 40 to x:50.
//when x = 5 we start drawing at x:50 to x:60, we do the same for y.
//Most of the time we have some kind of 2d array that holds a tile class.
//In a loop like this we can initialize and alter it
tileMap[x][y] = new Tile(waterTexture);
tileMap[x][y].tileTexture = forestTexture;
}
}