当我们在键盘上打字时,如何让字符串的内容出现在屏幕上? [英] How to make a string's content appears on screen as we type on keyboard?
问题描述
我有这个功能,玩家可以在其中输入他的名字,但我希望每个字母在他输入时出现在屏幕上.这是我的功能:
def input_player_name():player_name_screen = True姓名 = ""win.blit(player_name_bg, (0, 0))而 player_name_screen:对于 pygame.event.get() 中的事件:如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_RETURN:打印(名称)player_name_screen = False别的:名称 += event.unicodepygame.display.update()时钟滴答(fps)
如果我在 name+=event.unicode
之后写 print(name)
,每个输入的东西都会出现在控制台中.我是否必须使用这样的东西
textsurface = game_font.render(str(name), False, (255, 255, 255))win.blit(textsurface, (0, 0))
并在每次有新内容进入 name
时更新它?感谢您的帮助
您可以使用
导入pygame导入 pygame.fontpygame.init()赢 = pygame.display.set_mode((500, 200))时钟 = pygame.time.Clock()每秒帧数 = 60def input_player_name():# 创建字体和文本表面font = pygame.font.SysFont(None, 100)name_surf = font.render('', True, (255, 0, 0))player_name_screen = True姓名 = ""而 player_name_screen:对于 pygame.event.get() 中的事件:如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_RETURN:player_name_screen = False别的:名称 += event.unicode# 重新创建文本表面name_surf = font.render(name, True, (255, 0, 0))win.blit(player_name_bg, (0, 0))# blit 文本到窗口win.blit(name_surf, (50, 50))pygame.display.update()时钟滴答(fps)input_player_name()
I have this function, where player can type his name, but I want each letter to appear on the screen as he types them. Here is my function :
def input_player_name():
player_name_screen = True
name = ""
win.blit(player_name_bg, (0, 0))
while player_name_screen:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
print(name)
player_name_screen = False
else:
name += event.unicode
pygame.display.update()
clock.tick(fps)
If I write print(name)
right after name+=event.unicode
, each thing typed appears in the console. Do I have to use something like this
textsurface = game_font.render(str(name), False, (255, 255, 255))
win.blit(textsurface, (0, 0))
and make it update each time something new goes into name
?
Thanks for your help
You can either use pygame.font
or pygame.freetype
. In the following I use
pygame.font
.
Waht you have to do is to generate a font
object and render the text to a pygame.Surface
(name_surf
). This surface has to be blit
to the window continuously in the loop. When the name changes, the the surface has to be recreated:
import pygame
import pygame.font
pygame.init()
win = pygame.display.set_mode((500, 200))
clock = pygame.time.Clock()
fps = 60
def input_player_name():
# create font and text surface
font = pygame.font.SysFont(None, 100)
name_surf = font.render('', True, (255, 0, 0))
player_name_screen = True
name = ""
while player_name_screen:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
player_name_screen = False
else:
name += event.unicode
# recreate text surface
name_surf = font.render(name, True, (255, 0, 0))
win.blit(player_name_bg, (0, 0))
# blit text to window
win.blit(name_surf, (50, 50))
pygame.display.update()
clock.tick(fps)
input_player_name()
这篇关于当我们在键盘上打字时,如何让字符串的内容出现在屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!