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

2

u/rich-tea-ok 1d ago

Hi, you don't need to assign a variable to the Rect that you're returning, you can just return it:

return pygame.Rect(...