我几乎已经完成了我正在为学校项目制作的一款游戏,但现在我在游戏的一小部分上遇到了困难。我能够获取用户的姓名并使用它来将其写入排行榜 csv 文件,但我想要这样做,以便无论用户键入什么,游戏都会将用户的输入打印到屏幕上,就像您键入时一样在搜索框中,无论您输入什么键,该键都会显示在搜索框中。
只需创建一个字体对象,用它来渲染文本(这会给你一个pygame.Surface
),然后将文本表面传输到屏幕上。
另外,要将字母添加到user_input
字符串,你可以将它与event.unicode
属性。
这是一个最小的例子:
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
FONT = pg.font.Font(None, 40) # A font object which allows you to render text.
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue1')
user_input = ''
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_BACKSPACE:
user_input = user_input[:-1]
else:
user_input += event.unicode
screen.fill(BG_COLOR)
# Create the text surface.
text = FONT.render(user_input, True, BLUE)
# And blit it onto the screen.
screen.blit(text, (20, 20))
pg.display.flip()
clock.tick(30)
pg.quit()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)