javaandroidandroid-canvassurfaceview

Android java canvas surfaceview , the object is getting a value where it less or more than the set minimum value


i am using android studio for the development a game which is a snake game , i am using Surfaceview. my problem is that im getting value which is less than 1 from the min value i set.

here is my code for the update method where the validation of the snake object hits the corner and should draw the object to the opposite side.

numlockswide = 30

numblockshigh = 16

minhigh = 2

maxhigh = numblocks high

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

    }

problem is i alread set that the minimum value if its from left right direction is 0 but i am getting a value of -1 which is why i cant eat the apple object that is in the corner/edge of the borders. this is my firsttime using surfaceview and canvas ,would anyone please help me with this bug...


Solution

  • You insert 0 in snakeX[0] = 0;

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

    then here you decreased to -1

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

    To tackle the problem bring your condition to the end of your function:

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

    It will probably be OK:

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