我刚刚在我的 pygame 游戏中添加了一些音乐,但我认为代码太混乱了,没有任何东西在正确的位置。由于添加了此内容,我现在收到此错误:
回溯(最近一次调用最后一次):文件
“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)
我已经找到问题了。你正在检查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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)