Search code examples
pythonpygamepygame-surface

why does pygame.display.update stop working after being called a set number of times?


When running I am blit'ing some text onto my surface (WIN) and I want this to be shown in the game so I call pygame.display.update to update the display, this works perfectly fine until the loop has iterated around 75 times and after this the display stops updating. Code below

def dialogue(x,y,text):
    global clock
    global run
    typing=True
    tempstring=""
    counter=0
    switcher=0
    for z in range(0,(len(text))):
        clock.tick(15)
        tempstring=text[0:z+1]
        counter+=1
        print(counter)
        print("temp is "+tempstring)
        writing=TITLE_FONT.render(tempstring, 1, (255,255,255),(0,0,0))
        if x==-1 and y>-1:
            pygame.display.update(WIN.blit(writing, (640-(writing.get_width()//2),y-(writing.get_height()//2))))
        elif x>-1 and y==-1:
            pygame.display.update(WIN.blit(writing, (x-(writing.get_width()//2),360-(writing.get_height()//2))))
        elif x==-1 and y==-1:
            pygame.display.update(WIN.blit(writing, (640-(writing.get_width()//2),360-(writing.get_height()//2))))
        else:
            pygame.display.update(WIN.blit(writing, (x-(writing.get_width()//2),y-(writing.get_height()//2))))
    
    print(typing)
    while True:
        event = pygame.event.wait()
        if event.type == pygame.QUIT:
            run=False
            pygame.quit()
            break
        if event.type == pygame.KEYDOWN:
            break

I have tried just using pygame.display.update() at the end of the line of if statements too but this also fails.


Solution

  • On some systems PyGame doesn't work correctly if you don't get pygame.event from system - system may think that program hangs and it may even close it.

    You may need to use pygame.even.get() (or similar functions) inside your loop.


    In doc pygame.event you can find (but it is hidden in long description)

    To prevent lost events, especially input events which signal a quit command, your program must handle events every frame (with pygame.event.get(), pygame.event.pump(), pygame.event.wait(), pygame.event.peek() or pygame.event.clear()) and process them.

    Not handling events may cause your system to decide your program has locked up.

    To keep pygame in sync with the system, you will need to call pygame.event.pump() internally process pygame event handlers to keep everything current.