I'm trying to create a generative world with four randomly generated (location, size, colour) objects, each mouseClicked is supposed to regenerate those four objects in a random location, size, colour, etc. I have a method to draw the objects, now I'm trying to figure out how to actually draw the limited number of objects that I want because now the code will just constantly generate more and more objects.
final int NUM_OBJECTS = 4;
int numObjectsLeft;
float x;
float y;
float radius;
void renderBackground() {
//sky
fill(135, 206, 235);
rect(0, 0, width, height);
//grass
fill(86, 125, 70);
rect(0, 500, 1400, 750);
//Pathway
fill(181, 101, 29);
rect(0, 600, 1400, 100);
//Buildings
fill(128, 128, 128);
rect(900, 300, 100, 200);
rect(1000, 350, 80, 150);
fill(160, 220, 222);
rect(915, 315, 30, 30);
rect(955, 315, 30, 30);
rect(915, 355, 30, 30);
rect(955, 355, 30, 30);
rect(915, 395, 30, 30);
rect(955, 395, 30, 30);
rect(915, 435, 30, 30);
rect(955, 435, 30, 30);
rect(1010, 360, 25, 25);
rect(1045, 360, 25, 25);
rect(1010, 395, 25, 25);
rect(1045, 395, 25, 25);
rect(1010, 430, 25, 25);
rect(1045, 430, 25, 25);
//Trees
fill(102, 51, 0);
rect(275, 430, 50, 100);
fill(97, 138, 61);
ellipse(300, 400, 100, 100);
fill(102, 51, 0);
rect(380, 450, 40, 75);
fill(97, 138, 61);
ellipse(400, 425, 80, 80);
//Hills
fill(122, 115, 114);
triangle(100, 500, 200, 500, 150, 300);
triangle(175, 500, 225, 500, 200, 400);
}
void renderObject(float x, float y, float radius, int numObjectsLeft) {
fill(255);
line(x, y+(radius/2), x, y+radius+30);
line(x, y+radius-5, x+(radius/2), y+radius+25);
line(x, y+radius-5, x-(radius/2), y+radius+25);
line(x, y+radius+30, x+(radius/2), y+radius*2+25);
line(x, y+radius+30, x-(radius/2), y+radius*2+25);
ellipse(x, y, radius, radius);
}
void renderForeground() {
numObjectsLeft = NUM_OBJECTS;
x = random(0, 1400);
y = random(500, 700);
radius = 30;
numObjectsLeft--;
renderObject(x,y,radius,numObjectsLeft--);
if (numObjectsLeft > 0) {
renderObject(x,y,radius,numObjectsLeft);
if(numObjectsLeft <= 0) {
exit();
}
}
}
void setup() {
size(1400, 750);
renderBackground();
numObjectsLeft = NUM_OBJECTS;
}
void draw() {
x = random(0, 1400);
y = random(500, 700);
radius = 30;
renderForeground();
}
void mousePressed() {
}