微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法在 Pygame 中使用更多字体?

如何解决有没有办法在 Pygame 中使用更多字体?

每当我尝试在 Pygame 中使用某些字体时,总是出现此错误

我正在制作一个番茄钟应用程序 有关番茄钟的更多信息 here

我的代码

import sys
import pygame


class TIMER:
    def __init__(self):
        self.x = screenWidth / 2
        self.y = screenHeight / 2
        self.font_size = 40
        self.text_Color = (255,255,255)

    def draw_timer(self,text):
        # I Can't get any other font except freesansbold.ttf
        t = pygame.font.Font("didot.ttc",self.font_size) 
        img1 = t.render(str(text),True,self.text_Color)
        screen.blit(img1,(self.x,self.y))


pygame.mixer.pre_init(frequency=44100,size=16,channels=1,buffer=512)
pygame.init()

screenHeight = 600
screenWidth = 600
FPS = 60

screen = pygame.display.set_mode((screenWidth,screenHeight))

circle = pygame.image.load('circle.png')

MIN = pygame.USEREVENT + 0
pygame.time.set_timer(MIN,60000)

SEC = pygame.USEREVENT + 1
pygame.time.set_timer(SEC,1000)

timer = TIMER()
time_sec = 60
time_min = 25

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == SEC:
            print("sec")
            time_sec -= 1
            ''''''
        if event.type == MIN:
            print("min")
            time_min -= 1
            time_sec = 60


    pygame.display.update()
    screen.fill((0,0))


    timer.draw_timer(f"{time_min}:{time_sec}")
    screen.blit(circle,(0,0))

    clock.tick(FPS)

它给了我这个错误

回溯(最近一次调用最后一次): 文件“C:/Users/Admin/PycharmProjects/PythonGui/Pomodoro project/pomodoro.py”,第 65 行,在 timer.draw_timer(f"{time_min}:{time_sec}") 文件“C:/Users/Admin/PycharmProjects/PythonGui/Pomodoro project/pomodoro.py”,第 17 行,在 draw_timer 中 t = pygame.font.Font("didot.ttc",self.font_size) FileNotFoundError: [Errno 2] 没有这样的文件或目录:'didot.ttc'

进程以退出代码 1 结束


谁能帮帮我

如果您需要任何其他信息,请询问。

解决方法

使用 pygame.font.SysFont() 而不是 pygame.font.Font() 从系统字体创建一个 Font 对象:

返回一个从系统字体加载的新 Font 对象。字体将匹配请求的粗体和斜体标志。 Pygame 使用一小组常见的字体别名。如果您要求的特定字体不可用,则可以使用合理的替代字体。如果找不到合适的系统字体,这将退回加载默认的 pygame 字体。

例如:

t = pygame.font.SysFont("Arial",self.font_size) 
t = pygame.font.SysFont("Times",self.font_size) 
t = pygame.font.SysFont("Helvetica",self.font_size) 
t = pygame.font.SysFont("Consolas",self.font_size) 
t = pygame.font.SysFont("Script",self.font_size) 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。