# Making a labyrinth

**URL:** <https://discourse.processing.org/t/making-a-labyrinth/6869>\
**Category:** Coding Questions\
**Created:** [December 24, 2018, 7:42am UTC](https://discourse.processing.org/t/making-a-labyrinth/6869 "2018-12-24T07:42:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![singenya](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@singenya](https://discourse.processing.org/u/singenya)\
**Post date:** [December 24, 2018, 7:42am UTC](https://discourse.processing.org/t/making-a-labyrinth/6869/1 "2018-12-24T07:42:01Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 24, 2018, 12:28pm UTC](https://discourse.processing.org/t/making-a-labyrinth/6869/2 "2018-12-24T12:28:55Z")

</div>

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

```auto
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:

```auto
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 
}

```

---

<div class="post-metadata">

**Author:** ![singenya](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@singenya](https://discourse.processing.org/u/singenya)\
**Post date:** [December 24, 2018, 2:25pm UTC](https://discourse.processing.org/t/making-a-labyrinth/6869/3 "2018-12-24T14:25:09Z")

</div>

okay thank’s i’ll try this !

---

<div class="post-metadata">

**Author:** ![singenya](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@singenya](https://discourse.processing.org/u/singenya)\
**Post date:** [December 25, 2018, 8:56am UTC](https://discourse.processing.org/t/making-a-labyrinth/6869/4 "2018-12-25T08:56:20Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/46f6658075e07b391ab5d6a375b643a5396fbaa3.png) ![Captureexemple2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cbc951f800f9db605b57f93a714529b537f4dbf8.png)

```auto
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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 25, 2018, 9:19am UTC](https://discourse.processing.org/t/making-a-labyrinth/6869/5 "2018-12-25T09:19:56Z")

</div>

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

```auto
// 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
}

```

---

<div class="post-metadata">

**Author:** ![bonjaminbanjer](https://avatars.discourse-cdn.com/v4/letter/b/90ced4/32.png) [@bonjaminbanjer](https://discourse.processing.org/u/bonjaminbanjer)\
**Post date:** [April 4, 2022, 6:00am UTC](https://discourse.processing.org/t/making-a-labyrinth/6869/6 "2022-04-04T06:00:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 4, 2022, 8:20am UTC](https://discourse.processing.org/t/making-a-labyrinth/6869/7 "2022-04-04T08:20:52Z")

</div>

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