17

So I decided to start making a game and I was testing it a bit and then I got this error:

Traceback (most recent call last):
  File "TheAviGame.py", line 15, in <module>
    font = pygame.font.Font(None,25)
pygame.error: font not initialized

I have no idea what I have done wrong so far... Code:

#!/usr/bin/python
import pygame
blue = (25,25,112)
black = (0,0,0)
red = (255,0,0)
white = (255,255,255)
groundcolor = (139,69,19)
gameDisplay = pygame.display.set_mode((1336,768))
pygame.display.set_caption("TheAviGame")
direction = 'none'
clock = pygame.time.Clock()
img = pygame.image.load('player.bmp')
imgx = 1000
imgy = 100
font = pygame.font.Font(None,25)
def mts(text, textcolor, x, y):
    text = font.render(text, True, textcolor)
    gamedisplay.blit(text, [x,y])
def gameloop():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.pygame == pygame.KEYDOWN:
                if event.key == pygame.RIGHT:
                    imgx += 10
        gameDisplay.fill(blue)
        pygame.display.update()
        clock.tick(15)
gameloop()

3 Answers 3

38

You never initialized pygame and pygame.font after importing:

# imports

pygame.init() # now use display and fonts
3
  • 2
    If you call pygame.init you don't need to also call pygame.font.init (though it's innocuous to do so, it's not required) since pygame.init does that on your behalf. You call pygame.font.init to do any kind of font work before initializing the whole of pygame, if you so desire. As pygame.org/docs/ref/pygame.html#pygame.init explains, "You may want to initalise the different modules seperately to speed up your program or to not use things your game does not.". Commented Feb 14, 2015 at 17:17
  • Yes, I know. I was simply pointing that he has not initialized pygame for the use of the display. The font is a secondary issue. Commented Feb 14, 2015 at 17:18
  • I tried blitting in img and what I did was under the while true: I did gameDisplay.blit(img, (100,100)) and then pygame.display.update() under it but it it now it like flashes a ton... @MalikBrahimi Commented Feb 14, 2015 at 17:24
4

In order to use some pygame modules, either pygame or that specific module has to be initialised before you start using them - this is what the error message is telling you.

To initialize pygame:

import pygame
...
pygame.init()

To initialize a specific library (eg font):

import pygame
...
pygame.font.init()

In most cases, you will want to initialise all of pygame, so use the first version. In general, I would place this at the top of my code, just after importing pygame

1

You put None in the font:

font = pygame.font.Font(None, 25)

You can’t do that. You have to put a type of font there.

And you should call pygame.init().

1
  • 1
    As far as I know, None works. It just loads the default font of Pygame.
    – Veky
    Commented Jul 18, 2021 at 19:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.