Pygame在一个tim之后无法读取资源

2024-05-17 04:33:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个叫做忍者任务的Pygame游戏。到目前为止,我只做了几天,但是我发现当我启动游戏的时候,一切都很好,但是大约30秒后,游戏会崩溃,我说:

Traceback (most recent call last): File "NinjaQuest.py", line 151, in File "NinjaQuest.py", line 146, in main pygame.error: Couldn't read from 'Resources/Menu/Disasterpeace - Home.ogg'

虽然我现在只做了菜单,而且没有一个选项起作用,但是如果有人能告诉我我的代码有什么问题,我会很感激的,因为我已经问了我的一些piers,但是他们不能找出问题所在。提前感谢:)

顺便说一下,这是我的'忍者之旅':

#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”有帮助的话,我会使用它


Tags: gametruegetifrendersurfacepygamemenu
1条回答
网友
1楼 · 发布于 2024-05-17 04:33:59

我想我终于解决了我的问题!如果我不使用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)

我想说一声非常感谢你的菜鸟试图帮助我,听我的问题

相关问题 更多 >