JupiterOrange
JupiterOrange

Reputation: 349

Why isn't my pygame screen updating properly?

I have a small animation where circles are supposed to grow before disappearing and reappearing somewhere else. Kind of like ripples on a pond but one at a time for now. I'm using the pygame library.

The problem is, the screen doesn't update enough and and instead of a empty circle that is growing in radius, I get a filled in circle that gets bigger.

Does anyone know why?

import pygame
import random

pygame.init()

black=[0,0,0]
white=[255,255,255]

size_x=800
size_y=600

size=[size_x,size_y]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Circles")

point=[]

x=size_x/2
y=size_y/2

clock=pygame.time.Clock()

done =False
while done==False:


    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            done=True

    screen.fill(black)

    x+=random.randint(-1,1)*20
    y+=random.randint(-1,1)*20

    if x>size_x:
        x-=size_x

    if y>size_y:
        y-=size_y

    if x<0:
        x+=size_x

    if y<0:
        y+=size_y

    point=[x,y]

    c_1=random.randint(0,255)
    c_2=random.randint(0,255)
    c_3=random.randint(0,255)

    color=[c_1,c_2,c_3]

    for i in range(2,15):

        radius=i
        pygame.draw.circle(screen,color,point,radius,1)

        pygame.display.update()
        clock.tick(15)

Upvotes: 4

Views: 2253

Answers (1)

brentlance
brentlance

Reputation: 2209

It's because you're not clearing the screen after drawing each circle, so they "pile up," generating a filled circle.

Change the innermost loop to:

for i in range(2,15):

    screen.fill(black)
    radius=i
    pygame.draw.circle(screen,color,point,radius,1)

    pygame.display.update()
    clock.tick(15)

will solve the problem, for a single circle. For multiple circles, you will want to remove this innermost loop, and draw one circle for each iteration of the while loop instead.

If you are new to 2D graphics concepts, you may find it useful to read a basic pygame tutorial about moving an image. Good Luck!

Upvotes: 4

Related Questions