简体   繁体   中英

How do I make the Pygame time.delay() run without freezing my window?

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 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM