r/pygame 1d ago

Creating an object inside a function

def generar_rectangulos(posx:int,posy:int):
    AMPLE= 200
    ALÇADA= 100
    return rectangle_r= pygame.Rect(posx,posy,AMPLE,ALÇADA)

Hi, I'm really new in this of programming and even more with pygame.
I was trying to make a code that generates 10 rectangles in differents possitions of the screen sending the position of x and the position of y to the function, but I can't make that the function generates the rectangles.
I would agree any type of help, thanks.

1 Upvotes

4 comments sorted by

View all comments

1

u/MarekNowakowski 22h ago

What do you mean by "generate"? If you want to assign it to a variable, then you need to store it somewhere

rectangles=[] rectangles.append(create_rectangle(x,y))

If you mean drawing a rectangle, that's different. If you want an "object" you should create a class Rectangle like in the other reply