0

I am having some trouble with pygame. I am running Mac High Sierra, using Python 3, and using Spyder as my compiler. I am just trying to get a simple animation to run, but the time.delay() function is not working. Whenever I run my code, the pygame window opens, remains grey, and does not fill with white until after the time delays have all run. Then it displays my white screen and the final location of the circle.

How can I get the time.delay function to run properly without just freezing the pygame window?

import pygame, sys

pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])

pygame.draw.circle(screen, [255,0,255], [50,50],50, 0)
pygame.display.flip()

for x in range(5,640,5):
    pygame.time.wait(20)
    pygame.draw.rect(screen, [255,255,255],[x-5,0,100,100],0)
    pygame.draw.circle(screen, [255,0,255], [50+x,50],50, 0)
    pygame.display.flip()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
  • It's never a good idea to use delay in event driven programs, as the program needs to communicate with your operating system (explained [here](https://stackoverflow.com/a/42719689/6486738)). You need to either calculate when to perform some action (as The4thIceman did in their answer), or use events (as explained [here](https://stackoverflow.com/a/50237012/6486738)) – Ted Klein Bergman Dec 06 '18 at 20:31

1 Answers1

1

It runs fine on my computer. So it is hard to say, but I believe the issue is the design of the code. Generally, all drawing and animation should happen within the main loop (while True) and there shouldn't be a need to add any time delays.

x = 0  # keep track of the x location
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # clear the screen each time
    screen.fill([255, 255, 255])

    # draw the circle (notice the x variable wrapped in int())
    pygame.draw.circle(screen, [255, 0, 255], [int(50 + x), 50], 50, 0)

    # adjust higher or lower for faster animations
    x += 0.1

    # flip the display
    pygame.display.flip()

Now the animation happens in sync with the main loop. Remember that pixels on the screen are counted in integers, so any float operations you do (such as x += 0.1) need to be an int when drawn to the screen.

If you don't want to deal with floats and decimals, you would have the minimum speed adjustment set to 1, but only adjust it every certain number of frames

x = 0  # keep track of the x location
frames = 0  # keep track of frames
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # clear the screen each time
    screen.fill([255, 255, 255])

    # draw the circle (notice no int() function here)
    pygame.draw.circle(screen, [255, 0, 255], [50 + x, 50], 50, 0)

    # every 50 frames, increase movement. adjust higher or lower for varying speeds
    if frames % 50 == 0:
        x += 1  # never going to be a float, minimum of 1
    frames += 1

    # flip the display
    pygame.display.flip()
The4thIceman
  • 3,709
  • 2
  • 29
  • 36