代码之家  ›  专栏  ›  技术社区  ›  Barwani

Pygame IOError,无法读取字体文件

  •  2
  • Barwani  · 技术社区  · 7 年前

    错误显示:

    Traceback (most recent call last):
    File "/home/pi/Desktop/Python Game.py", line 83, in <module> game_loop()
    File "/home/pi/Desktop/Python Game.py", line 78, in game_loop crash()
    File "/home/pi/Desktop/Python Game.py", line 29, in message_display largeText = pygame.font.Font 
    ("/home/pi/.fonts/ARCADECLASSIC.TTF",110)
    IOError: unable to read font file '/home/pi/.fonts/ARCADECLASSIC.TTF'
    

    我的代码是:

    def car (x, y)
    gameDisplay.blit (carImg, (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 ("/Home/pi/.fonts/ARCADECLASSIC.TTF, 110)
    Textsurf, TextRect-text_objects (text, largeText)
    TextRect.center = ((display-width/2), (display-height/2))
    gameDisplay.blit (TextSurf, TextRect)
    

    我不知道发生了什么,因为我从另一个网站下载了相同的字体,这意味着它没有被破坏。

    我在一个树莓皮2运行树莓喘息。 以下是我的所有代码(如果有必要):

    import pygame
    import time
    
    pygame.init()
    
    display_width=800
    display_height=600
    
    gameDisplay=pygame.display.set_mode((display_width,display_height))
    pygame.display.set_caption("Road Obstacles!")
    
    black=(0,0,0)
    white=(255,255,255)
    red=(255,0,0)
    
    car_width=50
    
    clock=pygame.time.Clock()
    carImg = pygame.image.load("Racecar.png")
    
    def car(x,y):
        gameDisplay.blit(carImg,(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("ARCADECLASSIC.TTF",110)
        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("Woops! You Crashed!")
    
    
    
    def game_loop():
    
        x = (display_width * 0.45)
        y = (display_height * 0.8)
    
        x_change = 0
    
        gameExit = False
    
        while not gameExit:
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameExit = True
    
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        x_change += -5
                    elif event.key == pygame.K_RIGHT:
                        x_change += 5
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT:
                        x_change += 5
                    if event.key == pygame.K_RIGHT:
                        x_change += -5
    
    
            x += x_change
    
            gameDisplay.fill(white)
            car(x,y)
    
            if x > display_width - car_width or x < 0:
                crash()
    
            pygame.display.update()
            clock.tick(60)
    
    game_loop()
    pygame.quit()
    quit()
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   ashish mulchandani    7 年前

    在windows上运行良好。我从网上下载了一些随机的arcade字体文件 http://www.dafont.com/arcade-pizzadude.font