簡體   English   中英

在 PyGame 中實時打字

[英]Real time typing in PyGame

我正在嘗試編寫一個小型存錢罐程序。 我已經有一個帶有一些背景和文本的窗口,但我需要為用戶添加一個功能,讓他向存錢罐添加錢。 當用戶按“1”時,他可以看到文本“您要添加多少?” 這就是我的問題開始的地方......這是我有史以來編程的第三天,我什至不知道我在尋找什么。 我想實時顯示用戶輸入的數量。 有小費嗎?

到目前為止,這是我的代碼(我可以理解: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

您可能想要執行教程中所述的操作:

"""Edit text with the keyboard."""
import pygame
from pygame.locals import *
import time
 
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GRAY = (200, 200, 200)

pygame.init()
screen = pygame.display.set_mode((640, 240))

text = 'this text is editable'
font = pygame.font.SysFont(None, 48)
img = font.render(text, True, RED)

rect = img.get_rect()
rect.topleft = (20, 20)
cursor = Rect(rect.topright, (3, rect.height))

running = True
background = GRAY

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        
        if event.type == KEYDOWN:
            if event.key == K_BACKSPACE:
                if len(text)>0:
                    text = text[:-1]
            else:
                text += event.unicode
            img = font.render(text, True, RED)
            rect.size=img.get_size()
            cursor.topleft = rect.topright
    
    screen.fill(background)
    screen.blit(img, rect)
    if time.time() % 1 > 0.5:
        pygame.draw.rect(screen, RED, cursor)
    pygame.display.update()

pygame.quit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM