Random Walker with Motion (1st HW Assignment)

Nature of Code Random Walker from Colin Narver on Vimeo.

 

//Colin Narver
//HW Assignment 1
//Create a random walker with dynamic probabilities. 
//For example, can you give it a 50% chance of moving in the direction of the mouse?

Walker w;
PShape s;

void setup() {
  size(600,600);
  s = loadShape("bee.svg");
  // Create a walker object
  w = new Walker();

}

void draw() {

  if (w.is50==true){    //blink red at each frome that is50 is true, else fill white
   fill(255,0,0); 
  } else {
    fill(255);
  }
  ellipse(150,150,20,20);
  frameRate(15);
  w.step();
  w.render();
}

class Walker {
  int x, y;
  boolean is50;

  Walker() {        
    //just having it start in the middle, not giving it more assignments in constructor
    //so those arguments are not put into the parenthesis 
    x = width/2;
    y = height/2;
  }

  void render() {
    stroke(0);
    shape(s, x, y, 20,20);  //loaded svg file of fly 
  }

  // Randomly move, with a 50% chance of moving in the direction of the mouse
  void step() {

    float r = random(1);

    //With probability at 50%---every frame is a roll of the dice

    if (r < 0.5) {  
      is50=true;

      if (mouseX - x>0) {
        x+=10;
      }
      else {
        x-=10;
      }

      if(mouseY - y>0){
       y+=10;
      }
      else {
        y-=10;
      }
      }

      else {
        is50=false;

       if(mouseX -x<0) {
        x+=10;
       } 
        else {
         x-=10; 
        }

        if(mouseY-y<0){
         y+=10; 
        }
        else{
         y-=10; 
        }
      }

    x = constrain(x, 0, width-1);
    y = constrain(y, 0, height-1);
  }
}