Random falling rectangles

Hi, I would like to know if anyone can help me. I want to have rectangles fall from the top of the window in random x positions, but I am not sure how to do that. If anyone could help, that would be great. I currently don’t have any code for this program. Thanks in advance

Hello, and welcome to the forum!

Great to have you here!

Please go to the processing.org website and check the examples. https://www.processing.org/examples/linear.html - for example section Motion.

You need setup and draw in your Sketch.

See https://www.processing.org/tutorials/overview/

Then use rect() to display an rectangle. See reference https://www.processing.org/reference/

Its x is float x; before setup (also float y; before setup)

x=random(width); in setup after size

Then in draw(): y++;

Unfortunately, we are not allowed to show you Code.



@Chrisir’s outlined it nicely for you. You’ve posted your question in the Processing.py section, so I’m assuming you’re after Python advice? For Python Mode, the syntax is a little different:

  • To setup and draw in your sketch:

    def setup():
        size(200, 200)
    def draw():
        # draw shapes here
  • For drawing rectangles, refer to the relevant Python Mode reference entry: https://py.processing.org/reference/rect.html

  • x = random(width) is the same in Python.

  • In Python, y++ becomes y += 1. You’ll need to make it a global variable, too.

For more documentation, you can refer to the Processing.py website.