14
votes

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

3 Answers

24
votes

You never initialized pygame and pygame.font after importing:

# imports

pygame.init() # now use display and fonts
1
votes

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
votes

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