栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/638056.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号