Pygame游戏菜单问题

时间:2016-12-11 19:58:18

标签: python menu pygame

所以我在Pygame制作游戏,我需要一个菜单​​让玩家在不同的游戏类型之间进行选择。我试图这样做,以便在显示说明时,播放器可以使用键盘选择类型;但是,当我这样做时,游戏就会出错。指令将保留在屏幕上(有时会绘制平面精灵)。我添加了一行代码,在按下'e'时在shell中打印一些东西,但没有打印出来。

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()

注释掉的部分是开始游戏的原始代码。

2 个答案:

答案 0 :(得分:0)

感谢@furas帮助我实现这个问题!

为了修复程序,我在指令类的if循环中添加了对游戏的直接调用。

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

然后返回底部的分数

return donePlaying, score

主循环不再调用游戏,而是在donePlaying为True时调用指令

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

通过这种方式,我可以规避主循环问题,并添加更简化的方法,而不是在主要,入门和游戏类型之间反弹。

答案 1 :(得分:0)

这是我鄙视“全方位适用”事件驱动编程的另一个原因。虽然看起来你找到了解决方案,但我相信找到mousex和mousey的最佳方法就是致电

mousex,mousey = pygame.mouse.get_pos()

在事件循环之外。虽然MOUSEBUTTONUP当然适合它的使用,但鼠标坐标的发现根本不是需要基于事件的事物。