Making a labyrinth

hello i need help in order to make a labyrinth I have already done all the part to make the ellipse move, now I want to create the labyrinth itself and i have 2 ideas : making a labyrinth with some lines or rect Or adding a background image of a labyrinth and detect the black pixels

here’s the code i have for now:

balle B1 = new balle(width/2,width-10,10);

void setup()
{
  size(600,600);
  background(255);
}

void draw()
{ 
  background(255);
  B1.render();
}

class balle {
  int x;
  int y;
  int r;
  
  balle(int x_,int y_, int r_){
  x = x_;
  y = y_;
  r = r_;
}

void render()
{
  if(keyPressed == true)
  {
    if(key == CODED)
    {
      if (keyCode == UP && (y-r/2)>0)
      {
        y--;
      }
      if (keyCode == DOWN && (y+r/2)<width)
      {
        y++;
      }
      if (keyCode == LEFT && (x-r/2>0))
      {
        x--;
      }
      if(keyCode == RIGHT && (x+r/2)<width)
      {
      x++;
      }
    }
  }
  ellipse(x,y,r,r);
}
}

thank’s for your help

1 Like

there is some ?rules? about the naming of variables,
and you just hit it!
better:

class Balle {....

Balle b1;

if you want go with the color check ( instead object collision )
need the color check inside your class ( instead the window size limit check)
and should do just before you walk.

    • there was a small mistake, you can not use width and height in the variable declaration / prior to setup size()
      so b1 position was wrong.

for example:

Balle b1;
color labcol = color(200, 0, 0);

void setup() {
  size(600, 600);
  b1 = new Balle(width /2, height /2, 10);
  background(255);
}

void draw() { 
  background(255);
  draw_lab();
  b1.render();
}

class Balle {
  boolean goUP, goDOWN, goLEFT, goRIGHT; 
  int x, y, r;
  Balle(int x_, int y_, int r_) {
    x = x_;
    y = y_;
    r = r_;
  }
  void render() {
    if ((keyPressed == true) && (key == CODED)) {
      color_check();
      if (keyCode == UP      && goUP )    y--;
      if (keyCode == DOWN    && goDOWN )  y++;
      if (keyCode == LEFT    && goLEFT )  x--;
      if (keyCode == RIGHT   && goRIGHT ) x++;
    }
    fill(200, 200, 0);
    stroke(0, 0, 200);
    strokeWeight(1);
    ellipse(x, y, r, r);
  }
  void color_check() {
    goUP=goDOWN=goLEFT=goRIGHT=true;
    if ( labcol == get(x, y-r/2 -1) ) goUP    = false;
    if ( labcol == get(x, y+r/2 +1) ) goDOWN  = false;
    if ( labcol == get(x-r/2 -1, y) ) goLEFT  = false;
    if ( labcol == get(x+r/2 +1, y) ) goRIGHT = false;
  }
}

void draw_lab() {
  stroke(labcol);
  strokeWeight(20);
  noFill();
  rect( 20, 20, width-40, height-40);
  //... MORE 
}


1 Like

okay thank’s i’ll try this !

i’ve done a code based on your’s and it’s working quite well ! thanks !
but I have still a problem that occure : when i’m on a corner (like on the first picture) and i go in this example down or right it glitch on the color because it check only for the ellipse 4 side but not in diagonal (like picture 2)
if you have any idea of how I could fix it ?
and I have another question can i put a different color on a specific part of a rect only (in order to do a path on the rect ) or shall I use some basic lines and create a void at this place or a different colored line ?

here’s the pictures and the code :
Captureexemple1 Captureexemple2

balle B1;
color labcol = color(0,0,0);


void setup()
{
  size(600,600);
  B1 = new balle(50,50,30);
  background(255);
  
}

void draw()
{
  background(255);
  draw_lab();
  B1.render(); 
}
  
  class balle {
    boolean goUP  , goDOWN , goLEFT , goRIGHT;
    int x , y , r;
    balle (int x_ ,int y_ , int r_){
      x = x_;
      y = y_;
      r = r_;
    }
    
    void render()
    {
      if(keyPressed == true && key == CODED)
      {
      colorcheck();
      if (keyCode == UP      && goUP )    y--;
      if (keyCode == DOWN    && goDOWN )  y++;
      if (keyCode == LEFT    && goLEFT )  x--;
      if (keyCode == RIGHT   && goRIGHT ) x++;
      }
      fill(0,255,0);
      stroke(0);
      strokeWeight(1);
      ellipse(x,y,r,r);
    }
    
    void colorcheck()
    {
      goUP=goDOWN=goLEFT=goRIGHT = true;
      if(labcol == get(x,y-r/2 -1) ) goUP = false;
      if(labcol == get(x,y+r/2 +1) ) goDOWN = false;
      if(labcol == get(x-r/2 -1,y) ) goLEFT = false;
      if(labcol == get(x+r/2 +1,y) ) goRIGHT = false;
    }
  }
  
void draw_lab()
{
  stroke(labcol);
  strokeWeight(20);
  noFill();
 rect(20,20,width-40,height-40);
 rect(70,70,width-140,height-140);
}

thank’s for the help.

1 Like

i see what you mean,
here a example with more ways, and doors and blocks

// https://discourse.processing.org/t/neep-help-for-a-labyrinth/6869/2
// v0.2 more lab eoth doors and blocks

Ball b;
color labcol = color(200, 0, 0);

void setup() {
  size(600, 600);
  b = new Ball(width /2, height-10, 15);
}

void draw() { 
  background(255);
  draw_lab();
  b.render();
}

class Ball {
  boolean goUP, goDOWN, goLEFT, goRIGHT; 
  int x, y, r;
  Ball(int x_, int y_, int r_) {
    x = x_;
    y = y_;
    r = r_;
  }
  void render() {
    if ((keyPressed == true) && (key == CODED)) {
      color_check();
      if (keyCode == UP      && goUP    ) y--;
      if (keyCode == DOWN    && goDOWN  ) y++;
      if (keyCode == LEFT    && goLEFT  ) x--;
      if (keyCode == RIGHT   && goRIGHT ) x++;
    }
    fill(100, 200, 0);
    stroke(100,200, 0);
    strokeWeight(1);
    ellipse(x, y, r, r);
  }
  void color_check() {
    goUP=goDOWN=goLEFT=goRIGHT=true;
    if ( labcol == get(x, y-r/2 -1) ) goUP    = false;
    if ( labcol == get(x, y+r/2 +1) ) goDOWN  = false;
    if ( labcol == get(x-r/2 -1, y) ) goLEFT  = false;
    if ( labcol == get(x+r/2 +1, y) ) goRIGHT = false;
  }
}

void draw_lab() {
  int dl=40;
  noFill();
  stroke(labcol);
  strokeWeight(1);
  rect(1,1,width-2,height-2);  
  strokeWeight(dl/2);
  for ( int l = 1; l <8; l++) rect( dl*l, dl*l, width-2*dl*l, height-2*dl*l);
  stroke(255);  //cut doors
  //stroke(255,200,0);      // test    
  for ( int l = 0; l <7; l +=2) rect( dl/2+dl+dl*l, dl+dl*l-1, dl/16, dl/16);  
  for ( int l = 0; l <6; l +=2) rect( dl/2+4*dl+dl*l-2, 2*dl+dl*l-1, dl/16, dl/16);  
  stroke(labcol);           // blocks
  //stroke(255,200,0);      // test    
  rect(dl/2+3*dl,dl/2+1*dl-1, dl/16, dl/16);  
                            // more
}

1 Like

how would i make the ellipse mve faster in this case?

Add and sub more to x and y in the key section