python中font的用法_如何在pygame中使用pygame.font.font()?

这是改编自我写的一些非常古老的代码:def renderTextCenteredAt(text, font, colour, x, y, screen, allowed_width):

# first, split the text into words

words = text.split()

# now, construct lines out of these words

lines = []

while len(words) > 0:

# get as many words as will fit within allowed_width

line_words = []

while len(words) > 0:

line_words.append(words.pop(0))

fw, fh = font.size(' '.join(line_words + words[:1]))

if fw > allowed_width:

break

# add a line consisting of those words

line = ' '.join(line_words)

lines.append(line)

# now we've split our text into lines that fit into the width, actually

# render them

# we'll render each line below the last, so we need to keep track of

# the culmative height of the lines we've rendered so far

y_offset = 0

for line in lines:

fw, fh = font.size(line)

# (tx, ty) is the top-left of the font surface

tx = x - fw / 2

ty = y + y_offset

font_surface = font.render(line, True, colour)

screen.blit(font_surface, (tx, ty))

y_offset += fh

基本算法是将文本拆分为单词,然后逐字逐行逐字检查结果宽度,并在超出宽度时拆分为新行。在

由于您可以查询呈现文本的宽度,所以您可以确定在何处呈现文本以使其居中。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值