Prathamesh Bhatkar
Prathamesh Bhatkar

Reputation: 301

Is there a way to use more fonts in Pygame?

I always get this error whenever I try to use some fonts in Pygame

I am making a Pomodoro app more info about Pomodoro here

My Code

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, 0))


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

    clock.tick(FPS)

It gives me this error

Traceback (most recent call last): File "C:/Users/Admin/PycharmProjects/PythonGui/Pomodoro project/pomodoro.py", line 65, in timer.draw_timer(f"{time_min}:{time_sec}") File "C:/Users/Admin/PycharmProjects/PythonGui/Pomodoro project/pomodoro.py", line 17, in draw_timer t = pygame.font.Font("didot.ttc", self.font_size) FileNotFoundError: [Errno 2] No such file or directory: 'didot.ttc'

Process finished with exit code 1


Can anyone will help me

If you want any other Info please ask.

Upvotes: 1

Views: 547

Answers (1)

Rabbid76
Rabbid76

Reputation: 211186

Use pygame.font.SysFont() instead of pygame.font.Font() to create a Font object from the system fonts:

Return a new Font object that is loaded from the system fonts. The font will match the requested bold and italic flags. Pygame uses a small set of common font aliases. If the specific font you ask for is not available, a reasonable alternative may be used. If a suitable system font is not found this will fall back on loading the default pygame font.

For instance:

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) 

Upvotes: 1

Related Questions