Making a labyrinth

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