当我们在键盘上打字时,如何使字符串的内容出现在屏幕上?

当我们在键盘上打字时,如何使字符串的内容出现在屏幕上?,第1张

当我们在键盘打字时,如何使字符串的内容出现在屏幕上?

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 pygameimport pygame.fontpygame.init()win = pygame.display.set_mode((500, 200))clock = pygame.time.Clock()fps = 60def 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.unipre         # 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()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5643875.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存