int signal = 0; // 0 is nothing, 1 is green, 2 is red
int timer = 0;
int change = 130; // when the timer gets to this value show a new signal text
int score = 0;
color textColor;
String signalText = "...";
// button variables
int BUTTONW = 100;
int BUTTONH = 70;
int greenX = 150;
int greenY = 300;
int redX = 450;
int redY = 300;
void setup(){
size(600,600);
rectMode(CENTER);
textColor = getRandomColor();
textSize(24);
textAlign(CENTER, CENTER);
}
void draw(){
background(0);
// 1: write conditionals to assign signalText based on the signal
if(signal == 0){
signalText = "...";
}
if(signal == 1){
signalText = "GREEN";
}
if(signal == 2){
signalText = "RED";
}
fill(textColor);
text(signalText,300,300);
// use our timer to change the signal
// If you want you can stop displaying the timer once you see how it works
timer = timer + 1;
fill(255);
// text(timer,100,100);
if(timer >= change){
textColor = getRandomColor();
timer = 0;
signal = (int)random(0,3);
}
// draw the buttons on the screen
fill(0,200,0);
rect(greenX,greenY,BUTTONW,BUTTONH);
fill(200,0,0);
rect(redX,redY,BUTTONW,BUTTONH);
// display the score
fill(255);
textSize(30);
text("Score: " + score,400,100);
text("Simon Says! \n Click the correct buttons to get points", width/2, height-100);
}
void mousePressed(){
// 2: check if we're clicking the green button
// if we are and signal is 1, increase the score by 1
if(abs(greenX - mouseX) < BUTTONW/2){
if(abs(greenY - mouseY) < BUTTONH/2){
if(signal == 1){
score= score + 1;
signal = 0;
}
// BONUS: decrease the score the player gets it wrong?
else {
score--;
}
}
}
// 3: check if we're clicking the green button
// if we are and signal is 1, increase the score by 1
if(abs(redX - mouseX) < BUTTONW/2){
if(abs(redY - mouseY) < BUTTONH/2){
if(signal == 2){
score= score + 1;
signal = 0;
}
// BONUS: decrease the score the player gets it wrong?
else {
score--;
}
}
}
}
// this function creates a random color for the signal text
color getRandomColor() {
float r = random(1);
if(r < 0.33) {
return color(255, 0, 0);
} else if(r < .67) {
return color(0, 255, 0);
} else {
return color(255);
}
}