我正在使用Android Studio开发一个游戏,这是一个蛇游戏,我使用Surfaceview。我的问题是我从我设置的最小值中得到的值小于1。
下面是我的update方法的代码,其中snake对象的验证击中了角落,应该将对象绘制到对面。
numlockswide = 30
numblockshigh = 16
minhigh = 2
maxhigh = numblocks高
public void updateSnake() {
//every 100 ms or 1s the score increment
long elapse = (System.nanoTime() - startTime) / 1000000;
if (elapse > speed) {
//checks if hits the border so it could come from the opposite border
//north and south
if (snakeX[0] > numblocksWide) {
snakeX[0] = 0;
} else if (snakeX[0] < 0) {
snakeX[0] = numblocksWide;
}
//east west
if(snakeY[0] > maxHigh){
snakeY[0] = minHigh;
}else if(snakeY[0] < minHigh){
snakeY[0] = maxHigh;
}
for (int i = snakeLength; i > 0; i--) {
// System.out.println("snake[i] = " + i + " bx loc " + snakeX[i] + " by loc " + snakeY[i]);
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
// System.out.println("snake[i] = " + i + " nbx loc " + snakeX[i] + " nby loc " + snakeY[i]);
}
switch (currentDirection) {
case EAST:
snakeX[0]++;
angle = 0;
break;
case WEST:
snakeX[0]--;
angle = 180;
break;
case SOUTH:
snakeY[0]++;
angle = 90;
break;
case NORTH:
snakeY[0]--;
angle = 270;
break;
}
startTime = System.nanoTime();
}
}
问题是我已经设置了最小值,如果它从左到右的方向是0,但我得到的值是-1,这就是为什么我不能吃苹果对象是在角落/边缘的边界。这是我第一次使用SurfaceView和Canvas,有没有人能帮我解决这个bug……
1条答案
按热度按时间zphenhs41#
在snakeX[0] = 0中插入0;
那么在这里你减少到-1
为了解决这个问题,将你的条件带到函数的末尾:
可能会没事: