Labyrinth and collision with walls

Hey guys !
As an homework, my friend and me have to create a labyrinth(a basic one)
With a starting and an ending. At the moment almost everything is working perfectly but the problem we met is about the collision with the walls. Our teacher told us we could try to solve this problem with the color detection but it’s f***ing hard. Do u have any other idea or can u try to explain us how to do with this method.
ps: Our laby is in black and white and i’m french.

here’s the code :

PImage labyFond;
float x;
float y;
PImage[] Yoshi = new PImage[7];

void setup() 
{
  size(1024,1024);
  frameRate(30);
  labyFond = loadImage("laby.png");
  
  Yoshi[0] = loadImage( "Yoshi0.gif" );
  Yoshi[1] = loadImage( "Yoshi1.gif" );
  Yoshi[2] = loadImage( "Yoshi2.gif" );
  Yoshi[3] = loadImage( "Yoshi3.gif" );
  Yoshi[4] = loadImage( "Yoshi4.gif" );
  Yoshi[5] = loadImage( "Yoshi5.gif" );
  Yoshi[6] = loadImage( "Yoshi6.gif" );
  x=-5;
  y=117;
}

void draw() 
{
 background(labyFond);
 
 image( labyFond, 0, 0, width, height );
 image( Yoshi[frameCount%1], x, y );
}

float pixels;
float nl;
float nc;
float t;


void keyPressed(){
  if (key==CODED){
    if (keyCode==UP){
        
      y = y-2; // notre Yoshi avance vers le haut
      image( labyFond, 0, 0, width, height );
      image( Yoshi[frameCount%7], x, y );
    }
    if (keyCode==DOWN){
      y = y+2; // notre Yoshi avance vers le bas
      image( labyFond, 0, 0, width, height );
      image( Yoshi[frameCount%7], x, y );
    }
    if (keyCode==LEFT){
      x = x-2; // notre Yoshi avance vers la gauche
      image( labyFond, 0, 0, width, height );
      image( Yoshi[frameCount%7], x, y );
    }
    if (keyCode==RIGHT){
      x = x+2; // notre Yoshi avance vers la droite
        image( labyFond, 0, 0, width, height );
        image( Yoshi[frameCount%7], x, y );
    }
  }
}
1 Like

I would suggest using a separate method to check the pixels array in the labyFond image. Try something like this.

// in setup include if labyFond does not change
// if labyFond changes then include it in the isValidLocation() method
labyFond.loadPixels();

boolean isValidLocation(x, y) {
  if (labyFond.pixels[x + y * width] == color(255)) { // assuming walls are black if the walls are white change to color(0)
    return true;
  }
  return false;
}

// then change your all your keyPressed if statements to something like this
if (keyCode == DOWN && isValidLocation(x, y - 2)) {
  // all the same code
}
1 Like

It says that there is an error on the boolean but i understand the code idk why

Could you post your updated code?

Sorry i didn’t see these 2 lines in ur answer :
// in setup include if labyFond does not change
// if labyFond changes then include it in the isValidLocation() method
probably because of that

1 Like