我是一个名为Ninja Quest的Pygame游戏。到目前为止,我只对它工作了几天,但是我发现当我启动游戏时,一切正常,但是在大约30秒后,游戏会崩溃,说:
回溯(最近一次呼叫最后):文件" NinjaQuest.py",第151行, 在文件" NinjaQuest.py",第146行,在主pygame.error中: 无法阅读资源/菜单/灾难 - Home.ogg'
虽然我现在只完成了菜单,但没有一个选项可以使用,如果有人能告诉我我的代码有什么问题,我会很感激,因为我已经问了一些我的码头,但是他们不能&#39 ;弄清楚问题所在。在此先感谢:)。
顺便说一下,这是我的NinjaQuest.py':
#Imports
from pygame.locals import *; import pygame; from Image import *; from Background import *;
import sys, os
#Constants
FPS = 200
WINDOWWIDTH = 900; WINDOWHEIGHT = 506;
GAMETITLE = "Ninja Quest"; VERSION = "0.5 Pre-Dev"
WHITE = [255,255,255]; RED = [255,0,0]; GREEN = [0,255,0]; BLUE = [0,0,255]; BLACK = [0,0,0]
surface = pygame.display.set_mode([WINDOWWIDTH, WINDOWHEIGHT])
pygame.display.set_caption(GAMETITLE)
clock = pygame.time.Clock()
slc = 1
openingFinished = True
''' I define stuff here to avoid doing it in a loop to avoid lag '''
#Draw menu
pygame.font.init()
#Define background
background = Background("Resources/Menu/Background.png", 0, (601 - 506) * -1)
#Define text
titleFont = pygame.font.Font("Resources/ka1.ttf", 40)
titleText = titleFont.render("Ninja Quest",True,(255,165,0))
titleFont2 = pygame.font.Font("Resources/ka1.ttf", 30)
titleText2 = titleFont2.render(VERSION,True,(255,165,0))
newFont = pygame.font.Font("Resources/ka1.ttf", 30)
newText = newFont.render("New Game",True,(255,165,0))
loadFont = pygame.font.Font("Resources/ka1.ttf", 30)
loadText = loadFont.render("Load Game",True,(255,165,0))
creditFont = pygame.font.Font("Resources/ka1.ttf", 30)
creditText = creditFont.render("Credits",True,(255,165,0))
def drawMenu():
global titleText
global titleText2
global newText
global loadText
global creditText
global background
surface.blit(background.image, background.rect)
if slc == 1:
newText = newFont.render("New Game",True,BLUE)
loadText = newFont.render("Load Game",True,(255,165,0))
creditText = newFont.render("Credits",True,(255,165,0))
elif slc == 2:
loadText = newFont.render("Load Game",True,BLUE)
newText = newFont.render("New Game",True,(255,165,0))
creditText = newFont.render("Credits",True,(255,165,0))
elif slc == 3:
creditText = newFont.render("Credits",True,BLUE)
loadText = newFont.render("Load Game",True,(255,165,0))
newText = newFont.render("New Game",True,(255,165,0))
surface.blit(titleText,(WINDOWWIDTH / 2 - titleText.get_width() / 2, titleText.get_height()))
surface.blit(titleText2,(WINDOWWIDTH / 2 - titleText2.get_width() / 2, titleText.get_height() + titleText.get_height()))
surface.blit(newText,(WINDOWWIDTH / 2 - newText.get_width() / 2, WINDOWHEIGHT * 0.33333))
surface.blit(loadText,(WINDOWWIDTH / 2 - newText.get_width() / 2, (WINDOWHEIGHT * 0.33333) + loadText.get_height()))
surface.blit(creditText,(WINDOWWIDTH / 2 - newText.get_width() / 2, (WINDOWHEIGHT * 0.33333) + creditText.get_height() * 2))
pygame.display.update()
#Draw opening scene
def drawOpening():
openingFinished = False
surface.fill((255,255,255))
#Play theme tune
pygame.mixer.init()
pygame.mixer.music.load("Resources/Menu/8-Bit.ogg")
pygame.mixer.music.play()
pygame.display.update()
pygame.time.wait(1000)
#Draw background
background = Image("Resources/Menu/The 8-Bit Developers.png", 4, 4, 0, 0)
background = Image("Resources/Menu/The 8-Bit Developers.png", 4, 4, WINDOWWIDTH / 2 - background.image.get_width() / 2, WINDOWHEIGHT / 2 - background.image.get_height() / 2)
surface.blit(background.image,background.rect)
pygame.display.update()
pygame.time.wait(2000)
openingFinished = True
#Main Loop
def main(newGame):
running = True
global slc
pygame.key.set_repeat(1, 10)
#a = pygame.image.load("Resources/Menu/Icon.png")
#pygame.display.set_icon(a)
pygame.init()
if newGame == True:
#Start game and then music
pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
surface.fill((255,255,255))
drawOpening()
#pygame.mixer.music.load("Resources/Menu/Disasterpeace - Home.ogg")
pygame.mixer.music.load(os.path.join("Resources","Menu","Disasterpeace - Home.ogg"))
pygame.mixer.music.play()
while running:
#Controls
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and openingFinished == True:
if event.key == pygame.K_UP:
slc -= 1
if slc == 0:
slc = 3
if event.key == pygame.K_DOWN:
slc += 1
if slc == 4:
slc = 1
if event.key == pygame.K_RETURN:
if slc == 1:
#new game
pass
if slc == 2:
#resume game
pass
if slc == 3:
#credits
pass
drawMenu()
pygame.display.flip()
pygame.mixer.music.queue(os.path.join("Resources","Menu","Disasterpeace - Home.ogg"))
clock.tick(FPS)
if __name__ == '__main__':
main(True)
编辑:我使用的是Mac OS X 10.9' Maverics'如果它有帮助
答案 0 :(得分:1)
我想我终于解决了我的问题!似乎我可以避免游戏崩溃,而不是使用pygame.mixer.music.queue()我的主循环的每次迭代,而是,我只是使用:
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.load(os.path.join("Resources","Menu","Disasterpeace - Home.ogg"))
pygame.mixer.music.play()
这是最终代码:
#Imports
from pygame.locals import *; import pygame; from Image import *; from Background import *;
import sys, os
#Constants
FPS = 200
WINDOWWIDTH = 900; WINDOWHEIGHT = 506;
GAMETITLE = "Ninja Quest"; VERSION = "0.5 Pre-Dev"
WHITE = [255,255,255]; RED = [255,0,0]; GREEN = [0,255,0]; BLUE = [0,0,255]; BLACK = [0,0,0]
surface = pygame.display.set_mode([WINDOWWIDTH, WINDOWHEIGHT])
pygame.display.set_caption(GAMETITLE)
clock = pygame.time.Clock()
slc = 1
openingFinished = True
''' I define stuff here to avoid doing it in a loop to avoid lag '''
#Draw menu
pygame.font.init()
#Define background
background = Background("Resources/Menu/Background.png", 0, (601 - 506) * -1)
#Define text
titleFont = pygame.font.Font("Resources/ka1.ttf", 40)
titleText = titleFont.render("Ninja Quest",True,(255,165,0))
titleFont2 = pygame.font.Font("Resources/ka1.ttf", 30)
titleText2 = titleFont2.render(VERSION,True,(255,165,0))
newFont = pygame.font.Font("Resources/ka1.ttf", 30)
newText = newFont.render("New Game",True,(255,165,0))
loadFont = pygame.font.Font("Resources/ka1.ttf", 30)
loadText = loadFont.render("Load Game",True,(255,165,0))
creditFont = pygame.font.Font("Resources/ka1.ttf", 30)
creditText = creditFont.render("Credits",True,(255,165,0))
def drawMenu():
global titleText
global titleText2
global newText
global loadText
global creditText
global background
surface.blit(background.image, background.rect)
if slc == 1:
newText = newFont.render("New Game",True,BLUE)
loadText = newFont.render("Load Game",True,(255,165,0))
creditText = newFont.render("Credits",True,(255,165,0))
elif slc == 2:
loadText = newFont.render("Load Game",True,BLUE)
newText = newFont.render("New Game",True,(255,165,0))
creditText = newFont.render("Credits",True,(255,165,0))
elif slc == 3:
creditText = newFont.render("Credits",True,BLUE)
loadText = newFont.render("Load Game",True,(255,165,0))
newText = newFont.render("New Game",True,(255,165,0))
surface.blit(titleText,(WINDOWWIDTH / 2 - titleText.get_width() / 2, titleText.get_height()))
surface.blit(titleText2,(WINDOWWIDTH / 2 - titleText2.get_width() / 2, titleText.get_height() + titleText.get_height()))
surface.blit(newText,(WINDOWWIDTH / 2 - newText.get_width() / 2, WINDOWHEIGHT * 0.33333))
surface.blit(loadText,(WINDOWWIDTH / 2 - newText.get_width() / 2, (WINDOWHEIGHT * 0.33333) + loadText.get_height()))
surface.blit(creditText,(WINDOWWIDTH / 2 - newText.get_width() / 2, (WINDOWHEIGHT * 0.33333) + creditText.get_height() * 2))
pygame.display.update()
#Draw opening scene
def drawOpening():
openingFinished = False
surface.fill((255,255,255))
#Play theme tune
pygame.mixer.init()
pygame.mixer.music.load("Resources/Menu/8-Bit.ogg")
pygame.mixer.music.play()
pygame.display.update()
pygame.time.wait(1000)
#Draw background
background = Image("Resources/Menu/The 8-Bit Developers.png", 4, 4, 0, 0)
background = Image("Resources/Menu/The 8-Bit Developers.png", 4, 4, WINDOWWIDTH / 2 - background.image.get_width() / 2, WINDOWHEIGHT / 2 - background.image.get_height() / 2)
surface.blit(background.image,background.rect)
pygame.display.update()
pygame.time.wait(2000)
openingFinished = True
#Main Loop
def main(newGame):
running = True
global slc
pygame.key.set_repeat(1, 10)
#a = pygame.image.load("Resources/Menu/Icon.png")
#pygame.display.set_icon(a)
pygame.init()
if newGame == True:
#Start game and then music
pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
surface.fill((255,255,255))
drawOpening()
#pygame.mixer.music.load("Resources/Menu/Disasterpeace - Home.ogg")
pygame.mixer.music.load(os.path.join("Resources","Menu","Disasterpeace - Home.ogg"))
pygame.mixer.music.play()
while running:
#Controls
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and openingFinished == True:
if event.key == pygame.K_UP:
slc -= 1
if slc == 0:
slc = 3
if event.key == pygame.K_DOWN:
slc += 1
if slc == 4:
slc = 1
if event.key == pygame.K_RETURN:
if slc == 1:
#new game
pass
if slc == 2:
#resume game
pass
if slc == 3:
#credits
pass
drawMenu()
pygame.display.flip()
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.load(os.path.join("Resources","Menu","Disasterpeace - Home.ogg"))
pygame.mixer.music.play()
clock.tick(FPS)
if __name__ == '__main__':
main(True)
我想非常感谢PyNEwbie试图帮助我并倾听我的问题。