Help! Clocks! - Minutes & For Loops

Hello!

I’m in the process currently of trying to make a program that runs a type of clock. I am using images in my program, but for the purposes of my question, I have substituted ellipses. I understand how to use a nested loop to draw 60 circles, which represent the minutes, but I can’t figure out how to make them appear as the minutes go up.

For example, if it was 11:36 pm on your computer, I would want the program 36 ellipses. It would then draw a new one to add to the minute count each time the minute changed: 11:36 pm > 36 ellipses, 11:37pm > 37 ellipses.

I want the ellipses to stay in the same layout as my 60 ellipses nested for loop, but only having as many visible as the minutes.

Here is what I have:


void setup(){
  size(1000, 600);
}

void draw(){
  for(int xPosition = 0; xPosition < 1000; xPosition = xPosition + 100){
    for(int yPosition = 0; yPosition < 600; yPosition = yPosition + 100){
         ellipseMode(CORNER);
         ellipse(xPosition, yPosition, 100, 100);
        } 
      }
}

 
2 Likes

The solution is in two parts, first use background(…) to clear the display at the start of every frame otherwise you will not see any changes once all 60 pictures are shown. Use a counter to control the number of pictures visible like this

void setup() {
  size(1000, 600);
}

void draw() {
  background(230);
  int m = minute();
  for (int xPosition = 0; xPosition < 1000; xPosition = xPosition + 100) {
    for (int yPosition = 0; yPosition < 600; yPosition = yPosition + 100) {
      ellipseMode(CORNER);
      ellipse(xPosition, yPosition, 100, 100);
      m--;
      if (m == 0) break; // breakout of inner loop
    }
    if (m == 0) break;  // breakout of outer loop
  }
}```
4 Likes

Thank you so much! You’re a lifesaver!

Small thing: what’s the definition of “break”? is that the counter?

break is a Java keyword just like if, for, int

In this context it means leave the enclosing loop now effectively ending the for-loop.

So in this loop.

for(int i = 0; i < 10; i++){
  println(i);
  if (i == 5) break;
}

you will see the numbers 0 to 5 inclusive but i will never reach 6

In your code we need two such statements the first to exit the inner loop and the second to exit the outer loop.

4 Likes