Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame font error

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()
like image 877
Silver Fire Avatar asked Feb 14 '15 17:02

Silver Fire


People also ask

What fonts work with pygame?

Lastly, to load a font file that isn't a TrueType font, you can use the pygame. freetype module which supports TTF, Type1, CFF, OpenType, SFNT, PCF, FNT, BDF, PFR and Type42 fonts. You can user either font files by calling pygame.

How do I fix pygame video system is not initialized?

Pygame. error video system not initialized can be fixed by stopping the main loop of implementation with the exit() function. The pygame. init() error is caused by its function not being called and can be solved with the import and initialization of pygame modules.

How do I change font size in pygame?

Once the font is created, its size cannot be changed. A Font object is used to create a Surface object from a string. Pygame does not provide a direct way to write text onto a Surface object. The method render() must be used to create a Surface object from the text, which then can be blit to the screen.


1 Answers

You never initialized pygame and pygame.font after importing:

# imports

pygame.init() # now use display and fonts
like image 169
Malik Brahimi Avatar answered Sep 27 '22 01:09

Malik Brahimi