954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pygame font help

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
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.

wolfeater017
Light Poster
35 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Try this.

font1 = pygame.font.SysFont('ActionIsShaded', 12)
redyugi
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 15
Solved Threads: 30
 

Try this.

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

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

wolfeater017
Light Poster
35 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

redyugi
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 15
Solved Threads: 30
 

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

wolfeater017
Light Poster
35 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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

wolfeater017
Light Poster
35 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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()
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: