Subscribed unsubscribe Subscribe Subscribe

Ex_00_01_RandomWalker_OOP_Basic

nature_of_code

random walker
OOP basic

class Walker {
  int x;
  int y;
  
  int w = 1;

  Walker(){
    x = width/2;
    y = height/2;
  }

  void display() {
    stroke(0);
    point(x, y);
  }

  void step() {
    int choice = int(random(4)); // 0.0 to 3.9 // int()
    if (choice == 0) {
      x = x+w;
    }
    else if (choice == 1) {
      x = x-w;
    }
    else if (choice == 2) {
      y = y+w;
    }
    else if (choice == 3) {
      y = y-w;
    }
  }
}

Walker w; // walker objeect

void setup() {
  size (360, 360);
  w = new Walker();
  background(255);
}

void draw() {
  w.step();
  w.display();
}