首页 > 解决方案 > 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:库未初始化。并且在代码仍在运行时条件完成后,分数不会重置。

标签: pythonpygame

解决方案


所以我的问题是我使用了图像,我得到的位置是这样的:

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") 

推荐阅读