所以我试图在画布上显示一个随机生成的迷宫。
计划结构:
我的问题是:如何在画布上正确显示迷宫?
在画布上绘制所有内容的方法:
词汇:
我刚才意识到我应该解释一下switch语句是如何工作的。基本上每个Tile-Object都有4个Wall-Objects。每个Wall-Object都有一个整数属性,范围为1-4,表示墙的位置。所以1 =顶部,2 =右,3 =底部,4 =左。其余部分应该可以理解。
public void drawMaze(Canvas canvas, Tile[][] maze) {
double tileLength = canvas.getWidth() / maze.length;
double tileHeight = canvas.getHeight() / maze[0].length;
for (int x = 0; x < maze.length; x++) {
for (int y = 0; y < maze[x].length; y++) {
gc.setFill(Color.BLACK);
gc.fillRect(x * tileLength, y * tileHeight, tileLength, tileHeight);
gc.setFill(Color.WHITE);
gc.fillRect(x * tileLength + tileLength * 0.01, y * tileHeight + tileHeight * 0.01, tileLength - tileLength * 0.02, tileHeight - tileHeight * 0.02);
//The following part should remove the lines
for(Wall w : maze[x][y].getWalls()) {
if(w.isOpen()) {
switch(w.getPosition()) {
case 1: gc.clearRect(x * tileLength + tileLength * 0.01, y * tileHeight - tileHeight * 0.8, tileLength - tileLength * 0.02, tileHeight - tileHeight * 0.02);
break;
case 2: gc.clearRect(x * tileLength + tileLength * 0.8, y * tileHeight + tileHeight * 0.01, tileLength - tileLength * 0.02, tileHeight - tileHeight * 0.02);
break;
case 3: gc.clearRect(x * tileLength + tileLength * 0.01, y * tileHeight + tileHeight * 0.8, tileLength - tileLength * 0.02, tileHeight - tileHeight * 0.02);
break;
case 4: gc.clearRect(x * tileLength - tileLength * 0.8, y * tileHeight + tileHeight * 0.01, tileLength - tileLength * 0.02, tileHeight - tileHeight * 0.02);
}
}
}
}
}
}
删除行之前网格:
迷宫显示错误: