我有一个类Grid
,它管理所有地图功能。问题是,pacman 地图逆时针旋转了 90 度。
它看起来如何
它应该是什么样子
我通过换grid[x][y]
到grid[y][x]
里面得到了“固定”版本isWall()
(一种不整洁、不正确的方法)
这是Grid
该类的完整代码;
package com.jackwilsdon.pacman.game;
import org.newdawn.slick.Graphics;
public class Grid {
public static final int BLOCK_SIZE = 20;
public int[][] grid = null;
public Grid()
{
grid = new int[][] { {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1,0},
{0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0},
{0,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,0},
{0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0},
{0,1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,0},
{0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0},
{0,1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0},
{0,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0} };
}
public boolean isWall(int x, int y)
{
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length)
{
return grid[y][x] == 1;
}
return true;
}
public void draw(Graphics g)
{
for (int cX = 0; cX < grid.length; cX++)
{
for (int cY = 0; cY < grid[cX].length; cY++)
{
if (this.isWall(cX, cY))
{
g.fillRect(cX*Grid.BLOCK_SIZE, cY*Grid.BLOCK_SIZE, Grid.BLOCK_SIZE, Grid.BLOCK_SIZE);
}
}
}
}
}
我在代码中犯了一个愚蠢的错误吗?
我不想切换 x 和 y,因为这不再是二维数组的正确格式。