samedi 24 octobre 2015

Why is the program not recoginizng that the two objects are intersecting?

I ran into a few problems while trying to make the game snake. My first problem was that i cant get the food square and the snake square to overlap perfectly. in other words, the food will always be a little bit off.

My second problem was that the program is sometimes not recognizing that the snake is intersecting the food. It would think that when the snake is just a little bit higher, then it would be intersecting.

My third and last problem is that whenever the program does see that the two objects are intersecting, it does not generate a second snake like it is supposed to in the traditional game.

this is the part where it is supposed to generate a new snake:

   public void paintSnake(Graphics g){
        g.setColor(Color.green);
        g.fillRect(food.x, food.y, food.width, food.height);

        for(int i = count; i <= count; i++){
            g.setColor(Color.red);
            g.fillRect(snakes.x - 25, snakes.y - snakes.width - (i*5), snakes.width, snakes.height);
        }
    }

This is where the program checks if the two objects intersect

 if (snakes.intersects(food) && snakes.y  == food.y && snakes.x == food.x){
        count ++;
        System.out.println(count);
        food.x = randx;
        food.y = randy - 10;
    }

The next part is from three separate places in the code. This first one Makes the coordinates for the food block:

    snakes = new Rectangle(WIDTH / 2, HEIGHT / 2, 20, 20);
    food = new Rectangle( snakes.x, snakes.y, snakes.width, snakes.height);

After the first time the snake intersects with the food bock, it generates random number to use it there:

    randx = (int) (Math.floor(Math.random()*80)*10);
    randy = (int) (Math.floor(Math.random()*80)*10);

the numbers are assigned to the food object in these two lines:

    food.x = randx;
    food.y = randy;




Aucun commentaire:

Enregistrer un commentaire