سؤال

so in my menu in a game I'm creating a picture is displayed which shows how to play the game, and I have some code so that when the player presses the b button it returns to the main menu, but this doesn't work, can someone help me please?

def manual():

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

the 'menu()' is another function which has all the main menu code in it

Here is the menu function

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.")
هل كانت مفيدة؟

المحلول

I think is_pressed() in manual() doesn't wait for your press so you call mainloop() so I think you never leave that loop.

I see other bad ideas in your code like:

  • menu() call manual() which call menu() which call manual() etc. - use return and use loop in menu()
  • every time you call menu() you call pygame.init() and pygame.display.set_mode() - you should use it only once.

EDIT:

I don't know how games.keyboard.is_pressed() works (because there is no that function in PyGame) but I think manual() could be:

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

and you have to create loop in 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.")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top