why does the while loop keep making the game crash in pygame?

前端 未结 2 1829
既然无缘
既然无缘 2020-12-21 14:43

the code works just fine until I add the while true: also for some reason, the sleep function makes the entire code wait. however, I want only the parts after sleep() to wai

2条回答
  •  爱一瞬间的悲伤
    2020-12-21 15:36

    You can see that in your program, no call is being made to the event queue. In the pygame documentation for pygame.event.pump(), it says

    "If you fail to make a call to the event queue for too long, the system may decide your program has locked up."

    This causes the program to seem to be unresponsive. To solve this call the function pygame.event.pump() after pygame.init() so that the events are internally handeled

    import pygame, sys
    pygame.init()
    pygame.event.pump()
    from time import sleep
    
    screen = pygame.display.set_mode((500,400))
    PINK = (255,192,203)
    WHITE = (255,255,255)
    screen.fill(PINK)
    pygame.display.update()
    
    
    font = pygame.font.SysFont("comicsansms", 72)
    text = font.render("loading", True, WHITE)
    textrect = text.get_rect()
    textrect.center = (225,40)
    screen.blit(text,textrect)
    
    
    while True:
        sleep(1)
        text = font.render(".", True, WHITE)
        textrect = text.get_rect()
        textrect.center = (350,40)
        screen.blit(text,textrect)
        sleep(0.5)
        text = font.render(".", True, WHITE)
        textrect = text.get_rect()
        textrect.center = (370,40)
        screen.blit(text,textrect)
        sleep(0.5)
        text = font.render(".", True, WHITE)
        textrect = text.get_rect()
        textrect.center = (390,40)
        screen.blit(text,textrect)
        sleep(0.5)
    

提交回复
热议问题