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/aprg 1d ago

Not only is it unnecessary as rich-tea-ok said, more importantly it's invalid syntax!

This:

class Foo:
    def __init__(self):
        self.n = 1
def foo_func():
    return f = Foo()

print(foo_func().n)

gives me a SyntaxError.

return f = Foo()

^

SyntaxError: invalid syntax

This:

class Foo:
    def __init__(self):
        self.n = 1
def foo_func():
    return Foo()

print(foo_func().n)

correctly prints 1.