Random boxes and circles using array(s)

Hi,

Concerning the array, I would advise you to read the doc about arrayList and maybe try some exemples given with processing.

Now for the random part, I would deal with it that way:

Divide your screen into a certain amount of colomns

For every column:
  get a random number r
  if r < 0.5:
    set the y position to the top
  else:
    set the y position to the bottom

  get another random number r2
  if r2 < 0.2:
    draw a circle in the column at the y position
  else if r2 < 0.4:
    draw a rectangle in the column at the y position
  else:
    draw nothing

Of course you can adjust the numbers to fit the need of your game.
Here is a working exemple:

void setup() {
  size(500, 100);
  background(40);
  noStroke();
  fill(200);
  
  for (int i = 10; i < width; i+=20) {
    float shapeType = random(1);
    float r = random(1);
    int y;
    int w = 10;
    
    if (r < 0.5) {
      y = 10;
    } else {
      y = height - 20;
    }
    
    if (shapeType < 0.2) {
      rect(i, y, w, w);
    } else if (shapeType < 0.4) {
      ellipse(i + w/2.0, y + w/2.0, w, w);
    }
  }
}
1 Like