Search code examples
pythonfontsraspberry-pipygameioerror

Pygame IOError, unable to read Font File


I recently downloaded a font for a game im making in Pygame, but when the prompt where the font is used is meant to pop up, an error is displayed instead. The error reads:

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'

My code reads:

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)

I am not sure whats going on, as i have downloaded the same font from another website, which means it hasnt been corrupted.

Im on a Raspberry Pi 2 running Raspbian Wheezy. Here is all my code (if that's necessary):

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()

I used a tutorial by Sentdex on Youtube to teach me a few things about pygame, but i've hit this road block which caused me to halt progress, as i am trying to make this game an extension of his tutorial, rather than just a copy.


Solution

  • Works fine on windows. I downloaded some random arcade font file from http://www.dafont.com/arcade-pizzadude.font. It is possible that your fonts file is corrupt. Also to be noted, folders starting with "." in linux are hidden by default, but not in windows. So can you put the fonts file in some other folder and try running this code?