Pygame和Livewires按钮按下不起作用



所以在游戏中的菜单中,我正在创建一张图片,显示如何玩游戏,并且我有一些代码,以便当玩家按下 b 按钮时它会返回到主菜单,但这不起作用,有人可以帮我吗?

def manual():
    image = games.load_image("options.jpg")
    games.screen.background = image
    if games.keyboard.is_pressed(games.K_b):
        menu()
    games.screen.mainloop()

'menu()' 是另一个包含所有主菜单代码的函数

这是菜单功能

def menu():
    pygame.init()
    menubg = games.load_image("menubg.jpg", transparent = False)
    games.screen.background = menubg
    # Just a few static variables
    red   = 255,  0,  0
    green =   0,255,  0
    blue  =   0,  0,255
    size = width, height = 640,480
    screen = pygame.display.set_mode(size)
    games.screen.background = menubg
    pygame.display.update()
    pygame.key.set_repeat(500,30)
    choose = dm.dumbmenu(screen, [
                            'Start Game',
                            'Manual',
                            'Show Highscore',
                            'Quit Game'], 220,150,None,32,1.4,green,red)
    if choose == 0:
        main()
    elif choose == 1:
        manual()
    elif choose == 2:
        print("yay")
    elif choose == 3:
        print ("You choose Quit Game.")

我认为is_pressed() manual()不会等待您的媒体,所以您打电话给mainloop()所以我认为您永远不会离开那个循环。

我在您的代码中看到其他坏主意,例如:

  • menu()调用manual()调用menu()调用manual()等 - 使用return并使用循环menu()
  • 每次打电话给menu()你打电话给pygame.init()pygame.display.set_mode() - 你应该只使用它一次。

编辑:

我不知道games.keyboard.is_pressed()是如何工作的(因为 PyGame 中没有该功能),但我认为manual()可能是:

def manual():
    image = games.load_image("options.jpg")
    games.screen.background = image
    while not games.keyboard.is_pressed(games.K_b):
        pass # do nothing
    # if `B` was pressed so now function will return to menu()

并且您必须在菜单中创建循环:

running = True
while running:
    choose = dm.dumbmenu(screen, [
                        'Start Game',
                        'Manual',
                        'Show Highscore',
                        'Quit Game'], 220,150,None,32,1.4,green,red)
    if choose == 0:
        main()
    elif choose == 1:
        manual()
        # if 'B' was press manual() will return to this place
    elif choose == 2:
        print("yay")
    elif choose == 3:
        running = False
        print ("You choose Quit Game.")

最新更新