Drawing an faulty advent calendar

Hey guys, I hope you are having a nice pre-christmas time…

For my university homework I’ll have to draw an advent calendar which (in the first step) has to be drawn by a loop and a draw_advent_calendar_door(float x, float y, int number) function.

It draws the windows quiet nicely, but sadly I am stuck displaying the right number. Everything, including the constrain function, I’ve tried before does not work for me so far.

I would like to ask for some Ideas, how I can better try to solve this. Please do not provide a definite solutions as this is my homework and I’ll have to solve it myself.

Thank you in advance!

PS: I forgot to mention that we have doors from day 1 to day 24.

int doorNr = 1;

void setup() {
  size(777, 500);
  background(0, 150, 0);
}


void draw() {
  rectMode(CENTER);

  for (float i = 111; i <= 666; i += 111) {
    for (float a = 100; a <= 400; a += 100) {
      //for (int e = 1; e <= 24; e++) {

      draw_advent_calendar_door(i, a, doorNr);


      // }
    }
  }
  doorNr++;
  constrain(doorNr, 1, 24);
}

void draw_advent_calendar_door(float x, float y, int number) {
  fill(255);
  rect(x, y, 75, 75);
  fill(200, 0, 0);
  textAlign(CENTER, CENTER);
  textSize(60);
  text(number, x, y-10);
}

there are several solutions.

Place

 doorNr++;

right after

      draw_advent_calendar_door(i, a, doorNr);

So that the number increases every step/door.

Disadvantage is that you have a fixed order which is a bit boring.

Solution

You can also use an array (defined before setup()) with the numbers 1…24 and shuffle it.

int[] listOfNumbers = { 13, 7, 1, 24, ... };

and then use doorNr as an index (it gets bigger by 1 every step, like mentioned above) for that array:

listOfNumbers [doorNr];

2 Likes

Thank you for your reply @Chrisir ! I’ve tried to write doorNr++ right after the function inside the loop. But then the numbers keep counting up and are no stopping, even with the constrain function.

Please only give me a hint how to fix this. I’ll have to do everything myself for the university :slight_smile: .

The array to receive random numbers seems to be way better. But we have not learned about this yet, so I guess I must not use it…

Right

Reset the variable before the for loops please

2 Likes

Thank you very much!

1 Like