在 PyGame 中实时打字 [英] Real time typing in PyGame

查看:88
本文介绍了在 PyGame 中实时打字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小型存钱罐程序.我已经有一个带有一些背景和文本的窗口,但我需要为用户添加一个功能,让他向存钱罐添加钱.当用户按1"时,他可以看到文本您要添加多少?"这就是我的问题开始的地方......这是我有史以来编程的第三天,我什至不知道我在寻找什么.我想实时显示用户输入的数量.有什么提示吗?

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?

到目前为止,这是我的代码(我可以理解:P)

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()

推荐答案

这是一个简短的例子:

#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

这篇关于在 PyGame 中实时打字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆