Android java canvas surfaceview中,对象在小于或大于设置的最小值的地方得到一个值

fykwrbwg  于 2023-05-21  发布在  Android
关注(0)|答案(1)|浏览(169)

我正在使用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……

zphenhs4

zphenhs41#

在snakeX[0] = 0中插入0;

if (snakeX[0] > numblocksWide) {
            ***snakeX[0] = 0;***
        } else if (snakeX[0] < 0) {
            snakeX[0] = numblocksWide;
        }
...

那么在这里你减少到-1

case WEST:
                snakeX[0]--;
                angle = 180;
                break;

为了解决这个问题,将你的条件带到函数的末尾:

if (snakeX[0] > numblocksWide) {
            snakeX[0] = 0;
        } else if (snakeX[0] < 0) {
            snakeX[0] = numblocksWide;
        }

可能会没事:

public void updateSnake() {

    //every 100 ms or 1s the score increment
    long elapse = (System.nanoTime() - startTime) / 1000000;
    if (elapse > speed) {



        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;
        }

        //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;
        }

        startTime = System.nanoTime();
    }

}

相关问题