python - pygame.error:库未初始化,虽然已经初始化
问题描述
我的代码看起来像这样:
import pygame
pygame.init()
running = True
score = 0
score_text = score_font.render("Score: " + str(score), False, (0, 0, 0))
def construct_window():
main_window.blit(bg, (0,0))
main_window.blit(score_text, (260,0))
def reset_score():
score = 0
while running:
for event in pygame.event.get():
#making the game quitable
if event.type == pygame.QUIT:
running = False
if some_condition:
reset_score()
construct_window()
pygame.quit()
所以它就像一个魅力但是在我关闭窗口后我得到错误:pygame.error:库未初始化。并且在代码仍在运行时条件完成后,分数不会重置。
解决方案
所以我的问题是我使用了图像,我得到的位置是这样的:
import pygame
import pkg_resources
pygame.init()
img = pygame.image.load(pkg_resources.resource_filename("main", "my_image.png"))
我猜 pygame 不喜欢 pkg_resources。如果其他任何人遇到此问题,我动态获取文件路径的解决方案是:
import sys
import pygame
pygame.init()
current_dict = sys.path[0]
img = pygame.image.load(current_dict + "my_image.png")
推荐阅读
- elasticsearch - 一个索引中的 Elasticsearch 字段出现在另一个索引中
- c# - Newtonsoft.Json.JsonReaderException:'从 JsonReader 读取 JArray 时出错。当前 JsonReader 项不是数组
- amazon-web-services - ZendService\Amazon\S3 错误 500 试图实例化对象
- ruby - 如何使用官方 gem 从 Gmail 中获取邮件
- php - 如何在 Symfony 3.4 中从控制器调用命令?
- python - Numpy 函数在 IDLE 中不起作用
- javascript - 需要使用 $Subtype 和泛型来键入函数
- react-native - 如何在 React Native 的 FormInput 右侧放置一个按钮?
- javascript - 事件触发后 Vue.js 组件丢失状态
- abap - 压缩传输请求中的行