Windows/Python pygame.error: 添加 Mp3 文件后视频系统未初始化 [英] Windows/Python pygame.error: video system not initialized after adding Mp3 file

查看:72
本文介绍了Windows/Python pygame.error: 添加 Mp3 文件后视频系统未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在我的 pygame 游戏中添加了一些音乐,但我认为代码太混乱了,没有什么地方是正确的.由于此添加,我现在收到此错误:

I just added some music to my pygame game but I think the code is so messy that nothing is in the right place. As a result of this addition, I'm now getting this error:

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

Traceback (most recent call last): File "C:\Users\1234\AppData\Local\Programs\Python\Python36-32\My First game ERROR.py", line 31, in for event in pygame.event.get(): pygame.error: video system not initialized

这是我写的代码:

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 被提升.

I've found the problem. You're checking if event.type == pygame.quit(): in the event loop whereas you should check if event.type == pygame.QUIT:. That means the first time the event loop is executed, you call pygame.quit() in this line and uninitialize all modules whereupon you cannot use many pygame functions anymore and the pygame.error is raised.

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

I think you actually want your program to look more like this version:

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

这篇关于Windows/Python pygame.error: 添加 Mp3 文件后视频系统未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆