问题
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:
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
Here is the code that I have written:
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)
回答1:
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()
回答2:
I think that the issue is that you did not initialize the pygame module.
All pygame scripts must call the init function before using anything from the pygame library.
In your code, you have the line pygame.init() included. However, the line is in the wrong place and there is code that uses the pygame library before this line.
Luckily this problem is very easy to fix.
To fix this problem:
- Add the line
pygame.init()at the start of your script.
I hope this answer helped you and if you have any further queries please feel free to post a comment below!
来源:https://stackoverflow.com/questions/48286251/windows-python-pygame-error-video-system-not-initialized-after-adding-mp3-file