Index / 2017 / Assignment 04
600×400 · p5.js instance mode
2 PDE files 92 lines
// Assignment_04.pde
int state;
button start;

void setup() {
  size(600,400);
  rectMode(CENTER);
  textAlign(CENTER,CENTER);
  noStroke();
  start = new button(width/2,height/2,width/4,height/4,#FF5A8E,#B24467,#E88CA9);
}

void draw() {
  switch(state) {
    case 0: mainMenu(); break;
    case 1: optionsMenu(); break;
    case 2: game(); break;
    default: mainMenu(); break;
  }
}

void mainMenu() {
  background(#313743);
  start.display();
  
}

void optionsMenu() {
  background(100);
}

void game() {
  background(200);
}

// button.pde
class button {

  float x, y, w, h;
  color i, r, c;
  boolean mouseOn, clicked;

  button(float tempX, float tempY, float tempW, float tempH, color tempI, color tempR, color tempC) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    i = tempI;
    r = tempR;
    c = tempC;
    mouseOn = false;
    clicked = false;
  }
  
  void display() {
    update();
    rectMode(CENTER);
    noStroke();
    
    if (mouseOn) {
      fill(r);
      if (clicked) {
        fill(c);
      }
    } else {
      fill(i);
    }
    
    rect(x,y,w,h);
  }
  
  void update() {
    if (mouseOver(x, y, w, h) == true) {
      mouseOn = true;
      if (mousePressed) {
        clicked = true;
      } else {
        clicked = false;
      }
    } else {
      mouseOn = false;
    }
  }

  boolean mouseOver(float x, float y, float w, float h) {
    if (mouseX >= (x-w/2) && mouseX <= (x+w/2) && mouseY >= (y-h/2) && mouseY <= (y+h/2)) {
      return true;
    } else {
      return false;
    }
  }
}
read-only archive source from /2017/Assignments/A04 - Old School Games/03-27-2017/Assignment_04/Assignment_04.pde