Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
TITLE: VENDING MACHINE
BY: QUINN GOFFIN
DESCRIPTION: VENDING MACHINE WHERE YOU MAY CHOOSE BETWEEN CHOCOLATE OR CHIPS. PUSH THE BUTTONS TO HAVE YOUR ORDER COME UP ON THE DISPLAY. 
THAT CORRESPONDING ORDER WILL THEN BE DROPPED TO THE MACHINES DOOR WHERE YOU CAN THEN SCROLL YOUR MOUSE OVER TO OPEN IT AND VIEW THE FOOD ITEM DROPPED.

*/
Buttons myButton;
Scene myScene;
Screen myScreen;
Food myFood;
Door myDoor;

int foodDrop;
float [] foodChoice;
float foodSelect;
float foodLetter;
float foodNumber;
void setup() {
  size(400, 400);

  myButton = new Buttons();
  myScene = new Scene();
  myScreen = new Screen();
  foodChoice = new float [2];
  myFood = new Food();
  myDoor = new Door();
  foodChoice[0] = 0;
  foodChoice[1] = 0;
}

void draw() {
  background(200);
  myScene.display();
  myButton.display();
  myFood.display();
  foodSelect = myButton.pressed(mouseX, mouseY);
  determineFood();
  myScreen.display(foodChoice[0], foodChoice[1]);
  foodDrop = myFood.choice(foodChoice[0]);
  myDoor.display(mouseX, mouseY, foodDrop);
}



//Edits array depending on values returned by buttons
void determineFood() {

  if (foodSelect > 0 && foodSelect < 4) {
    foodChoice[0] = foodSelect;
  } else if (foodSelect > 3) {
    foodChoice[1] = foodSelect;
  }
}
class Buttons {

  //Visual properties of buttons
  float x1;
  float x2;
  float y1;
  float y2;
  float y3;
  float size;

  //Variable for determining if button is pressed
  float a;
  float b;
  float c;
  float one;
  float two;
  float three;
  float none;

  Buttons() {

    //Initialize variables
    x1 = 300;
    x2 = 325;
    y1 = 100;
    y2 = 125;
    y3 = 150;
    size = 24;
    a = 1;
    b = 2;
    c = 3;
    one = 4;
    two = 5;
    three = 6;
  }

  //Draws buttons

  void display() {
    stroke(50);
    strokeWeight(1);
    fill(60);
    rect(x1, y1, size, size);
    rect(x2, y1, size, size);
    rect(x1, y2, size, size);
    rect(x2, y2, size, size);
    rect(x1, y3, size, size);
    rect(x2, y3, size, size);
  }

  //Determines which button has been pressed and returns the value to the main program to be used by other objects

  float pressed(float mx, float my) {
    if (mx > x1 && mx < x1+size && my > y1 && my < y1+size && mousePressed) {
      return a;
    } else if (mx > x1 && mx < x1+size && my > y2 && my < y2+size && mousePressed) {
      return b;
    } else if (mx > x1 && mx < x1+size && my > y3 && my < y3+size && mousePressed) {
      return c;
    } else if (mx > x2 && mx < x2+size && my > y1 && my < y1+size && mousePressed) {
      return one;
    } else if (mx > x2 && mx < x2+size && my > y2 && my < y2+size && mousePressed) {
      return two;
    } else if (mx > x2 && mx < x2+size && my > y3 && my < y3+size && mousePressed) {
      return three;
    } else {
      return 0;
    }
  }
}
class Door {
  //Location Variables
  int xpos;
  int ypos;

  Door() {

    xpos = 125;
    ypos = 350;
  }

  void display(float mx, float my, int food) {

    //Displays chips when mouse over door

    if (mx > xpos && mx < xpos+ 125 && food == 1) {
      fill(50);
      rect(xpos, ypos, 125, 50);
      fill(100);
      quad(xpos, ypos, xpos+125, ypos, xpos+100, ypos+25, xpos+25, ypos+25);
      fill(200, 200, 0);
      rect(xpos+25, ypos+25, 75, 25);
    }

    //Displays chocolate when mouse over door

    else if (mx > xpos && mx < xpos+ 125 && food == 2) {
      fill(50);
      rect(xpos, ypos, 125, 50);
      fill(100);
      quad(xpos, ypos, xpos+125, ypos, xpos+100, ypos+25, xpos+25, ypos+25);
      fill(59, 26, 5);
      rect(xpos+25, ypos+25, 65, 15);
    }

    //Keeps door at neutral state when mouse is not over door and no food option has been choosen

    else {
      fill(100);
      rect(xpos, ypos, 125, 50);
    }
  }
}
class Food {

  //Variables to return what food will drop to the door
  int chips;
  int chocolate;

  //Location Variables
  int x1;
  int x2;
  int x3;
  int y1;
  int y2;
  int y3;


  Food() {
    chips = 1;
    chocolate = 2;
    x1 = 80;
    x2 = 155;
    x3 = 230;
    y1 = 25;
    y2 = 125;
    y3 = 225;
  }

  void display() {

    //Chips
    fill(200, 200, 0);
    rect(x1, y1, 40, 40);
    rect(x2, y1, 40, 40);
    rect(x3, y1, 40, 40);
    rect(x1, y3, 40, 40);
    rect(x2, y3, 40, 40);
    rect(x3, y3, 40, 40);

    //Chocolate
    fill(59, 26, 5);
    rect(x1, y2, 20, 40);
    rect(x2, y2, 20, 40);
    rect(x3, y2, 20, 40);
  }



  //Returns which food will drop depending on button input
  int choice (float letter) {
    if (letter == 1 || letter == 3) {
      return chips;
    } else if (letter  == 2) {
      return chocolate;
    } else {
      return 0;
    }
  }
}
class Scene {


  void display() {

    //Vending machine outter body

    noStroke();
    fill(175, 0, 0);
    rect(50, 0, 300, 400);

    // Vending machine rows

    for (int i = 0; i < 3; i++) {
      fill(35);
      rect(75, 25+(i*100), 225, 50);
      fill(25);
      rect(75, 0+(i*100), 225, 25);
      fill(100);
      rect(75, 75+(i*100), 225, 25);
      fill(0, 0, 0, 30);
      rect(75, 25+(i*100), 225, 25);
    } 
    //Trash can
    fill(100);
    rect(375, 300, 25, 100);
    fill(75);
    ellipse(425, 300, 100, 50);
  }
}
class Screen {

  //Location Variables
  float xpos;
  float ypos;


  Screen() {

    xpos = 305;
    ypos = 75;
  }

  void display(float letter, float number) {

    //Screen
    stroke(50);
    strokeWeight(1);
    fill(60);
    rect(300, 50, 49, 50);

    //Text corresponding to buttons pressed
    textSize(20);
    fill(255, 0, 0);

    //Determines which button has been pressed
    if (letter == 1) {
      text("A", xpos, ypos);
    } else if (letter  == 2) {
      text("B", xpos, ypos);
    } else if (letter == 3) {
      text("C", xpos, ypos);
    }

    if (number == 4) {
      text("1", xpos+25, ypos);
    } else if (number == 5) {
      text("2", xpos+25, ypos);
    } else if (number == 6) {
      text("3", xpos+25, ypos);
    }
  }
}