hi there! I want to use random function to give this blocks random positions in that canvas, but I really don’t know how to do it. I can make the pink one has y valor between 0 and 500: rect(0,random(0,500),900,400); but I don’t know what to do with yellow one and the others to move them up and down depending on the position of the rest. And my level, as you can see, is absolutely beguinner. So do you have any a idea about how to progress with this? Thank you so much!
If so you could set a variable for the height of the first box,
give it a random value,
draw that box using the variable as the lower corner of the box,
remove the height of that box you’ve just drawn from the random range,
use that height to position the top corner of the next box,
calculate a random height for the next box in the remaining canvas space
and so on.
The random() function in processing allows you to set a upper and lower bounds for the range of values that you’d like to produce. i.e:
float value = random(lower_bounds, upper_bounds);
You can also make use of the built in static variables height and width within the draw loop, as these default to the height and width of the canvas defined by size() in setup()
Oh, thank you so much! That is really interesting and I think about playing with widths, but what I really want to change are the position of the blocks, I mean, pink box has to have a width of 400 px, and the same with the others. Do you think there is a way to move them randomly without hide them behind themselves?
So you’d want to have some sort of data structure for saving the positions of the boxes so that you can make a logical check against them so that none overlap.
You could create a 2D boolean array the same size as the canvas, and when you draw a box set those positions in the array to True (or false), that is, they contain a box.
Then when trying to draw the next box you can check for overlap using that array.
Really this would be kind of a brute force solution for finding valid positions but I think it may do the job?
Thank you so much for you help, I really appreciate it. I don’t find the way to do it, I think that I need to think it out of the application, I dont know what i have to program! But I will think about those things that you told me, thanks again.