Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/////////////////
/////
//Slot Machine by Lena Stonewell (Schott)
/////
/////////////////

//This interactive toy allows you to use the lever on the
//slot machine, which randomizes the 3 slots. If you get a
//matching 3 images, you win and coins spit out.

// variable to determine whether you win
boolean win;

//variables related to slots
boolean crown1 = true;
boolean crown2 = true;
boolean crown3 = true;
boolean alien1 = false;
boolean alien2 = false ;
boolean alien3 = false;
boolean cherry1 = false;
boolean cherry2 = false;
boolean cherry3 = false;

//variables related to lever
boolean lever;
float leverY = 130;
int leverspeed = 8;
boolean slotstart = false;
int coinspeed = 1;

//variables related to coins
boolean coingenerate = false;
int coinX = 50;
int coinY = 50;
int coinhere = 0;
float coinspawn = 0;

// sets up canvas
void setup() {
  size(400, 400);
  noCursor();
  smooth();
  rectMode(CORNER);
  frameRate(25);
}


void draw() {

  background (mouseY, mouseX, 255);

  // draws basic framework of slotmachine
  drawSlots();
  drawBanner();
  drawLever();
  drawDispenser();

  // updates conditions of slots
  outcomeSlots();
  drawOutcome();
  bannerWin();
  bannerLose();
  coinSpit();

  // draws each slot with random images
  slotOne();
  slotTwo();
  slotThree();
}


// determines whether or not the conditions are met for winning or losing the slots
void outcomeSlots () {
  if ((alien1 == true  && alien2 == true && alien3 == true) || (crown1 == true && crown2 == true && crown3 == true) || (cherry1 == true && cherry2 == true && cherry3 == true) ) {
    win = true;
  } else {
    win = false;
  }
}

// draws the final outcome from the updated slots
void drawOutcome() {
  if (lever == false && slotstart == true) {

    // draws either crown, cherry or alien in slot 1
    if (crown1 == true) {
      noStroke();
      fill(255, 255, 51);
      triangle(60, 210, 80, 220, 70, 240);
      triangle(85, 200, 70, 220, 100, 220);
      triangle(90, 220, 110, 210, 100, 240);
      rect(60, 240, 50, 10);
      rect(70, 220, 30, 30);
      fill(51, 255, 255);
      quad(85, 220, 80, 230, 85, 240, 90, 230);
    } else if (cherry1 == true) {
      strokeWeight(6);
      stroke(102, 204, 0);
      line(70, 240, 90, 210);
      line(97, 240, 90, 210);
      line(80, 200, 105, 215);

      noStroke();
      fill(255, 51, 51);
      ellipse(70, 250, 20, 20);
      ellipse(100, 250, 20, 20);
    } else if (alien1 == true) {
      noStroke();
      fill(128, 255, 0);
      ellipse(85, 230, 50, 50);
      strokeWeight(6);
      stroke(128, 255, 0);
      line(60, 200, 70, 195);
      line(110, 200, 100, 195);
      line(65, 198, 70, 210);
      line(105, 198, 100, 210);

      strokeWeight(2);
      stroke(128, 200, 0);
      fill(mouseX, mouseY, 100);
      ellipse(70, 230, 10, 30);
      ellipse(100, 230, 10, 30);
    }

    // draws either crown, cherry or alien in slot 2
    if (crown2 == true) {
      noStroke();
      fill(255, 255, 51);
      triangle(60+75, 210, 80+75, 220, 70+75, 240);
      triangle(85+75, 200, 70+75, 220, 100+75, 220);
      triangle(90+75, 220, 110+75, 210, 100+75, 240);
      rect(60+75, 240, 50, 10);
      rect(70+75, 220, 30, 30);
      fill(51, 255, 255);
      quad(85+75, 220, 80+75, 230, 85+75, 240, 90+75, 230);
    } else if (cherry2 == true) {
      strokeWeight(6);
      stroke(102, 204, 0);
      line(70+75, 240, 90+75, 210);
      line(97+75, 240, 90+75, 210);
      line(80+75, 200, 105+75, 215);

      noStroke();
      fill(255, 51, 51);
      ellipse(70+75, 250, 20, 20);
      ellipse(100+75, 250, 20, 20);
    } else if (alien2 == true) {
      noStroke();
      fill(128, 255, 0);
      ellipse(85+75, 230, 50, 50);
      strokeWeight(6);
      stroke(128, 255, 0);
      line(60+75, 200, 70+75, 195);
      line(110+75, 200, 100+75, 195);
      line(65+75, 198, 70+75, 210);
      line(105+75, 198, 100+75, 210);

      strokeWeight(2);
      stroke(128, 200, 0);
      fill(mouseX, mouseY, 100);
      ellipse(70+75, 230, 10, 30);
      ellipse(100+75, 230, 10, 30);
    }

    // draws either crown, cherry or alien in slot 3
    if (crown3 == true) {
      noStroke();
      fill(255, 255, 51);
      triangle(60+150, 210, 80+150, 220, 70+150, 240);
      triangle(85+150, 200, 70+150, 220, 100+150, 220);
      triangle(90+150, 220, 110+150, 210, 100+150, 240);
      rect(60+150, 240, 50, 10);
      rect(70+150, 220, 30, 30);
      fill(51, 255, 255);
      quad(85+150, 220, 80+150, 230, 85+150, 240, 90+150, 230);
    } else if (cherry3 == true) {
      strokeWeight(6);
      stroke(102, 204, 0);
      line(70+150, 240, 90+150, 210);
      line(97+150, 240, 90+150, 210);
      line(80+150, 200, 105+150, 215);

      noStroke();
      fill(255, 51, 51);
      ellipse(70+150, 250, 20, 20);
      ellipse(100+150, 250, 20, 20);
    } else if (alien3 == true) {
      noStroke();
      fill(128, 255, 0);
      ellipse(85+150, 230, 50, 50);
      strokeWeight(6);
      stroke(128, 255, 0);
      line(60+150, 200, 70+150, 195);
      line(110+150, 200, 100+150, 195);
      line(65+150, 198, 70+150, 210);
      line(105+150, 198, 100+150, 210);

      strokeWeight(2);
      stroke(128, 200, 0);
      fill(mouseX, mouseY, 100);
      ellipse(70+150, 230, 10, 30);
      ellipse(100+150, 230, 10, 30);
    }
  }
}

// intiates "WIN!" on the banner if you win the slots
void bannerWin() {
  int screen = 55; 
  strokeWeight(11);
  stroke(255);
  if (win == true && lever == false || slotstart == false) {
    line(120, 60, 130, 100);
    line(130, 100, 145, 80);
    line(145, 80, 160, 100);
    line(160, 100, 170, 60);

    line(200, 60, 200, 100);

    line(230, 100, 230, 60);
    line(230, 60, 270, 100);
    line(270, 100, 270, 60);

    line(290, 100, 290, 100);
    line(290, 80, 290, 60);
    strokeWeight(2); 
    stroke(0); 
    while (screen <= 105) {
      line(60, screen, 346, screen); 
      screen=screen+5;
    }
  }
}

//initiates "LOSE!" on the banner if you lose the slots
void bannerLose() {
  int screen = 55; 
  strokeWeight(11);
  stroke(255);
  if (win == false && lever == false && slotstart == true) {

    line(100, 60, 100, 100);
    line(100, 100, 130, 100);

    line(150, 100, 180, 100);
    line(180, 100, 180, 60);
    line(180, 60, 150, 60);
    line(150, 60, 150, 100);

    line(200, 60, 230, 60);
    line(200, 60, 200, 80);
    line(200, 80, 230, 80);
    line(230, 80, 230, 100);
    line(230, 100, 200, 100);

    line(250, 60, 250, 100);
    line(250, 60, 280, 60);
    line(250, 100, 280, 100);
    line(250, 80, 270, 80);

    line(310, 100, 310, 100);
    line(310, 80, 310, 60);
    strokeWeight(2); 
    stroke(0); 
    while (screen <= 105) {
      line(60, screen, 346, screen); 
      screen=screen+5;
    }
  }
}

// sets up slot 1 randomizer, creating illusion of flickering images like a slotmachine
void slotOne() {

  if (slotstart == true && lever == true) {
    for (int slotroll = 0; slotroll < 2; slotroll=slotroll+1) {
      float slotgen = random(3);
      if (slotgen < 1) {
        noStroke();
        fill(255, 255, 51);
        triangle(60, 210, 80, 220, 70, 240);
        triangle(85, 200, 70, 220, 100, 220);
        triangle(90, 220, 110, 210, 100, 240);
        rect(60, 240, 50, 10);
        rect(70, 220, 30, 30);
        fill(51, 255, 255);
        quad(85, 220, 80, 230, 85, 240, 90, 230);
        crown1=true;
        cherry1=false;
        alien1=false;
      } else if (slotgen >= 1 && slotgen < 2) {
        strokeWeight(6);
        stroke(102, 204, 0);
        line(70, 240, 90, 210);
        line(97, 240, 90, 210);
        line(80, 200, 105, 215);

        noStroke();
        fill(255, 51, 51);
        ellipse(70, 250, 20, 20);
        ellipse(100, 250, 20, 20);
        crown1=false;
        cherry1=true;
        alien1=false;
      } else if (slotgen > 2) {
        noStroke();
        fill(128, 255, 0);
        ellipse(85, 230, 50, 50);
        strokeWeight(6);
        stroke(128, 255, 0);
        line(60, 200, 70, 195);
        line(110, 200, 100, 195);
        line(65, 198, 70, 210);
        line(105, 198, 100, 210);

        strokeWeight(2);
        stroke(128, 200, 0);
        fill(mouseX, mouseY, 100);
        ellipse(70, 230, 10, 30);
        ellipse(100, 230, 10, 30);
        crown1=false;
        cherry1=false;
        alien1=true;
      }
    }
  }
}

// sets up slot 2 randomizer, creating illusion of flickering images like a slotmachine
void slotTwo() {

  if (slotstart == true && lever == true) {
    for (int slotroll = 0; slotroll < 2; slotroll=slotroll+1) {
      float slotgen = random(3);
      if (slotgen < 1) {
        noStroke();
        fill(255, 255, 51);
        triangle(60+75, 210, 80+75, 220, 70+75, 240);
        triangle(85+75, 200, 70+75, 220, 100+75, 220);
        triangle(90+75, 220, 110+75, 210, 100+75, 240);
        rect(60+75, 240, 50, 10);
        rect(70+75, 220, 30, 30);
        fill(51, 255, 255);
        quad(85+75, 220, 80+75, 230, 85+75, 240, 90+75, 230);
        crown2=true;
        cherry2=false;
        alien2=false;
      } else if (slotgen >= 1 && slotgen < 2) {
        strokeWeight(6);
        stroke(102, 204, 0);
        line(70+75, 240, 90+75, 210);
        line(97+75, 240, 90+75, 210);
        line(80+75, 200, 105+75, 215);

        noStroke();
        fill(255, 51, 51);
        ellipse(70+75, 250, 20, 20);
        ellipse(100+75, 250, 20, 20);
        crown2=false;
        cherry2=true;
        alien2=false;
      } else if (slotgen > 2) {
        noStroke();
        fill(128, 255, 0);
        ellipse(85+75, 230, 50, 50);
        strokeWeight(6);
        stroke(128, 255, 0);
        line(60+75, 200, 70+75, 195);
        line(110+75, 200, 100+75, 195);
        line(65+75, 198, 70+75, 210);
        line(105+75, 198, 100+75, 210);

        strokeWeight(2);
        stroke(128, 200, 0);
        fill(mouseX, mouseY, 100);
        ellipse(70+75, 230, 10, 30);
        ellipse(100+75, 230, 10, 30);
        crown2=false;
        cherry2=false;
        alien2=true;
      }
    }
  }
}

// sets up slot 3 randomizer, creating illusion of flickering images like a slotmachine
void slotThree() {

  if (slotstart == true && lever == true) {
    for (int slotroll = 0; slotroll < 2; slotroll=slotroll+1) {
      float slotgen = random(3);
      if (slotgen < 1) {
        noStroke();
        fill(255, 255, 51);
        triangle(60+150, 210, 80+150, 220, 70+150, 240);
        triangle(85+150, 200, 70+150, 220, 100+150, 220);
        triangle(90+150, 220, 110+150, 210, 100+150, 240);
        rect(60+150, 240, 50, 10);
        rect(70+150, 220, 30, 30);
        fill(51, 255, 255);
        quad(85+150, 220, 80+150, 230, 85+150, 240, 90+150, 230);
        crown3=true;
        cherry3=false;
        alien3=false;
      } else if (slotgen >= 1 && slotgen < 2) {
        strokeWeight(6);
        stroke(102, 204, 0);
        line(70+150, 240, 90+150, 210);
        line(97+150, 240, 90+150, 210);
        line(80+150, 200, 105+150, 215);

        noStroke();
        fill(255, 51, 51);
        ellipse(70+150, 250, 20, 20);
        ellipse(100+150, 250, 20, 20);
        crown3=false;
        cherry3=true;
        alien3=false;
      } else if (slotgen > 2) {
        noStroke();
        fill(128, 255, 0);
        ellipse(85+150, 230, 50, 50);
        strokeWeight(6);
        stroke(128, 255, 0);
        line(60+150, 200, 70+150, 195);
        line(110+150, 200, 100+150, 195);
        line(65+150, 198, 70+150, 210);
        line(105+150, 198, 100+150, 210);

        strokeWeight(2);
        stroke(128, 200, 0);
        fill(mouseX, mouseY, 100);
        ellipse(70+150, 230, 10, 30);
        ellipse(100+150, 230, 10, 30);
        crown3=false;
        cherry3=false;
        alien3=true;
      }
    }
  }
}

// if winning conditions are met, coins spit out from the bottom
void coinSpit() {
  if (win == true && lever == false && slotstart == true) {
    for (int coins = 0; coins < 10; coins=coins+1) {
      coinspawn = random(80, 320); 
      coinhere = coinhere + coinspeed; 

      if (coinhere >190) {
        coinhere = 0;
      }
      strokeWeight(4);
      stroke(193, 162, 2);
      fill(255, 213, 0); 
      ellipse(coinspawn, 360+ coinhere, coinX, coinY);
    }
  }
}

// draws the banner and create the illusion of flashing lights
void drawBanner() {
  int a = 40; 
  int b = 40; 
  int c = 80; 
  int d = 40; 
  int e = 60; 
  int f = 10; 
  int tri = 40; 
  int light = 10; 
  int h = 45; 
  int p = 55; 

  strokeWeight(3); 
  stroke(225, 0, 0); 
  fill(225, 0, 0); 
  rect(40, 40, 320, 80); 

  fill(0); 
  rect(50, 50, 300, 60); 

  // creates different colours for top of banner
  fill(255, mouseY, mouseX); 
  while (a <= 320) {
    triangle(a, b, c, d, e, f); 
    a=a+tri; 
    c=c+tri; 
    e=e+tri;
  }

  // creates illusion of flashing lights using mouse location
  if (mouseY < 200) {
    fill(255); 
    for (int maxdots = 0; maxdots < 39; maxdots += 1) {
      if (maxdots < 15) {
        fill(255); 
        ellipse(p+maxdots*20, h, light, light); 
        fill(0); 
        ellipse(p+maxdots*20+10, h, light, light);
      } else if (maxdots < 19) {
        fill(255); 
        ellipse(300+p, h, light, light); 
        fill(0); 
        ellipse(300+p, h+10, light, light); 
        h=h+20;
      } else if (maxdots < 35) {
        fill(255); 
        ellipse(p-10, h-10, light, light); 
        fill(0); 
        ellipse(p, h-10, light, light); 
        p=p+20;
      } else if (maxdots < 39) {
        fill(255); 
        ellipse(p-330, h-10, light, light); 
        fill(0); 
        ellipse(p-330, h-20, light, light); 
        h=h-20;
      }
    }
  } else if (mouseY > 200) {
    fill(0); 
    for (int maxdots = 0; maxdots < 39; maxdots += 1) {
      if (maxdots < 15) {
        fill(0); 
        ellipse(p+maxdots*20, h, light, light); 
        fill(255); 
        ellipse(p+maxdots*20+10, h, light, light);
      } else if (maxdots < 19) {
        fill(0); 
        ellipse(300+p, h, light, light); 
        fill(255); 
        ellipse(300+p, h+10, light, light); 
        h=h+20;
      } else if (maxdots < 35) {
        fill(0); 
        ellipse(p-10, h-10, light, light); 
        fill(255); 
        ellipse(p, h-10, light, light); 
        p=p+20;
      } else if (maxdots < 39) {
        fill(0); 
        ellipse(p-330, h-10, light, light); 
        fill(255); 
        ellipse(p-330, h-20, light, light); 
        h=h-20;
      }
    }
  }
}

// draws basic framework for slotmachine
void drawSlots() {
  float r = random(255);
  float g = random(255);
  float b = random(255);

  strokeWeight(5); 
  stroke(0); 
  if (lever == true && slotstart == true) {
    fill(r, g, b);
  } else {
    fill(225, 0, 0);
  }
  rect(41, 124, 240, 200); 
  rect(285, 124, 74, 200); 
  fill(55); 
  rect(317, 140, 10, 170); 
  noStroke(); 
  rect(51, 140, 70, 170); 
  rect(126, 140, 70, 170); 
  rect(201, 140, 70, 170);
}

// draws lever and its conditions
void drawLever() {
  strokeWeight(3); 
  stroke(0); 
  fill(225, 0, 0); 
  rect(292, leverY+2, 60, 20);
  if (lever == true) {
    rect(292, leverY, 60, 20);
    leverY=leverY+leverspeed;
  } 
  if (leverY > 295 || leverY <= 130) {
    leverspeed = leverspeed * -1;
  } 
  if (leverY == 130) {
    lever = false;
  }
}


// draws basic framework for dispenser at bottom
void drawDispenser() {
  float r = random(255);
  float g = random(255);
  float b = random(255);

  strokeWeight(5); 
  stroke(0); 
  if (lever == true && slotstart == true) {
    fill(r, g, b);
  } else {
    fill(225, 0, 0);
  }
  rect(41, 320, 318, 50); 
  fill(0); 
  rect(60, 340, 280, 20);
}

// releasing mousekey initiates slot machine
void mouseReleased() {
  if (lever == false) {
    lever = true;
    slotstart = true;
    if (lever == false && slotstart == true) {
      outcomeSlots();
    }
  }
}