How to Create a Pygame Menu Selection

In this tutorial we will create a Simple Main Menu Selection. Pygame is a Free and Open Source python programming language framework for making game applications. It is highly portable and runs on nearly every platform and operating system. It is highly recommended for beginners to learn Pygame when making basic games.

So let's now do the coding...

Getting Started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

After Python IDLE's is installed, open the command prompt then type "python -m pip install pygame", and hit enter.

tut1

Importing Modules

After setting up the installation, run the IDLE and click the file and then the new file. After that, a new window will appear containing a black file this will be the text editor for the python.

Then copy the code that I provided below and paste it inside the IDLE text editor

  1. import pygame
  2. from pygame.locals import *
  3. import os

Writing The Variables

We will assign the certain variables that we will need to declare later in the main loop. To do that just kindly write this code inside your text editor.

  1. # Game Initialization
  2. pygame.init()
  3.  
  4. # Center the Game Application
  5. os.environ['SDL_VIDEO_CENTERED'] = '1'
  6.  
  7. # Game Resolution
  8. screen_width=800
  9. screen_height=600
  10. screen=pygame.display.set_mode((screen_width, screen_height))
  11.  
  12. # Text Renderer
  13. def text_format(message, textFont, textSize, textColor):
  14. newFont=pygame.font.Font(textFont, textSize)
  15. newText=newFont.render(message, 0, textColor)
  16.  
  17. return newText
  18.  
  19.  
  20. # Colors
  21. white=(255, 255, 255)
  22. black=(0, 0, 0)
  23. gray=(50, 50, 50)
  24. red=(255, 0, 0)
  25. green=(0, 255, 0)
  26. blue=(0, 0, 255)
  27. yellow=(255, 255, 0)
  28.  
  29. # Game Fonts
  30. font = "Retro.ttf"
  31.  
  32.  
  33. # Game Framerate
  34. clock = pygame.time.Clock()
  35. FPS=30

Main Function

This is the Main Code for the Pygame application. The code contains several input functions that will allow the object to move and render it to the application. To do that just write this code inside the IDLE text editor.

  1. # Main Menu
  2. def main_menu():
  3.  
  4. menu=True
  5. selected="start"
  6.  
  7. while menu:
  8. for event in pygame.event.get():
  9. if event.type==pygame.QUIT:
  10. pygame.quit()
  11. quit()
  12. if event.type==pygame.KEYDOWN:
  13. if event.key==pygame.K_UP:
  14. selected="start"
  15. elif event.key==pygame.K_DOWN:
  16. selected="quit"
  17. if event.key==pygame.K_RETURN:
  18. if selected=="start":
  19. print("Start")
  20. if selected=="quit":
  21. pygame.quit()
  22. quit()
  23.  
  24. # Main Menu UI
  25. screen.fill(blue)
  26. title=text_format("Sourcecodester", font, 90, yellow)
  27. if selected=="start":
  28. text_start=text_format("START", font, 75, white)
  29. else:
  30. text_start = text_format("START", font, 75, black)
  31. if selected=="quit":
  32. text_quit=text_format("QUIT", font, 75, white)
  33. else:
  34. text_quit = text_format("QUIT", font, 75, black)
  35.  
  36. title_rect=title.get_rect()
  37. start_rect=text_start.get_rect()
  38. quit_rect=text_quit.get_rect()
  39.  
  40. # Main Menu Text
  41. screen.blit(title, (screen_width/2 - (title_rect[2]/2), 80))
  42. screen.blit(text_start, (screen_width/2 - (start_rect[2]/2), 300))
  43. screen.blit(text_quit, (screen_width/2 - (quit_rect[2]/2), 360))
  44. pygame.display.update()
  45. clock.tick(FPS)
  46. pygame.display.set_caption("Python - Pygame Simple Main Menu Selection")

Initializing The Game

This is the Initialization code, this will run the entire code when the application starts. It will render the Main Loop to display the specific methods. To do that just write this code inside the IDLE text editor.

  1. #Initialize the Game
  2. main_menu()
  3. pygame.quit()
  4. quit()

DEMO

There you have it we successfully created a Simple Main Menu Selection using Pygame. I hope that this simple tutorial helps you to what you are looking for and enhance your programming capabilities. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!

Comments

Submitted byPranjal g (not verified)on Wed, 10/16/2019 - 02:22

Do mouse input work? If not can you show us how can we?

Submitted byCyrus Malekani (not verified)on Tue, 04/21/2020 - 23:45

Exception has occurred: NameError name 'os' is not defined File "D:\GMCAT\Python - Pygame Simple Main Menu Selection\main.py", line 8, in os.environ['SDL_VIDEO_CENTERED'] = '1'
Submitted byShadeAce (not verified)on Mon, 05/10/2021 - 16:24

What code would i use to make it so when you select start it wipes the menu off the screen but leaving the window open(on a blank screen)???

Add new comment