- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个奇怪的问题,因为我已经在同一个执行过程中使用了该字体,并且......在同一个函数中!
我用我的程序的一部分来解释自己:
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('正在加载,这可能需要一些时间...',15,Dark_Blue,240,240)
并且效果很好,那为什么呢?
PS:完整代码:
import time
spb = time.time()
#----+ 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')
最佳答案
只是不要在主循环中调用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
它会将字体模块(和其他模块)置于不再可用的状态。
关于python - “pygame 错误”: font not initialized.。在已使用的字体上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54677481/
我正在尝试将 keras.initializers 引入我的网络,following this link : import keras from keras.optimizers import RMS
我正在为程序创建某种前端。为了启动程序,我使用了调用 CreateProcess(),其中接收到一个指向 STARTUPINFO 结构的指针。初始化我曾经做过的结构: STARTUPINFO star
我已经模板化了 gray_code 类,该类旨在存储一些无符号整数,其基础位以格雷码顺序存储。这里是: template struct gray_code { static_assert(st
我已经查看了之前所有与此标题类似的问题,但我找不到解决方案。所有错误都表明我没有初始化 ArrayList。我是否没有像 = new ArrayList 这样初始化 ArrayList? ? impo
当涉及到 Swift 类时,我对必需的初始化器和委托(delegate)的初始化器有点混淆。 正如您在下面的示例代码中所见,NewDog 可以通过两种方式中的一种进行初始化。如您所见,您可以通过在初始
几天来我一直在为一段代码苦苦挣扎。我在运行代码时收到的错误消息是: 错误:数组初始值设定项必须是初始值设定项列表 accountStore(int size = 0):accts(大小){} 这里似乎
我想返回一个数组,因为它是否被覆盖并不重要,我的方法是这样的: double * kryds(double linje_1[], double linje_2[]){ double x = linje
尝试在 C++ 中创建一个简单的 vector 时,出现以下错误: Non-aggregates cannot be initialized with initializer list. 我使用的代码
如何在构造函数中(在堆栈上)存储初始化列表所需的临时状态? 例如,实现这个构造函数…… // configabstraction.h #include class ConfigAbstraction
我正在尝试编写一个 native Node 插件,它枚举 Windows 机器上的所有窗口并将它们的标题数组返回给 JS userland。 但是我被这个错误难住了: C:\Program Files
#include using namespace std; struct TDate { int day, month, year; void Readfromkb() {
我很难弄清楚这段代码为何有效。我不应该收到“数组初始值设定项必须是初始值设定项列表”错误吗? #include class B { public: B() { std::cout << "B C
std::map m = { {"Marc G.", 123}, {"Zulija N.", 456}, {"John D.", 369} }; 在 Xcode 中,我将 C+
为了帮助你明白这一点,我给出了我的代码:(main.cpp),只涉及一个文件。 #include #include using namespace std; class test{ public
这在 VS2018 中有效,但在 2008 中无效,我不确定如何修复它。 #include #include int main() { std::map myMap = {
我有一个类: #include class Object { std::shared_ptr object_ptr; public: Object() {} template
我正在为 POD、STL 和复合类型(如数组)开发小型(漂亮)打印机。在这样做的同时,我也在摆弄初始化列表并遇到以下声明 std::vector arr{ { 10, 11, 12 }, { 20,
我正在使用解析实现模型。 这是我的代码。 import Foundation import UIKit import Parse class User { var objectId : String
我正在观看 Java 内存模型视频演示,作者说与 Lazy Initialization 相比,使用 Static Lazy Initialization 更好,我不清楚他说的是什么想说。 我想接触社
如果您查看 Backbone.js 的源代码,您会看到此模式的多种用途: this.initialize.apply(this, arguments); 例如,这里: var Router =
我是一名优秀的程序员,十分优秀!