Code issue\ A bug that killed me (P5)

Hello friends,
I created a simple game that has an annoying bug that I can not solve /
may be simple for some of you but my brain has become mashed and I can no longer think of a logical solution.

So the goal in the game is to eat as many white squares per minute and be careful of the red squares, eating three red squares will end the game. Navigation is performed using the arrow keys on the keyboard.
Now, if you invest a minute or two you will see that my test of Collusion not always working.
its in lines 217 (for good squeres) and 251 (for the bad ones).

I would really appreciate from the bottom of my heart for anyone who can take a look.
Thanks!

specific function (what am i missing here?)

    //CHECK COLLUSION FUNCTION
    this.checkCollusion = function(player) {
        if (this.pos.x >= player.pos.x && this.pos.x <= player.pos.x + player.size &&
            this.pos.y >= player.pos.y && this.pos.y <= player.pos.y + player.size ||
            this.pos.x + this.size >= player.pos.x && this.pos.x + this.size <= player.pos.x + player.size &&
            this.pos.y + this.size <= player.pos.y + player.size && this.pos.y >= player.pos.y) {
            return true;
        } else {
            return false;
        }

code:
https://editor.p5js.org/royjan/sketches/u0qZkJjtd

1 Like

Hey there !

Hm your collision seems okay as in the if statement on how it is being called so I see no issue there, but I did notice you had trouble finishing the game ?

So I made a simple solution on here to show you that.

But your collision seems okay, could you demonstrate more what could you mean that is wrong with the collision ?

Thanks so much for the help!
I actually put the part that ends the game in comment because I did not want the game to stop
while someone was trying to figure out the bug.
But thanks again for the great intent!

Just while I try to β€œeat” as many squares as possible, sometimes I pass right over another square,
and obviously they share the same surface area but nothing happens. (My partner also noticed this so I realized that I neither imagine nor can I ignore it :blush:)

[image]