pygame text flickers

Question:

I’m making a game on pygame and I want to add text, but I encounter text flickering. Can someone help to sort this out? Here is part of the code:

def start_game():
    fon1_demo = pygame.image.load('fon1remake2.png')
    fon1 = pygame.transform.scale(fon1_demo, (1920, 1080))
    diktor_demo = pygame.image.load('diktor.png')
    diktor = pygame.transform.scale(diktor_demo, (950, 870))

    playgame_btn = Button(70, 70)

    fonidet = True
    while fonidet:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        print_text('Test.', 200, 900, font_color=(255, 255, 255), font_type='01.ttf', font_size = 30)
        print_text('Test!', 200, 950, font_color=(255, 255, 255), font_type='01.ttf', font_size=30)
        print_text('(Нажмите на ">>>" слева)', 200, 1000, font_color=(255, 255, 255), font_type='01.ttf', font_size=30)

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

        display.blit(fon1, (0, 0))
        playgame_btn.draw(80, 930, '>>>')
        display.blit(diktor, (1150, 58))
        pygame.display.update()
        clock.tick(60)
Asked By: lobzinc

||

Answers:

Just update the display once at the end of the application loop. Multiple updates of the display causes flickering:

while fonidet:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    print_text('Test.', 200, 900, font_color=(255, 255, 255), font_type='01.ttf', font_size = 30)
    print_text('Test!', 200, 950, font_color=(255, 255, 255), font_type='01.ttf', font_size=30)
    print_text('(Нажмите на ">>>" слева)', 200, 1000, font_color=(255, 255, 255), font_type='01.ttf', font_size=30)

    #pygame.display.update()                  <-- DELETE
    #clock.tick(15)

    display.blit(fon1, (0, 0))
    playgame_btn.draw(80, 930, '>>>')
    display.blit(diktor, (1150, 58))
    pygame.display.update()
    clock.tick(60)
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.