Reputation: 17
I keep getting the error "Font not initialized" after running my program. Here's the program. I know I'm initializing pygame. I've tried initializing pygame, pygame.font, and and both of them at the same time. I've done everything. I've tried using pygames default fonts as pygame.font.Font('freesansbold.ttf', 115). That didn't work either.
import pygame
import time
import random
pygame.init
pygame.font.init
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
car_width = 100
car_height = 100
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A Bit Racey')
clock = pygame.time.Clock()
carImg = pygame.image.load ('racecar.png')
gameOver = pygame.image.load ('gameover.png')
def gO (x,y):
gameDisplay.blit(gameOver, (x,y))
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def text_objects(text, font):
textSurface = font.render (text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
font = pygame.font.SysFont("C:\Windows\Fonts\Arial.ttf", 115)
TextSurf, TextRect = text_objects(text, font)
TextRect.center = ((display_width/2), (display_height/2))
gameDisplay.blit(TextSurf, TextRect)
time.sleep(2)
game_loop()
def crash ():
message_display('Game Over')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 7
thing_width = 100
thing_height = 100
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
#thingx, thingy, thinkw, thingh, color)
things(thing_startx, thing_starty, thing_width, thing_height, black)
thing_starty += thing_speed
car (x,y)
if x > display_width - car_width or x < 0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0, display_width)
if y < thing_starty+thing_height:
print('y crossover')
if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and + car_width < thing_startx+thing_width:
print('x crossover')
gO (display_width/2 - 145.5, display_height/2-200)
time.sleep(2)
game_loop()
if gameExit == True:
pygame.quit()
quit()
pygame.display.update()
clock.tick(45)
game_loop()
pygame.quit ()
quit ()
Upvotes: 0
Views: 1654
Reputation: 1374
Your question is strange. Really strange. It even does not have any problem in it. You are not calling the function pygame.init()
. You are evaluating it. You are checking for its existence. Python will raise an exception if the function does not exist BUT it will NOT do anything if it does. To call a function use <function_name>(<parameters>)
. This. Is. Not. Ruby.
Upvotes: 0
Reputation: 20224
You just didn't really run init
function in your snippet.
# didn't run function
pygame.init
pygame.font.init
# instead
pygame.init()
pygame.font.init()
Upvotes: 1