Pygame游戏菜单问题



,所以我正在使用Pygame进行游戏,我需要一个菜单,让玩家在不同的游戏类型之间进行选择。我试图做到这一点,以便在显示指令时,玩家可以使用键盘选择类型。但是,当我这样做时,游戏会出现错误。说明将保留在屏幕上(有时会绘制平面精灵)。我添加了一系列代码以在按下'e'时在外壳中打印某些内容,但没有任何打印。

def instructions(score):
    pygame.display.set_caption("Mail Pilot!")
    plane = Plane()
    ocean = Ocean()
    allSprites = pygame.sprite.Group(ocean, plane)
    insFont = pygame.font.SysFont(None, 50)
    insLabels = []
    instructions = (
    "Mail Pilot.     Last score: %d" % score ,
    "Instructions:  You are a mail pilot,",
    "delivering mail to the islands.",
    "",
    "Fly over an island to drop the mail,",
    "but be careful not to fly too close",    
    "to the clouds. Press 'e' for",
    "endless",
    "",
    "good luck!",
    "",
    "click to start, escape to quit..."
    )
    for line in instructions:
        tempLabel = insFont.render(line, 1, (255, 255, 0))
        insLabels.append(tempLabel)
    keepGoing = True
    clock = pygame.time.Clock()
    pygame.mouse.set_visible(False)
    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
                donePlaying = True
                pygame.display.quit()
                pygame.quit()
                sys.exit()
##            if event.type == pygame.MOUSEBUTTONDOWN:
##                keepGoing = False
##                donePlaying = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_e:
                    gameType = "endless"
                    keepGoing = False
                    donePlaying = False
                elif event.key == pygame.K_ESCAPE:
                    keepGoing = False
                    pygame.display.quit()
                    pygame.quit()
                    sys.exit()
                    donePlaying = True
        allSprites.update()
        allSprites.draw(screen)
        for i in range(len(insLabels)):
            screen.blit(insLabels[i], (50, 30*i))
        pygame.display.flip()
    plane.sndEngine.stop()    
    pygame.mouse.set_visible(True)
    return donePlaying
def main():
    gameType = ""
    donePlaying = False
    score = 0
    while not donePlaying:
        donePlaying = instructions(score)
        if not donePlaying:
            if gameType == "endless":
                score = gameEndless()

if __name__ == "__main__":
    main()

评论的部分是将开始游戏的原始代码。

感谢@furas帮助我意识到问题!

要修复程序,我从指令类中的if循环中添加了一个直接调用。

if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_e:
                    gameType = "endless"
                    keepGoing = False
                    donePlaying = False
                    score = gameEndless()

然后返回底部的分数

return donePlaying, score

主循环不再调用游戏,而是在完成完成时只打电话给指令

def main():
    donePlaying = False
    score = 0
    while not donePlaying:
        donePlaying = instructions(score)

这样,我可以规避主循环问题,并添加更简化的方法,而不是在主,结构和游戏类型之间弹跳。

这是我鄙视" Omni-applicable"事件驱动编程的另一个原因。虽然您似乎已经找到了一个解决方案,但我相信找到鼠标和鼠标的最佳方法就是调用

mousex,mousey = pygame.mouse.get_pos()

活动循环外。尽管MouseButtonUp适合其使用,但鼠标坐标的发现根本不是基于此上下文的事件。

最新更新