Table of contents
1.
Introduction
2.
Creating a start menu in Pygame
3.
Source Code for Creating a Start Menu in Python using pygame 
3.1.
Output Screen
4.
FAQs
4.1.
Is Pygame a library or an engine?
4.2.
What are Pygame features?
4.3.
What is the usefulness of Pygame?
4.4.
What graphics does Pygame use?
4.5.
Is Pygame worth learning?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Creating a Start Menu in Pygame

Author Teesha Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pygame is a free and open-source cross-platform framework for creating multimedia applications, such as video games, with Python. It abstracts the most frequent functions using the Simple DirectMedia Layer framework and many other famous libraries, making designing these programs more accessible.
Also see, Merge Sort Python

Creating a start menu in Pygame

Pygame lacks a built-in layout design or user interface framework, making it challenging to create UI or stages for a game. In pygame, the only key to creating stages or alternative menus is to use functions.

In Pygame, different functions can be used to include special menus or levels by declaring an event type within every function and then accessing the functions using their container function. 

For example, we are creating a game with a start menu and various levels. The start menu will have options to play, change settings, and exit. So this start menu is the container function that has our functions for starting the game, changing the settings, and exiting the game by clicking the respective buttons. 

This article will discuss creating this start menu in python using the pygame library.  

Note: The pygame library does not come with the standard python package, so you will first need to install pygame in your system. Once installed, you can import it into your python module and use its functionalities. 

Source Code for Creating a Start Menu in Python using pygame 

"""
Importing important libraries
"""
import pygame, sys

"""
Setting up an environment to initialize pygame
"""
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('game base')
screen = pygame.display.set_mode((600, 300),0,32)
 
#setting font settings
font = pygame.font.SysFont(None, 30)
 
"""
A function that can be used to write text on our screen and buttons
"""
def draw_text(text, font, color, surface, x, y):
    textobj = font.render(text, 1, color)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)
 
# A variable to check for the status later
click = False
 
# Main container function that holds the buttons and game functions
def main_menu():
    while True:
 
        screen.fill((0,190,255))
        draw_text('Main Menu', font, (0,0,0), screen, 250, 40)
 
        mx, my = pygame.mouse.get_pos()

        #creating buttons
        button_1 = pygame.Rect(200, 100, 200, 50)
        button_2 = pygame.Rect(200, 180, 200, 50)

        #defining functions when a certain button is pressed
        if button_1.collidepoint((mx, my)):
            if click:
                game()
        if button_2.collidepoint((mx, my)):
            if click:
                options()
        pygame.draw.rect(screen, (255, 0, 0), button_1)
        pygame.draw.rect(screen, (255, 0, 0), button_2)
 
        #writing text on top of button
        draw_text('PLAY', font, (255,255,255), screen, 270, 115)
        draw_text('OPTIONS', font, (255,255,255), screen, 250, 195)


        click = False
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    click = True
 
        pygame.display.update()
        mainClock.tick(60)
 
"""
This function is called when the "PLAY" button is clicked.
"""
def game():
    running = True
    while running:
        screen.fill((0,0,0))
       
        draw_text('GAME SCREEN', font, (255, 255, 255), screen, 20, 20)
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    running = False
       
        pygame.display.update()
        mainClock.tick(60)

"""
This function is called when the "OPTIONS" button is clicked.
"""
def options():
    running = True
    while running:
        screen.fill((0,0,0))
 
        draw_text('OPTIONS SCREEN', font, (255, 255, 255), screen, 20, 20)
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    running = False
       
        pygame.display.update()
        mainClock.tick(60)
 
main_menu()
You can also try this code with Online Python Compiler
Run Code

Output Screen

FAQs

Is Pygame a library or an engine?

Pygame is an open-source python library and uses the superb SDL library to create multimedia applications such as games. C, Python, Native, and OpenGL are all used in this module.

What are Pygame features?

Pygame enables you to produce fully functional games and multimedia apps in Python. Pygame is highly portable, running on almost any platform and system software. 

What is the usefulness of Pygame?

You may control the logic and aesthetics of your games with the pygame module without bothering about the backstage complications of dealing with video and audio.

What graphics does Pygame use?

Pygame uses raster graphics. It can manipulate individual pixels quickly and runs on practically every platform.

Is Pygame worth learning?

Pygame is an excellent tool for newbies to get familiar with programming and the game production process and feel rewarded when creating games. 

Conclusion

This article discussed how to create a start menu in pygame. We discussed the way to initialize the screen and add buttons to create a start menu. I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass