1
votes

I'm making a game in pygame, and when making this game I use a lot of text on the screen. but when I get to this point in my code the text that is written in first doesn't blink but the second does and I would like to change that. Also when I hold a key that letter gets written rapidly over and over when I only want it to write that letter once. Code:

def game_loop():

    while True:

        clock = pygame.time.Clock()




        def enter_name():
            global kiriko
            global pressed_key
            global ikey
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_1 :
                        pressed_key = '1'

                    if event.key == pygame.K_2 :
                        pressed_key = '2'


                    if event.key == pygame.K_3 :
                        pressed_key = '3'


                    if event.key == pygame.K_4 :
                        pressed_key = '4'


                    if event.key == pygame.K_5 :
                        pressed_key = '5'


                    if event.key == pygame.K_6 :
                        pressed_key = '6'


                    if event.key == pygame.K_7 :
                        pressed_key = '7'


                    if event.key == pygame.K_8 :
                        pressed_key = '8'


                    if event.key == pygame.K_9 :
                        pressed_key = '9'


                    if event.key == pygame.K_0 :
                        pressed_key = '0'


                    if event.key == pygame.K_q :
                        pressed_key = 'q'


                    if event.key == pygame.K_w :
                        pressed_key = 'w'

    #same thing but for every letter in the alphabet...


                    if event.key == pygame.K_PLUS :
                        pressed_key = '+'


                    if event.key == pygame.K_BACKSPACE :
                        kiriko = kiriko[:-1]

                if event.type == pygame.KEYUP:
                    pressed_key = ''



        try:

            message_small(kiriko, display_width / 2, display_height / 1.3)
            message_display("Enter your name")
            pygame.display.update()
            window.fill(white)
            enter_name()
            kiriko += pressed_key
            message_small(kiriko, display_width / 2, display_height / 1.3)
            message_display("Enter your name")
            pygame.display.update()
            clock.tick(120)
        except:
            message_small(kiriko, display_width / 2, display_height / 1.3)
            message_display("Enter your name")
            pygame.display.update()
            window.fill(white)
            enter_name()
            message_display("Enter your name")
            pygame.display.update()
            clock.tick(120)

EDIT: Now the only problem is blinking text as I got a solution for that

1
Also when I hold a key that letter gets written rapidly over and over when I only want it to write that letter once That's because you're listening to the KEYDOWN event... if you only want it triggered once, listen to the KEYUP instead - Michael
@Thanks, it worked. sry for the dumb question. though I don't see an answer to the flashing screen - Adde21_30

1 Answers

0
votes

I believe the reason your text is blinking is because your calling pygame.display.update() in way to many places. Just call pygame.display.update() once near the end of your code.

~Mr.Python