Pygame Python字体

Pygame Python字体,python,pygame,width,glyph,Python,Pygame,Width,Glyph,我有一个关于字形a字体和pygame字体宽度的问题,昨天我有一些关于这个的代码,下面是代码: import pygame pygame.init() win = pygame.display.set_mode((800, 600)) font = pygame.font.Font("freesansbold.ttf", 32) i = 0 text = "hello how are you?" run = True while run: for

我有一个关于字形a字体和pygame字体宽度的问题,昨天我有一些关于这个的代码,下面是代码:

import pygame

pygame.init()
win = pygame.display.set_mode((800, 600))
font = pygame.font.Font("freesansbold.ttf", 32)
i = 0
text = "hello how are you?"
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    letter = text[i]
    text_1 = font.render(letter, True, (255, 255, 255))

    bw, bh = font.size(letter)
    glyph_rect = pygame.mask.from_surface(text_1).get_bounding_rects()
    # print(glyph_rect)
    if glyph_rect:
        gh = glyph_rect[0].height
        print(f"{glyph_rect[0]}")
        print(f'letter {letter}  bitmap height: {bh}  glyph height: {gh} width = {glyph_rect[0].width} width_2 = {bw}')

    win.fill((0, 0, 0))
    win.blit(text_1, (0, 0))
    pygame.display.update()

    i += 1
    run = i < len(text)

pygame.quit()
# win.blit(text_1, (0, 0))
导入pygame
pygame.init()
win=pygame.display.set_模式((800600))
font=pygame.font.font(“freesansbold.ttf”,32)
i=0
text=“你好,你好吗?”
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
字母=文本[i]
text_1=font.render(字母,True,(255,255,255))
bw,bh=字体大小(字母)
glyph_rect=pygame.mask.from_surface(text_1).get_bounding_rects()
#打印(字形)
如果标志符号正确:
gh=字形[0]。高度
打印(f“{glyph_rect[0]}”)
打印(字母{letter}位图高度:{bh}字形高度:{gh}宽度={glyph_rect[0]。宽度}宽度{bw}
赢。填充((0,0,0))
win.blit(文本_1,(0,0))
pygame.display.update()
i+=1
运行=i

我知道为什么字形高度不同,因为我在一个问题中问了这个问题,我得到了答案,但为什么字形宽度总是比实际宽度小一点?有人能给我详细解释一下吗?

宽度不同,因为当字符连接到单词时,字符之间有一个空格。
字形[0]。宽度
是字形的宽度
bw
是位图的宽度。图示符不会填充整个位图,并且两侧都有一些空间


您可以使用以下程序对此进行调查(每个字母显示1秒):

导入pygame
pygame.init()
win=pygame.display.set_模式((800600))
clock=pygame.time.clock()
font=pygame.font.font(“freesansbold.ttf”,200)
i=0
text=“你好,你好吗?”
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
字母=文本[i]
text_1=font.render(字母,True,(0,0,0))
text_draw=font.render(字母,True,(0,0,0),(255,255,255))
bw,bh=字体大小(字母)
glyph_rect=pygame.mask.from_surface(text_1).get_bounding_rects()
如果标志符号正确:
gh=字形[0]。高度
打印(f“{glyph_rect[0]}”)
打印(字母{letter}位图高度:{bh}字形高度:{gh}宽度={glyph_rect[0]。宽度}宽度{bw}
win.fill((128128128))
win.blit(文本绘制,(100100))
对于字形中的r\u rect:
draw_rect=r.move(100100)
pygame.draw.rect(赢,(255,0,0),draw_rect,3)
pygame.display.update()
i+=1
运行=i
@Rohit12345是的,你说得对。非常感谢你向我解释biitmap字体、位图和字形以及许多其他东西。因此,字符“”没有连接像素,因为它是背景,所以我将得到一个空列表作为字形没有与它连接的像素,因为它是背景,所以我将得到一个空列表作为glyph_rect@Rohit12345是,对于“”列表为空。对于“?”列表有2个元素。