【问题标题】:Pygame and Livewires button press not working?Pygame 和 Livewires 按钮按下不起作用?
【发布时间】:2013-11-26 13:29:21
【问题描述】:

所以在我的游戏菜单中,我正在创建一张显示如何玩游戏的图片,并且我有一些代码,以便当玩家按下 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.")

【问题讨论】:

  • 菜单功能有什么作用?你试过调试你的代码吗?您还写了 - 返回主菜单,这是否意味着从主菜单调用手动功能?
  • 手动功能从菜单功能中调用一次是
  • 在 menu() 中调用 manual() 调用 menu() 和 call manual() 等 - 这是个坏主意。

标签: python pygame livewires


【解决方案1】:

我认为manual() 中的is_pressed() 不会等待你的新闻,所以你打电话给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.")

【讨论】:

  • 那么我怎样才能让手动功能留在屏幕上呢?也感谢您帮助我清理代码 :)
  • 我添加一些建议来回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多