因此,目前我正在制作迷宫游戏,当然,它是一个迷宫游戏,它必须有墙壁。墙壁需要阻止玩家越过他们。我在检查碰撞时遇到问题。它在一些地方有效,而其他地方则不适用。我目前的方法是通过我的二维数组,通过将他们当前的x和y除以50,然后使用这两个数字来尝试查看玩家是否会碰撞墙或不。正在发生的事情是,它阻止了玩家移动到某些墙上,有些则没有。此外,它还会阻止玩家在没有墙的地方(值为2的值)。我觉得有些东西正在与数学混淆,但我无法弄清楚什么。下面是我如何制作迷宫,伴随着阵列它正在从制造代码:试图通过2d数组,并没有得到正确的值?
private int[][] mazeWalls = { //top of the maze
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
{1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
{1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
{1, 1, 1, 1, 2, 2, 2, 2, 2, 1},
/*left side*/{1, 2, 2, 2, 2, 2, 2, 2, 2, 1}, //right side
{1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
{1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
{1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
//bottom of the maze
};
public void paintMaze(Graphics g){
for(int row = 0; row < mazeWalls.length; row++){ //example of loops
for(int col = 0; col < mazeWalls[row].length; col++){ //getting the positions set up
if(mazeWalls[row][col] == 1){
g.setColor(Color.RED); //color of the walls
g.fillRect(col * 50, row * 50, 50, 50); //col times 50 is the x coordinate, and row times 50 is the y coordinate
}
}
}
}
这里是我如何在同一个班级检查碰撞的代码:
public void collisionChecker(int playerX, int playerY){ //takes the user's location and then checks if they are running into the wall
if(mazeWalls[playerX/50][playerY/50] == 1){
setCollision(true);
}
else if(mazeWalls[playerX/50][playerY/50] != 1){
setCollision(false); //just in case
}
}
我觉得它应该起作用,但由于某种原因(我认为这是用分割玩家坐标之后的数字来表示),但事实并非如此。
我相信'playerX'和'playerY'是用户点击的坐标,然后你试图获得该rectanagle左上角的cooridnates来确定是否碰撞。对? –
playerX和playerY的实际值是多少,它们总是可以被50整除?如果不是,那可能是问题。 – Chizzle