[Python] Pygame error using font: no such file or directory

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    i have a pygame program that simulates a race car, driving and dodging blocks, this program worked 100% until i tried it today, now it gives me an error:

    Warning (from warnings module):
    File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 50
    largeText = pygame.font.Font(None,75)
    RuntimeWarning: use font: libSDL_ttf-2.0.so.0: cannot open shared object file: No such file or directory
    (ImportError: libSDL_ttf-2.0.so.0: cannot open shared object file: No such file or directory)
    Traceback (most recent call last):
    File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 151, in <module>
    begin()
    File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 29, in begin
    message_display(over)
    File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 50, in message_display
    largeText = pygame.font.Font(None,75)
    File "/usr/local/lib/python3.4/dist-packages/pygame/__init__.py", line 74, in __getattr__
    raise NotImplementedError(MissingPygameModule)
    NotImplementedError: font module not available
    (ImportError: libSDL_ttf-2.0.so.0: cannot open shared object file: No such file or directory)


    this makes me think somethings wrong with the font i use, it is realy very frustrating, and i realy cant see anything wrong, here is my code:

    import pygame
    import time
    import random


    pygame.init()

    display_width = 1500
    display_height = 1000

    black = (255,255,255)
    white = (255,255,255)
    red = (255,0,0)
    back_ground = (25,25,25)
    over = "Start your engins!"



    car_width = 50

    gameDisplay = pygame.display.set_mode((display_width,display_height))
    pygame.display.set_caption("A bit Racey")
    clock = pygame.time.Clock()

    carImg = pygame.image.load("resieskar.png")
    resized = pygame.transform.scale(carImg, (50, 100))

    def begin():
    message_display(over)

    def you_win():
    message_display("You Win!")

    def things_dodged(count):
    font = pygame.font.SysFont(None,25)
    text = font.render("Dodged: "+str(count),True,white)
    gameDisplay.blit(text, (0,0))

    def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, block_color, [thingx, thingy, thingw, thingh])

    def car(x,y):
    gameDisplay.blit(resized,(x,y))

    def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

    def message_display(text):
    largeText = pygame.font.Font(None,75)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()

    time.sleep(2)
    game_loop()

    def crash():
    message_display('You Crashed!')



    def game_loop():
    global block_R
    global block_G
    global block_B
    global block_color

    black = (0,0,0)
    white = (255,255,255)
    red = (255,0,0)
    block_R = random.randrange(100,200)
    block_G = random.randrange(100,200)
    block_B = random.randrange(30,200)

    block_color = (block_R,block_G,block_B)

    x = (display_width*0.45)
    y = (display_height*0.8)
    x_change = 0

    thing_width = 100
    thing_startx = random.randrange(0, display_width-thing_width)
    thing_starty = -600
    thing_speed = 5
    thing_height = 100
    dodged = 0
    dificuilty = 2
    bigger = 15

    gameExit = False

    while not gameExit:

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    quit()


    if event.type == pygame.KEYDOWN:

    if event.key == pygame.K_RIGHT:
    x_change = 50

    elif event.key == pygame.K_LEFT:
    x_change = -50

    if event.type == pygame. KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
    x_change = 0


    x += x_change



    gameDisplay.fill(back_ground)

    # things(thingx, thingy, thingw, thingh, color)
    things(thing_startx, thing_starty, thing_width, thing_height, block_color)
    thing_starty += thing_speed
    car(x,y)
    things_dodged(dodged)
    if dodged > 34:
    you_win()

    if x > display_width - car_width or x < 0:
    crash()

    if thing_starty > display_height:
    thing_starty = 0 - thing_height
    thing_startx = random.randint(0,(795-int(thing_width)))
    dodged += 1
    bigger = bigger * 0.9
    dificuilty = dificuilty * 0.6
    thing_speed += dificuilty
    thing_width += bigger



    if y < thing_starty+thing_height:
    if x > thing_startx and x < thing_startx+thing_width or x+car_width > thing_startx and x+car_width<thing_startx+thing_width:
    crash()


    pygame.display.update()
    clock.tick(60)

    begin()
    game_loop()
    pygame.quit()
    quit()


    the only parts where i use the font is in message_display() and things_dodged()

    Continue reading...

Compartilhe esta Página