Spaces between objects within an array and between arrays

Guys,

I got a question. I am a beginner at coding. I am trying to program a little game but I’m already stuck.

Basically, I have set up two arrays with a number of objects within both of them. Now I’ve encountered a little problem. I want there to be spaces between each object within the array and also between the objects in both arrays. Meaning: I don’t want objects touching one another. Does anyone know how to achieve that?

In Processing, I’m currently using three tabs, I’ll post the code in order.

float borderWidth = 40;                       // Width of the wall 
color borderColor = color(191, 10, 48);     // Color of the wall
PImage img = createImage(70, 50, RGB);


void setup () {
  size (1000, 500, P2D);
  for (int i = 0; i<burgers.length; i++) 
  {
    burgers[i] = new Burger();
  };
  for (int j = 0; j<russias.length; j++)
  {
    russias[j] = new Russia();
  }
}
void draw () {
  //background
  background (0, 40, 104);

  // Wall
  fill(borderColor);
  noStroke();
  rect(0, 0, borderWidth, height);
  rect(width-borderWidth-1, 0, borderWidth, height);
  rect(0, 0, width, borderWidth);
  rect(0, height-borderWidth-1, width, borderWidth);

  for (int i = 0; i<burgers.length; i++) {
    burgers[i].move();
    burgers[i].display();
  }
  for (int j = 0; j<russias.length; j++)
  {
    russias[j].display();
    russias[j].move();
  };
  fill(borderColor);
  noStroke();
  rect(0, 0, borderWidth, height);
  rect(width-borderWidth-1, 0, borderWidth, height);
  rect(0, 0, width, borderWidth);
  rect(0, height-borderWidth, width, borderWidth);
  //All objects are made to display and move. They have been coded in seperate tabs.
}
Burger[] burgers =  new Burger[10];

class Burger {
  float xpos;
  float ypos;
  float burgerWidth;
  float yspeed;
  int colorA;
  int colorBread;

  Burger () {
    xpos = random(borderWidth, width-100);
    ypos = random(-1000, -50);
    burgerWidth = 60;
    yspeed = 2;
    colorA = 255;
    colorBread = 156;
  }

  void display () {
    stroke (0); 
    fill (colorA, colorBread, 0); //top bread
    arc  (xpos+30, ypos, burgerWidth, 50, PI, TWO_PI);
    fill (236, 56, 40); //tomato
    rect (xpos, ypos, burgerWidth, 8, 3, 6, 12, 18);
    fill (colorA, 218, 0); //cheese
    rect (xpos, ypos+4, burgerWidth, 4, 3, 6, 12, 18);
    fill (174, 97, 66); // meat
    rect (xpos, ypos+7, burgerWidth, 10, 3, 6, 12, 18);
    fill (0, 218, 83); //salad 
    rect (xpos, ypos+17, burgerWidth, 7, 3, 6, 12, 18);
    fill (colorA, colorBread, 0); //bottom bread
    rect (xpos, ypos+23, burgerWidth, 12, 0, 0, 12, 18);
  }

  void move () {
    ypos = ypos + yspeed;
    if (ypos>height) {
      ypos=random(-1000, 50); 
      xpos=random(100, width-100);
    }
  }
}
Russia[] russias = new Russia[10];

class Russia {
  float xRussia;
  float yRussia;
  float russiaWidth;
  float russiaHeight;
  float yspeed;

  Russia () {
    xRussia = random(borderWidth+20, width-120);
    yRussia = random(-1000, -50);
    russiaWidth = 60;
    russiaHeight = 15;
    yspeed = 2;
  }

  void display() {
    stroke (255); 
    fill (255); 
    rect(xRussia, yRussia, russiaWidth, russiaHeight);
    stroke (0, 57, 166); 
    fill (0, 57, 166); 
    rect(xRussia, yRussia+15, russiaWidth, russiaHeight);
    stroke (213, 43, 30); 
    fill (213, 43, 30); 
    rect(xRussia, yRussia+30, russiaWidth, russiaHeight);
  }

  void move () {
    yRussia = yRussia + yspeed;
    if (yRussia>height) {
      yRussia=random(-1000, -50); 
      xRussia=random(borderWidth+20, width-120);
    }
  }
}
1 Like

Hi german_guy,

To have the items not touch horizontally you could position the items ‘on a grid’. For example: your burgerWidth is 60px. The width of the window is 1000. That means you can fit about 16 burgers next to each other. If you divide your random picked xpos by the burgerWidth you get a small number in the range 0,0 to 16,6. You can then then round that value to a whole number for example using int(). That will give you a whole number between 0 and 16. If you multiply that with the burgerWidth your burgers are nicely separated in horizontal direction. Here’s the code:

xpos = int(random(borderWidth, width-100) / burgerWidth) * burgerWidth;

Best wishes,
Rick

2 Likes

Thank you, Rick! “On a grid” - I see what you did there :wink: