Real time typing in PyGame

2019-08-18 07:15发布

问题:

I am trying to write a small piggybank program. I already have a window with some background and text but I need to add a functionality for the user to let him add money to the piggybank. When the user press "1" he can see the text "how much do you want to add?" and here is where my problem begins... It's my third day with programming ever and I don't even know what I am looking for. I want to to show in real time what amount user is typing. Any tips?

Here's my code so far (which I can understand :P)

    # wyświetlenie napisów
    font = pygame.font.SysFont("comic sans MS", 15, bold=True) #ustawienie czcionki
    text = font.render("::Swinka 1.4::",False,(BLACK))
    screen.blit(text, [150,0])
    pygame.draw.line(screen, BLACK, [0,20], [400,20], 3)
    text = font.render("Chlewik",False,(BLACK))
    screen.blit(text,[145,50])
    text = font.render("Aktualny Stan: " +stan_konta,False,(BLACK))
    screen.blit(text,[145,110])
    text = font.render("1. Nakarm mnie",False,(BLACK))
    screen.blit(text,[10,150])
    text = font.render("2. Dieta",False,(BLACK))
    screen.blit(text,[10,170])

    # obsługa klawiszy
    if event.type == pygame.KEYDOWN: # ogólne wywołanie wciśnięcia klawisza
        if event.key == pygame.K_2: # naciśnięcie konktretnego przycisku, w tym przypadku "2"
            #screen.fill(PINK) # odświeżenie ekranu (wycyszczenie plus pomalowanie)
            screen.blit(background, [0,0])
            stan_konta = 0 # przypisanie początkowej wartości konta
            stan_konta=str(stan_konta) # zamiana z liczb na znaki
            plik = open("dane/amount.txt", "w") # zapisanie do pliku 
            plik.write(stan_konta)
            plik.close()
            text = font.render("Swinka pusta!",False,(BLACK))
            screen.blit(text, [185,170])
        if event.key == pygame.K_1:
            #screen.fill(PINK)
            screen.blit(background, [0,0])
            text = font.render("Ile mam zjesc?",False,(BLACK))
            screen.blit(text,[185,150])


    pygame.display.flip()
pygame.quit()

回答1:

Here is a short example:

#creates a new string, that will store the character you have written
number_str = "" 

#create a new Font object that is used to render the string into a Surface object
font_renderer = pygame.font.Font("monospace", 15) 

while True:
    screen.fill((0,0,0)) # fill the whole screen with a black color
    # create a new Surface object from a string, where the text is white.
    rendered_number = font_renderer.render(number_str, True, (255,255,255))

    #draws the created Surface onto the screen at position 100,100
    screen.blit(rendered_number, (100, 100))
    # updates the screen to show changes
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN: 
            if pygame.KEY_0 < event.key < pygame.KEY_9: # checks the key pressed
                character = chr(event.key) #converts the number to a character
                number_str += str(character) #adds the number to the end of the string