0

I am working in Python and developing a game using the popular library Pygame. I have developed it completely but but now I want to add a menu which contains a picture behind it and has 3 buttons 'Play', 'Instructions' and 'Credits'. I want the game to start on pressing 'Play' and some text to appear on clicking 'Instructions' & 'Credits'. After that I want a 'Retry' button to appear on my game over display. If you know how to do so, please tell me.

  • You can find examples [here](https://stackoverflow.com/questions/59726334/how-to-ask-20-multiple-choice-questions-on-pygame/59729471#59729471), [here](https://stackoverflow.com/questions/59678318/how-can-i-make-my-sprite-launch-an-object-towards-the-mouse-position/59681310#59681310) and [here](https://stackoverflow.com/questions/14700889/pygame-level-menu-states/14727074#14727074) – sloth Nov 12 '20 at 07:21

1 Answers1

1

This is easy to do First make sure your game is in a function like

def startgame():
   content..... 

This is necessary to do and a after that define a function for a button and in the button function write:

def button(x,y,w,h):
    pos = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if pos[0] > x and pos[0] < x + w and pos[1] > y and pos[1] < y + h:
       if click[0] == 1:
         startgame()
    pygame.draw.rect(screen, color, (x,y,w,h))

In this way you can create a button,

Next create a menu function like:

def menu():

 while True:

    surface.blit(background, (0, 0))


    button(x,y,w,h)

    for event in pygame.event.get():
       if event.type == pygame.QUIT:
           pygame.quit()
           sys.exit()
    pygame.display.update()

menu()