【问题标题】:Windows/Python pygame.error: video system not initialized after adding Mp3 fileWindows/Python pygame.error: 添加 Mp3 文件后视频系统未初始化
【发布时间】:2018-01-16 16:57:34
【问题描述】:

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

Traceback(最近一次调用最后一次):文件 “C:\Users\1234\AppData\Local\Programs\Python\Python36-32\我的第一个游戏 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)

【问题讨论】:

  • 请正确缩进您的代码,以便我们可以准确地看到您在编辑器中看到的内容。单击编辑,粘贴您的代码,全选并按 Ctrl+K 或 {} 按钮以正确格式化。
  • 对不起,我只是手动添加了四个空格。
  • 暂时将您的代码发布在pastebin.com 或gist.github.com 上,以便我们查看您的程序的实际外观(我们可以为您添加到帖子中)。

标签: python pygame


【解决方案1】:

我发现了问题。您在事件循环中检查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()

【讨论】:

  • 在这里使用Game 类实际上是没有意义的,因为它只包含一个方法。您可以将main 方法转换为普通函数。
【解决方案2】:

我认为问题在于您没有初始化 pygame 模块。

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

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

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

要解决此问题:

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

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

【讨论】:

  • 具体需要在哪里添加?
  • 我尝试添加它,但仍然遇到同样的错误。
  • 在你导入 pygame 之后。
  • # 这只是导入所有 Pygame 模块 import pygame pygame.init() class Game(object): def main(self, screen):
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多