Highlighting a rect for some time

Hi,

ich try to get into processing and thought I’ll programm a small game. I googled for some game ideas and found the game simon that seemed to be pretty simple to program.

I have defined my colors and stored them in an array. I use this to draw the rects in draw() and it works. newRound is a new boolean I initialize with true.

Now comes the interesting part. I try to highlight the rectangles. At the beginning of the game 1 rect, then 2 then 3 and so on. The rects to highlight are stored in an array that gets expanded by one every new round.

However highlighting doe not seem to work. I already tried delay(), loop()and noLoop(), currentFrame (Framerate is 30) and millis(), however none of those methods seem to actually work.

I think millis()seems to be the best solution, but it just does not work. For testing I’ve set the wait time to 5000ms. The recs flash, but they ignore the time parameter.

Does anyone have a suggestions how I can actuall let the square be a specific time bright and then go back to the default color?

Thanks in advance.

Here is the whole code:

  //definde layout variables
  int FLASHSPEED = 5000;
  int FLASHDELAY = 200;
  int BUTTONSIZE = 200;
  int BUTTONGAPSIZE = 20;
  int TIMEOUT = 4000; // timout in milliseconds 

  // define colors used
  color WHITE        = color(255, 255, 255);
  color BLACK        = color(  0,   0,   0);
  color BRIGHTRED    = color(255,   0,   0);
  color RED          = color(155,   0,   0);
  color BRIGHTGREEN  = color(  0, 255,   0);
  color GREEN        = color(  0, 155,   0);
  color BRIGHTBLUE   = color(  0,   0, 255);
  color BLUE         = color(  0,   0, 155);
  color BRIGHTYELLOW = color(255, 255,   0);
  color YELLOW       = color(155, 155,   0);
  color DARKGRAY     = color( 40,  40,  40);
  color[] BRIGHT     = {BRIGHTRED, BRIGHTGREEN, BRIGHTBLUE, BRIGHTYELLOW};
  color[] DEFAULT    = {RED, GREEN, BLUE, YELLOW};
  
  //set the margins
  int XMARGIN;
  int YMARGIN;
  
  // Game variables
  int time;
  int[] pattern = {};
  int currentPattern = 0;
  int currentStep = 0; // the color the player must push next
  int score = 0;
  boolean waiting = false;
  boolean waitingForInput = false; //when False, the pattern is playing. when True, waiting for the player to click a colored button
  boolean playMode = true;
  
void setup() {
  size(840, 680); // window size
  noStroke(); // not outline
  frameRate(30);
  time = 0;
  
  //set the margins
  XMARGIN = int((width - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2);
  YMARGIN = int((height - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2);
}

void draw(){
  if(playMode){
    // reset to black background
    background(0); 
    
    // draw basic rectangles
    drawRect(0, false);
    drawRect(1, false);
    drawRect(2, false);
    drawRect(3, false);
    
    // draw info text
    textSize(24);
    fill(WHITE);
    text("Match the pattern by clicking on the button.", 10, height - 25);
    
    // draw score
    text(score, 10, 25);
    
    if(waitingForInput == false){
      if(currentPattern == 0){
          pattern = expand(pattern, pattern.length+1);
          pattern[pattern.length-1] = int(random(4));
      }
      if(waiting){
        if(waitTime()){
          drawRect(pattern[currentPattern],true);
        } else {
          waiting = false;
        }
      } else {
        time = millis() + FLASHSPEED;
        waiting = true;
      }
      
      currentPattern++;
      
      if(currentPattern == pattern.length){ 
        currentPattern = 0;  
        waitingForInput = true;
      }
    } else {
      score++;
      waitingForInput = false;
    }
  } else {
   //game over 
  }
}
void drawRect(int number, boolean bright){
  if(bright){
    fill(BRIGHT[number]);
  } else {
    fill(DEFAULT[number]);
  }
  switch(number){
    case 0:
      rect(XMARGIN, YMARGIN, BUTTONSIZE, BUTTONSIZE);
      break;
    case 1:
      rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN, BUTTONSIZE, BUTTONSIZE);
      break;
    case 2:
      rect(XMARGIN, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE);
      break;
    case 3:
    rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE); 
    break;
  }
}

boolean waitTime(){
  if(time > millis()){
    return true;
  }
  return false;
}

void gameOver(){
 background(0); 
  text("Game Over!", 10, height /2);
  playMode = false;
}
1 Like

OK, it seemes like I had several bugs in my code. I’ve changed the waitingForInput == false part in draw() and not it works as intended:

if(waitingForInput == false){
      // add a new element to the pattern if it's a new round
      if(newRound){
          pattern = expand(pattern, pattern.length+1);
          pattern[pattern.length-1] = int(random(4));
          newRound = false;
      }
      
      // flash the rects
      if(waiting){
        if(time > millis()){
          drawRect(pattern[currentPattern],true);
        } else {
          if(time +FLASHDELAY < millis()){
            currentPattern++;
            waiting = false;
            if(currentPattern == pattern.length){ 
              currentPattern = 0;  
              waitingForInput = true;
            }
          }
        }
      } else {
        time = millis() + FLASHSPEED;
        waiting = true;
      }
    }
2 Likes