'ошибка Pygame': шрифт не инициализирован .. на уже использованном шрифте? - PullRequest
0 голосов
/ 13 февраля 2019

Это странная проблема, потому что я уже использовал шрифт во время того же выполнения и ... в той же функции!

Я объясняю себя частью моей программы:

def text(show_text, show_size, show_color, show_x, show_y):
    fontObj = pygame.font.Font('Font.ttf',show_size)                # The line 43
    Load_text = fontObj.render(show_text,True,show_color,None)
    render_text  = Load_text.get_rect()
    render_text.center = (show_x,show_y)
    screen.blit(Load_text,render_text)

def checkmouse(t_text,size,px,py,color1,color2,window):
    global p_x, p_y
    px_min = px - (int(len(str(text))/2))
    px_max = px + (int(len(str(text))/2))
    py_min = py - 10
    py_max = py + 10
    if p_x >=  px_min and p_x <= px_max and p_y >= py_min and p_y <= py_max :
       text(t_text,size,color1,px,py)  # The line 68
       pygame.display.update()
    else:
       text(t_text,size,color2,px,py)
       pygame.display.update()

Проблема здесь:


    if w == 'main':
        sceen=pygame.display.set_mode((1920,1080), pygame.NOFRAME)
        while w == 'main':
            image(bg32_big,0,0)
            Main = True
            while Main :
               checkmouse('Play',32,960,260,Dark_Blue,White,'load') # line 109
               checkmouse('Quit',32,1520,740,Dark_Blue,White,'load')

И это мой «след»:

Traceback (most recent call last):
  File "C:\Users\Cédric\3D Objects\_TAL WIP_\TAL 1 script\newfile.py", line 143, in <module>
    Window('main')
  File "C:\Users\Cédric\3D Objects\_TAL WIP_\TAL 1 script\newfile.py", line 109, in Window
    checkmouse('Play',32,960,260,Dark_Blue,White,'load')
  File "C:\Users\Cédric\3D Objects\_TAL WIP_\TAL 1 script\newfile.py", line 68, in checkmouse
    text(t_text,size,color2,px,py)
  File "C:\Users\Cédric\3D Objects\_TAL WIP_\TAL 1 script\newfile.py", line 43, in text
    fontObj = pygame.font.Font('Font.ttf',show_size)
pygame.error: font not initialized

Но как это возможно?В той же функции я раньше использовал text('Loading, this may take a few time...',15,Dark_Blue,240,240), и она отлично работала, так почему?

PS: полный код:

import time
spb = time.time()
print('+*------------------------------------------------------------------------------------*+')
print(' ▀▀█▀▀ █░░█ █▀▀   █▀▀█ █░░█ █▀▀▄ █░░█ █▀▀   █░░░ █▀▀ █▀▀▀ █▀▀ █▀▀▄ █▀▀▄ ')
print(' ░▒█░░ █▀▀█ █▀▀   █▄▄█ █░░█ █▀▀▄ █▄▄█ ▀▀█   █░░░ █▀▀ █░▀█ █▀▀ █░░█ █░░█ ')
print(' ░▒█░░ ▀░░▀ ▀▀▀   █░▒█ ░▀▀▀ ▀▀▀░ ▄▄▄█ ▀▀▀   █▄▄█ ▀▀▀ ▀▀▀▀ ▀▀▀ ▀░░▀ ▀▀▀░ ')
print('+*------------------------------------------------------------------------------------*+')

#----+ Major Imports +----#
print('Loading pygame and other child modules :')
import pygame
import os, sys
pygame.init()
print('All modules was loaded succesfully !')
#---+ center windows +---#
os.environ['SDL_VIDEO_CENTERED'] = '1'

#-----+ ALL THE CONSTANTS FROM THE GAME +-----#
print('+*---------------*+')
print('Getting variables')


Dark_Blue = ( 0, 0, 128)
White = ( 255, 255, 255)

barPos = (40, 200)
barSize = (400, 25)

current_load = 'rien.png'

max_a = 3 # Numbur of pictures to convert
a=0
bar_percent = 0
display_percent = '0'
a_db,a_da,a_dif = 0,0,0
p_x,p_y =  0,0
show_size = 25
fontObj = pygame.font.Font('Font.ttf',show_size)
#------+ Loading window +------#

screen=pygame.display.set_mode((1,1), pygame.NOFRAME)

#-----+ ALL THE DEFINITIONS OF THE PROGRAM +-----#
print('Getting definitions')
def text(show_text, show_size, show_color, show_x, show_y):
    fontObj = pygame.font.Font('Font.ttf',show_size)
    Load_text = fontObj.render(show_text,True,show_color,None)
    render_text  = Load_text.get_rect()
    render_text.center = (show_x,show_y)
    screen.blit(Load_text,render_text)
def image(name,x,y):
    screen.blit(name,(x,y))
def DrawBar(pos, size, borderC, barC, progress):
    global screen
    pygame.draw.rect(screen, borderC, (*pos, *size), 1)
    innerPos = (pos[0]+3, pos[1]+3)
    innerSize = ((size[0]-6) * progress, size[1] - 6)
    pygame.draw.rect(screen, barC, (*innerPos, *innerSize))
    # The Program draw a bar from the value that we give in 'progress'
    # which is equal to a/max_a !
def checkmouse(t_text,size,px,py,color1,color2,window):
    global p_x, p_y
    px_min = px - (int(len(str(text))/2))
    px_max = px + (int(len(str(text))/2))
    py_min = py - 10
    py_max = py + 10
    if p_x >=  px_min and p_x <= px_max and p_y >= py_min and p_y <= py_max :
       text(t_text,size,color1,px,py)
       pygame.display.update()
    else:
       text(t_text,size,color2,px,py)
       pygame.display.update()

#---+ Needed for loading +---#
bg32 = pygame.image.load('sprites/bg32.png').convert_alpha()
bgbar = pygame.image.load('sprites/bgbar.png').convert_alpha()
outline = pygame.image.load('sprites/outline.png').convert_alpha()
title = pygame.image.load('sprites/loading_title.png').convert_alpha()

#---+ Program Heart +---#
def Window(w):
    global screen,a,max_a,barPos,barSize,Very_Dark_Blue,borderColor,bg32,bgbar
    global outline,title,current_load,a_dif,a_db,a_da,bar_percent
    if w == 'first_load':
        screen=pygame.display.set_mode((480,270), pygame.NOFRAME)
        w = 'load'

    if w == 'load':
        print(current_load)
        a_db = int(( a / max_a ) * 100)
        a = a + 1
        a_da = int(( a/max_a ) * 100)
        a_dif = a_da - a_db
        for i in range(0,a_dif):
            image(bg32,0,0)
            image(bgbar,40,200)
            image(outline,0,0)
            image(title,40,0)
            bar_percent = bar_percent + 1
            DrawBar(barPos, barSize, Dark_Blue, Dark_Blue, bar_percent/100)
            display_percent = str(current_load) + '   -   ' + str(bar_percent) + '  %'
            text(display_percent,25,Dark_Blue,240,185)
            text('Loading, this may take a few time...',15,Dark_Blue,240,240)
            pygame.display.update()

    if w == 'main':
        sceen=pygame.display.set_mode((1920,1080), pygame.NOFRAME)
        while w == 'main':
            image(bg32_big,0,0)
            Main = True
            while Main :
               checkmouse('Play',32,960,260,Dark_Blue,White,'load')
               checkmouse('Quit',32,1520,740,Dark_Blue,White,'load')
               for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                         Window(window) 
               pygame.quit()



            print('+*---------------*+')
print('Loading window :')

#-----+ Loading window +-----#
current_load = 'Background.png'
Window('first_load')
background = pygame.image.load('sprites/Background.png').convert_alpha()
current_load = 'outline.png'
Window('load')
outline_big = pygame.image.load('sprites/outline.png').convert_alpha()
current_load = 'bg32.png'
Window('load')
bg32_big = pygame.image.load('sprites/bg32_big.png').convert_alpha()

print('All pictures was loaded succesfully !')
#---+ time debug +---#
spe = time.time()
spt = spe - spb
sptm = 0
while spt >= 60:
    spt = spt - 60
    sptm = sptm + 1
print('+*---------------------*+')
print('All ressources needed for the game was loaded in',sptm,'minutes and',spt,'seconds')
#-----+ and... +-----#
Window('main')

1 Ответ

0 голосов
/ 14 февраля 2019

Только не вызывайте pygame.quit() в вашем основном цикле:

       while Main :
           checkmouse('Play',32,960,260,Dark_Blue,White,'load')
           checkmouse('Quit',32,1520,740,Dark_Blue,White,'load')
           for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                     Window(window) 
           pygame.quit() # <-- don't do this

Он переведет модуль шрифта (и другие модули) в состояние, которое больше не используется.

...