我正在尝试编写一个小型的储钱计划。我已经有了一个带有一些背景和文字的窗口,但我需要为用户添加一项功能,让他为moneybank添加资金。当用户按下" 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()
答案 0 :(得分:3)
这是一个简短的例子:
#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
答案 1 :(得分:0)
您可能想要做一些类似于 this 教程中解释的事情:
"""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()