Code

// State Machine 01
// Herbert Spencer says: Help! I guess I have a scoping problem here. The square won't remain as a square...

// Initial variables
float xpos = random(300);
float ypos = random(300);
float newxpos = random(300);
float newypos = random(300);
int square = 100;
int delayer = 10;
boolean black = true;

void setup(){
  size(400,400);
  rectMode(CORNER);
}

void draw(){
  background(255);
  stroke(0);
  strokeWeight(5);
  if(black) {
    fill(0);
  } 
  else {
    fill(255);
  }

  xpos = xpos + ((newxpos - xpos) / delayer);
  ypos = ypos + ((newypos - ypos) / delayer);

  rect(xpos,ypos, square, square);

}

void mousePressed(){
  if ((mouseX >= xpos) && (mouseX <= xpos+square) &&
    (mouseY >= ypos) && (mouseY <= ypos+square)) {
    newxpos = random(300);
    newypos = random(300);
    if(black) {
      black = false;    
    } 
    else{ 
      black = true;    
    }
  }
}

014 -- Flipping Square I: Simple State Machines (Due Monday 9/18)

Statement:This exercise tests your ability to maintain system state in some sort of variable, and also asks you to devise a way of testing the containment of a point (the cursor) within a special region (a reactive square).

In a 400x400 canvas, create a 100x100 pixel reactive square, such that each click in the square flips its color from white to black (if it is white), or from black to white (if it is black). Mouse clicks outside the square should have no effect! This example may be helpful to you.
I think the code is right but...

hide statement