'pygame random points function

im trying to create a function that fill the screen with dots(that will be the points in the game)

the problem is the points created by a for loop and when i run the program the points just move around and wont stay in place.

the code:

def random_points():

point_x = random.randint(0, 900)
point_y = random.randint(0, 500)
rand_color = (random.random(), random.random(), (random.random()))
R = random.randint(1, 4)
for _ in range(1, 5):
    pygame.draw.circle(WIN, rand_color, (point_x, point_y), R)


Solution 1:[1]

You need to create a list of points before the application loop:

point_list = []
for _ in range(1, 5):
    point_x = random.randint(0, 900)
    point_y = random.randint(0, 500)
    rand_color = (random.random(), random.random(), (random.random()))
    R = random.randint(1, 4)
    point = (rand_color, (point_x, point_y), R)
    point_list.append(point)

Draw the points from the list in the application loop:

run = True
while run:
    for event = pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    WIN.fill(0)
    for color, center, rad in point_list:
        pygame.draw.circle(WIN, color, center, rad)
    pygame.disaply.flip()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rabbid76