center argument must be a pair of numbers

Viewed 492

I'm trying to make it so that the script generates a random x position for a shape that will be created.

I am rather new to python so I have no idea how to fix this.

Code section: (inside of pygame game loop)

class falling_object():
    def __init__(self):
        pass

    def create():
        global random_x
        random_x = random.randint(10, 470)
        random_size = random.randint(20, 40)
        pygame.draw.circle(screen, (255, 255, 0), random_x, falling_y, random_size, random_size) # error is here at the random_x and random_size part, falling_y is a variable equal to 0

Where the function is called:

obj1 = falling_object()
falling_object.create()
1 Answers

The center argument of pygame.draw.circle needs to be a tuple with the x and y coordinate:

pygame.draw.circle(screen, (255, 255, 0), random_x, falling_y, random_size, random_size)

pygame.draw.circle(screen, (255, 255, 0), (random_x, falling_y), random_size) 

Create the coordinates of the sphere in the constructor and draw the sphere in a mthod:

class falling_object():
    def __init__(self):
        self.x = random.randint(10, 470)
        self.y = falling_y
        self.size = random.randint(20, 40)

    def create(self):
        self.y = falling_y
        pygame.draw.circle(screen, (255, 255, 0), (self.x, self.y), self.size)

Construct the sphere before the application loop, but draw it in the loop:

obj1 = falling_object()

# [...]

while running:

    # [...]  

    obj1.create()
Related