Windows/Python pygame.error:添加 Mp3 文件后视频系统未初始化



我刚刚在我的pygame游戏中添加了一些音乐,但我认为代码太混乱了,没有任何东西在正确的位置。由于此添加,我现在收到此错误:

回溯(最近一次调用(:文件 "C:\Users\1234\AppData\Local\Programs\Python\Python36-32\My First Game ERROR.py",第 31 行,在 对于 pygame.event.get(( 中的事件:pygame.error:视频系统未初始化

这是我编写的代码:

import pygame, sys
pygame.mixer.init(44100, -16,2,2048)
class Game(object):
def main(self, screen):
    import time
pygame.mixer.music.load('The Tonight Show Star Wars The Bee Gees Stayin     Alive Shortened.mp3')
pygame.mixer.music.play(-1, 0.0)
#class Player(pygame.sprite.Sprite):
   # def __init__(self, *groups):
   # super(Player, self.__init__(*groups)
    #self.image = pygame.image.load('Sprite-01.png')
   # self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
    #def update(self):
       # key = pygame
image = pygame.image.load('Sprite-01.png')
clock = pygame.time.Clock()
# initialize variables
image_x = 0
image_y = 0
while 1:
clock.tick(30)     
for event in pygame.event.get():
    if event.type == pygame.quit():
        pygame.quit()
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
        pygame.quit()
image_x += 0
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
    image_x -= 10
if key[pygame.K_RIGHT]:
    image_x += 10
if key[pygame.K_UP]:
    image_y -= 10
if key[pygame.K_DOWN]:
    image_y += 10
screen.fill((200, 200, 200))
screen.blit(image, (image_x, image_y))
pygame.display.flip()
pygame.mixer.music.stop(52)

if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('St.Patrick game')
Game().main(screen)

我发现了问题。您正在检查事件循环中的if event.type == pygame.quit():,而您应该检查if event.type == pygame.QUIT: 。这意味着第一次执行事件循环时,您在此行中调用pygame.quit()并取消初始化所有模块,这样您将不能再使用许多pygame函数并引发pygame.error

我认为您实际上希望您的程序看起来更像这个版本:

import pygame

pygame.mixer.init(44100, -16,2,2048)

class Player(pygame.sprite.Sprite):
    def __init__(self, *groups):
        super(Player, self.__init__(*groups))
        self.image = pygame.image.load('Sprite-01.png')
        self.rect = pygame.rect.Rect((320, 240), self.image.get_size())

class Game(object):
    def main(self, screen):
        # pygame.mixer.music.load('The Tonight Show Star Wars The Bee Gees Stayin     Alive Shortened.mp3')
        # pygame.mixer.music.play(-1, 0.0)
        # image = pygame.image.load('Sprite-01.png')
        image = pygame.Surface((30, 50))
        image.fill((0, 90, 240))
        clock = pygame.time.Clock()
        # initialize variables
        image_x = 0
        image_y = 0
        done = False
        while not done:
            clock.tick(30)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    done = True
            key = pygame.key.get_pressed()
            if key[pygame.K_LEFT]:
                image_x -= 10
            if key[pygame.K_RIGHT]:
                image_x += 10
            if key[pygame.K_UP]:
                image_y -= 10
            if key[pygame.K_DOWN]:
                image_y += 10
            screen.fill((200, 200, 200))
            screen.blit(image, (image_x, image_y))
            pygame.display.flip()
            # pygame.mixer.music.stop(52)

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption('St.Patrick game')
    Game().main(screen)
    pygame.quit()
我认为

问题是您没有初始化pygame模块。

所有 pygame 脚本都必须在使用 pygame 库中的任何内容之前调用 init 函数。

在您的代码中,您包含pygame.init()行。但是,该行位于错误的位置,并且在此行之前有使用 pygame 库的代码。

幸运的是,这个问题很容易解决。

要解决此问题,请执行以下操作:

  • 在脚本的开头添加行pygame.init()

我希望这个答案对您有所帮助,如果您有任何其他疑问,请随时在下面发表评论!

最新更新