Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
///////////////////////////////
/////////CODE BREAKERS/////////
///////////////////////////////

//By Jessica Zhang
//Click each button to cycle through a variety of colours to guess the correct colour combination. If the colour is correct, a green light will apear around it. if not, a red light will appear around it.

//Referenced Peter Francis' code from 2016 just to see how his buttons worked to cycle through various options.

//declare button related vars
int buttonColourRow1 = 0;
int buttonColourRow2 = 0;
int buttonColourRow3 = 0;
int buttonColourRow4 = 0;

int activeButtonSet;
int activeButtonSetX=40;

int buttonPositionX1=40;
int buttonPositionX2=40;
int buttonPositionX3=40;
int buttonPositionX4=40;

boolean buttonClicked1;
boolean buttonClicked2;
boolean buttonClicked3;
boolean buttonClicked4;
boolean redSubmitButtonPushed;
int redSubmitButton;

//set the secret code
float colourComboRow1 = (int) random (1, 6);
float colourComboRow2 = (int) random (1, 6);
float colourComboRow3 = (int) random (1, 6);
float colourComboRow4 = (int) random (1, 6);

boolean secretCode1Correct;
boolean secretCode2Correct;
boolean secretCode3Correct;
boolean secretCode4Correct;


void setup() {
  size (400, 400);
  background(0);
  rectMode (CORNERS);
  drawCircuitBoard(); 
  drawAllInactiveButtons();
}

void draw() {

  drawBlinkingLights(); //fix that shit
  drawActiveButtonSet();
  changeButtonColourRow1();
  changeButtonColourRow2();
  changeButtonColourRow3();
  changeButtonColourRow4();

}


///////////////////////////////
/////BUTTON COLOUR CHOICE MECHANICS
///////////////////////////////
//
//every time you click the mouse, the button colour changes. The column chosen is set by buttonPositionX which is incremented by 80 pixels every time the submit button is pushed (to continue to the next column)
//the active button set determines which column is currently in play. The code is repeated for each Y axis of the buttons.

void changeButtonColourRow1() {

  strokeWeight(1);
  stroke(255);
  if (mousePressed &&  buttonClicked1 == false) {
    buttonClicked1 = true;
    if (mouseX>=buttonPositionX1-10 && mouseX<=buttonPositionX1+10 && mouseY >=20 && mouseY <=80) {
      buttonColourRow1++;
    }
    if (buttonColourRow1>6) {
      buttonColourRow1=1;
    }

    if (buttonColourRow1==1) {
      //red
      fill(199, 66, 66);
      ellipse(buttonPositionX1, 50, 40, 40);
    } else if (buttonColourRow1==2) {
      //orange
      fill(245, 151, 10);
      ellipse(buttonPositionX1, 50, 40, 40);
    } else if (buttonColourRow1==3) {
      //yellow
      fill(230, 215, 6);
      ellipse(buttonPositionX1, 50, 40, 40);
    } else if (buttonColourRow1==4) {
      //green
      fill(53, 162, 71);
      ellipse(buttonPositionX1, 50, 40, 40);
    } else if (buttonColourRow1==5) {
      //blue
      fill(0, 171, 255);
      ellipse(buttonPositionX1, 50, 40, 40);
    } else if (buttonColourRow1==6) {
      //purple
      fill(126, 60, 240);
      ellipse(buttonPositionX1, 50, 40, 40);
    }
    //pushing the red button causes the activebuttonset to move right by one. This causes a discrepancy between where the active buttons are and where the active button set is. 
    //the following lines of code activate the next set of buttons and resets the values of button colour back to zero.
    if (activeButtonSetX != buttonPositionX1) {
      buttonPositionX1+=80;
      buttonColourRow1=0;
    }
  }
}

void changeButtonColourRow2() {
  strokeWeight(1);
  stroke(255);
  if (mousePressed &&  buttonClicked2 == false) {
    buttonClicked2 = true;
    if (mouseX>=buttonPositionX2-10 && mouseX<=buttonPositionX2+10 && mouseY >=100 && mouseY <=160) {
      buttonColourRow2++;
    }
    if (buttonColourRow2>6) {
      buttonColourRow2=1;
    }

    if (buttonColourRow2==1) {
      fill(199, 66, 66);
      ellipse(buttonPositionX2, 130, 40, 40);
    } else if (buttonColourRow2==2) {
      fill(245, 151, 10);
      ellipse(buttonPositionX2, 130, 40, 40);
    } else if (buttonColourRow2==3) {
      fill(230, 215, 6);
      ellipse(buttonPositionX2, 130, 40, 40);
    } else if (buttonColourRow2==4) {
      fill(53, 162, 71);
      ellipse(buttonPositionX2, 130, 40, 40);
    } else if (buttonColourRow2==5) {
      fill(0, 171, 255);
      ellipse(buttonPositionX2, 130, 40, 40);
    } else if (buttonColourRow2==6) {
      fill(126, 60, 240);
      ellipse(buttonPositionX2, 130, 40, 40);
    }
    if (activeButtonSetX != buttonPositionX2) {
      buttonPositionX2+=80;
      buttonColourRow2=0;
    }
  }
}

void changeButtonColourRow3() {
  strokeWeight(1);
  stroke(255);
  if (mousePressed &&  buttonClicked3 == false) {
    buttonClicked3 = true;
    if (mouseX>=buttonPositionX3-10 && mouseX<=buttonPositionX3+10 && mouseY >=180 && mouseY <=240) {
      buttonColourRow3++;
    }
    if (buttonColourRow3>6) {
      buttonColourRow3=1;
    }

    if (buttonColourRow3==1) {
      fill(199, 66, 66);
      ellipse(buttonPositionX3, 210, 40, 40);
    } else if (buttonColourRow3==2) {
      fill(245, 151, 10);
      ellipse(buttonPositionX3, 210, 40, 40);
    } else if (buttonColourRow3==3) {
      fill(230, 215, 6);
      ellipse(buttonPositionX3, 210, 40, 40);
    } else if (buttonColourRow3==4) {
      fill(53, 162, 71);
      ellipse(buttonPositionX3, 210, 40, 40);
    } else if (buttonColourRow3==5) {
      fill(0, 171, 255);
      ellipse(buttonPositionX3, 210, 40, 40);
    } else if (buttonColourRow3==6) {
      fill(126, 60, 240);
      ellipse(buttonPositionX3, 210, 40, 40);
    }
    if (activeButtonSetX != buttonPositionX3) {
      buttonPositionX3+=80;
      buttonColourRow3=0;
    }
  }
}

void changeButtonColourRow4() {
  strokeWeight(1);
  stroke(255);
  if (mousePressed &&  buttonClicked4 == false) {
    buttonClicked4 = true;
    if (mouseX>=buttonPositionX4-10 && mouseX<=buttonPositionX4+10 && mouseY >=260 && mouseY <=320) {
      buttonColourRow4++;
    }
    if (buttonColourRow4>6) {
      buttonColourRow4=1;
    }

    if (buttonColourRow4==1) {
      fill(199, 66, 66);
      ellipse(buttonPositionX4, 290, 40, 40);
    } else if (buttonColourRow4==2) {
      fill(245, 151, 10);
      ellipse(buttonPositionX4, 290, 40, 40);
    } else if (buttonColourRow4==3) {
      fill(230, 215, 6);
      ellipse(buttonPositionX4, 290, 40, 40);
    } else if (buttonColourRow4==4) {
      fill(53, 162, 71);
      ellipse(buttonPositionX4, 290, 40, 40);
    } else if (buttonColourRow4==5) {
      fill(0, 171, 255);
      ellipse(buttonPositionX4, 290, 40, 40);
    } else if (buttonColourRow4==6) {
      fill(126, 60, 240);
      ellipse(buttonPositionX4, 290, 40, 40);
    }
    if (activeButtonSetX != buttonPositionX4) {
      buttonPositionX4+=80;
      buttonColourRow4=0;
    }
  }
}

//booleans to make them buttons work
void mouseReleased() {
  buttonClicked1 = false;
  buttonClicked2 = false;
  buttonClicked3 = false;
  buttonClicked4 = false;
  redSubmitButtonPushed = false;
}

void drawActiveButtonSet() {
  rectMode(CENTER);
  noFill();
  strokeWeight(4);
  stroke(255, 255, 255, 240);
  rect(activeButtonSetX, height/2, 80, 400);

  if (mouseX>=activeButtonSetX-30 && mouseX<=activeButtonSetX+30 && mouseY >=340 && mouseY <=380 && mousePressed &&  redSubmitButtonPushed == false) {
    activeButtonSetX+=80;
    redSubmitButtonPushed = true;
    testIfColourCombinationIsCorrect();
  }
}

//tests for whether the randomized colour matches the colour set by the player.
void testIfColourCombinationIsCorrect() {
  noStroke();
  if (colourComboRow1==buttonColourRow1) {
    fill(0, 255, 0);
    ellipse(buttonPositionX1, 50, 50, 50);
  } else if (colourComboRow1 != buttonColourRow1) {
    fill (255, 0, 0);
    ellipse (buttonPositionX1, 50, 50, 50);
  }

  if (colourComboRow2==buttonColourRow2) {
    fill(0, 255, 0);
    ellipse(buttonPositionX2, 130, 50, 50);
  } else if (colourComboRow2 != buttonColourRow2) {
    fill (255, 0, 0);
    ellipse (buttonPositionX2, 130, 50, 50);
  }

  if (colourComboRow3==buttonColourRow3) {
    fill(0, 255, 0);
    ellipse(buttonPositionX3, 210, 50, 50);
  } else if (colourComboRow3 != buttonColourRow3) {
    fill (255, 0, 0);
    ellipse (buttonPositionX3, 210, 50, 50);
  }

  if (colourComboRow4==buttonColourRow4) {
    fill(0, 255, 0);
    ellipse(buttonPositionX4, 290, 50, 50);
  } else if (colourComboRow4 != buttonColourRow4) {
    fill (255, 0, 0);
    ellipse (buttonPositionX4, 290, 50, 50);
  }
}

void drawAllInactiveButtons() {
  //drawing the outside white rings
  noStroke();
  fill(255);
  //top row
  ellipse(40, 50, 50, 50);
  ellipse(120, 50, 50, 50);
  ellipse (200, 50, 50, 50);
  ellipse (280, 50, 50, 50);
  ellipse (360, 50, 50, 50);
  //second row
  ellipse (40, 130, 50, 50);
  ellipse(120, 130, 50, 50);
  ellipse (200, 130, 50, 50);
  ellipse (280, 130, 50, 50);
  ellipse (360, 130, 50, 50);
  //third row
  ellipse (40, 210, 50, 50);
  ellipse(120, 210, 50, 50);
  ellipse (200, 210, 50, 50);
  ellipse (280, 210, 50, 50);
  ellipse (360, 210, 50, 50);
  //fourth row
  ellipse (40, 290, 50, 50);
  ellipse(120, 290, 50, 50);
  ellipse (200, 290, 50, 50);
  ellipse (280, 290, 50, 50);
  ellipse (360, 290, 50, 50);

  //drawing the actual buttons
  fill (80);
  //top row
  ellipse(40, 50, 40, 40);
  ellipse(120, 50, 40, 40);
  ellipse (200, 50, 40, 40);
  ellipse (280, 50, 40, 40);
  ellipse (360, 50, 40, 40);
  //second row
  ellipse (40, 130, 40, 40);
  ellipse(120, 130, 40, 40);
  ellipse (200, 130, 40, 40);
  ellipse (280, 130, 40, 40);
  ellipse (360, 130, 40, 40);
  //third row
  ellipse (40, 210, 40, 40);
  ellipse(120, 210, 40, 40);
  ellipse (200, 210, 40, 40);
  ellipse (280, 210, 40, 40);
  ellipse (360, 210, 40, 40);
  //fourth row
  ellipse (40, 290, 40, 40);
  ellipse(120, 290, 40, 40);
  ellipse (200, 290, 40, 40);
  ellipse (280, 290, 40, 40);
  ellipse (360, 290, 40, 40);

  //drawing the red submit buttons
  noStroke();
  fill (255, 0, 0);
  rect (10, 340, 70, 380);
  rect (90, 340, 150, 380);
  rect (170, 340, 230, 380);
  rect (250, 340, 310, 380);
  rect (330, 340, 390, 380);
}

void drawCircuitBoard() {
  //draws a super cool circuit in the background

  strokeWeight(2);
  stroke(0, 255, 0);

  line(0, 20, 100, 20);
  line(100, 20, 100, 120);
  line(0, 120, 100, 120);
  line(100, 80, 320, 80);
  line(240, 0, 240, 80);
  line(320, 80, 320, 160);
  line(320, 160, 400, 160);
  line(280, 80, 280, 180);
  line(160, 180, 280, 180);
  line(160, 120, 160, 300);
  line(160, 300, 40, 300);
  line(40, 200, 40, 360);
  line(40, 360, 360, 360);
  line(240, 260, 240, 360);
  line(220, 260, 320, 260);
  line(320, 240, 320, 320);
  line(320, 320, 400, 320);
}

void drawBlinkingLights() {
  frameRate (15);
  float lightOn1 = random (0, 20);
  if (lightOn1 > 10) {

    fill(0, 255, 0);
    stroke(255);
    strokeWeight(1);
    ellipse(160, 120, 5, 5);
  } else if (lightOn1 <=10) {
    fill(0, 0, 0);
    stroke(255);
    strokeWeight(1);
    ellipse(160, 120, 5, 5);
  }

  float lightOn2 = random (0, 100);
  if (lightOn2 > 50) {

    fill(0, 255, 0);
    stroke(255);
    strokeWeight(1);
    ellipse(220, 260, 5, 5);
  } else if (lightOn2 <=50) {
    fill(0, 0, 0);
    stroke(255);
    strokeWeight(1);
    ellipse(220, 260, 5, 5);
  }

  float lightOn3 = random (0, 150);
  if (lightOn3 > 68) {

    fill(0, 255, 0);
    stroke(255);
    strokeWeight(1);
    ellipse(320, 240, 5, 5);
  } else if (lightOn3 <=68) {
    fill(0, 0, 0);
    stroke(255);
    strokeWeight(1);
    ellipse(320, 240, 5, 5);
  }
}