Prasoon Jha
Prasoon Jha

Reputation: 23

How to add Main Menu and Retry menu in pygame

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.

Upvotes: 0

Views: 1694

Answers (1)

Syntax Terminator
Syntax Terminator

Reputation: 11

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

Upvotes: 1

Related Questions