Im trying to use a different font in pygame but every time I run it I get this syntax error message
[Traceback (most recent call last):
File "C:\Python31\pytest.py", line 22, in <module>
font1 = pygame.font.Font('C:\ActionIsShaded.ttf', 12)
IOError: unable to read font filename]


I tried getting rid of the qoutes around the file name but syntax just gave me an error and didn't even run the program

Please help and thanks in advance to anyone that gives me any suggestions.

Recommended Answers

All 7 Replies

Try this.

font1 = pygame.font.SysFont('ActionIsShaded', 12)

Try this.

font1 = pygame.font.SysFont('ActionIsShaded', 12)

IT works but it uses the default font and im trying to use a different font

Perhaps try lower case? I don't have Python on this computer so I can't test it right now

maybe I have to load it first but I dont know how to do that at all

Here is document in internet:
http://www.pygame.org/docs/ref/font.html#pygame.font.Font

SysFont looks like for system fonts, not for file:
http://www.pygame.org/docs/ref/font.html#pygame.font.SysFont

Also SysFont returns font, does not set it, if I understand correctly.
In this game it is used with render method of the font returned:
http://www.gamedev.net/community/forums/topic.asp?topic_id=444490

def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.SysFont("None", 30)
        self.text = ""
        self.center = (320, 240)
                
   def update(self):
        self.image = self.font.render(self.text, 1, (0, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.center = self.center

what does it mean if I get this back
[Traceback (most recent call last):
File "C:\Python31\pytest.py", line 26, in <module>
text2 = font1.render ('Work',False,WHITE,BLUE)
AttributeError: 'NoneType' object has no attribute 'render']

Here is a good PyGame font example ...

""" customFont.py
use of a customized font with PyGame:
http://www.cs.iupui.edu/~aharris/pygame/ch05/customFont.py
download custom font from:
http://www.cs.iupui.edu/~aharris/pygame/ch05/GringoNights.ttf
"""

import pygame
pygame.init()

def main():
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("display some text")
    
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    # rgb tuple (0, 0, 0) is black
    background.fill((0, 0, 0))
    
    # makes sure the font file is in the working directory
    # or give the full path name
    myFont = pygame.font.Font("GringoNights.ttf", 40)
    # rgb tuple (255, 0, 0) is red
    # rgb tuple (255, 255, 0) is yellow
    label = myFont.render("Python in the Wild West", 1, (255, 0, 0))
    
    # use clock if you want to animate something
    #clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        #clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
        
        screen.blit(background, (0, 0))
        screen.blit(label, (100, 100))
        
        pygame.display.flip()

if __name__ == "__main__":
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.