Programming: Menus (March 2020)

Originally posted March 4th, 2020
Another month has gone by, and I’m proud to say that I’ve remained committed to my personal coding project. I’m beginning to feel confident understanding the general structure of programs. As an exercise, I’ll breakdown the feature that I’m developing at the moment: the game menu.

When you load up a game, it doesn’t just throw you into the level. Instead there’s a start screen, option screen, level select screen, etc.

Credit to programmingpixels for the guide I followed for implementing this into my own project.
The Code

I first create a “GameState” enum class. The enum class takes a unique name and assigns it to a unique number; similar to a dictionary’s key-value relationship. In this instance, it shall hold the different modes of the game; whether it is on the “TITLE” screen or “SELECT” screen.
 
 
 
 
class GameState(enum):
TITLE = 0
SELECT = 1
I define the “main():” function to hold the value of the “GameState” value. The bulk of the code is the “While” loop. This how programs keeps refreshing itself to update with the different variables and inputs provided. By default, Python will run through the code in about 3ms (300 times per second), depending on the code and processor speed. This is way too fast for a game, so there is a way to limit FPS to a reasonable rate.

So we need the “While” loop to determine what function to run, and refresh automatically when the “GameState” is changed.
 
def main():
pygame.init() #Initialize the pygame module
win = pygame.display_set_mode((500,500)) #Displays game window
game_state = GameState.TITLE
 
while True:
if game_state == GameState.TITLE:
game_state = title_screen(win)
elif game_state == GameState.SELECT:
game_state = select_screen(win)
The “title_screen(win)” and “select_screen(win)” functions are the actual meat of the “main()” function. When they are executed, they’re loading the graphics, sound effects, etc. To keep it bare bones, I’ll describe the minimum of what would be needed to toggle between the two game states.
 
def title_screen(win):
font = pygame.font.SysFont (‘constantia’, 24) #Define font
title = font.render(‘Title Screen’, 1,(255,255,255))#Text surface
keys = pygame.key.get_pressed() #Enables user input
 
while True:
win.blit(title, (250,250)) #Draws text on window
if keys[pygame.K_SPACE]:#When user presses the spacebar
return GameState.SELECT #Update the ‘game_state’ value
 
def select_screen(win):
font = pygame.font.SysFont (‘constantia’, 24) #Define font
title = font.render(‘Select Screen’, 1,(255,255,255))#Text surface
keys = pygame.key.get_pressed() #Enables user input
 
while True:
win.blit(select, (250,250)) #Draws text on window
if keys[pygame.K_SPACE]:
return GameState.TITLE #Update the ‘game_state’ value
The functions above perform the following:
  1. Import the font from the pygame module
  2. Create a text surface and display it on screen
  3. Enable user input from the pygame module
  4. When the user presses the spacebar, update the “GameState” value
The functions above have their own While loop (within the “main()” while loop). While executing the function, the “main()” is essentially just waiting for a return value. Plenty of other actions (settings, entire gameplay) can occur before the value gets returned and updates the game state.

It checks if the spacebar has been pressed and returns the “GameState” value to update the “game_state” value in the “main()” While loop. If the “game_state” value changes, it changes the conditional to run a different function:

main()

The last line of the code is to simply run the “main()” function, which begins the infinite While loop to check the “GameState”. This not confusing figure should help summarize everything:

And a treat for sticking with it, here’s how things are shaping up:

Comments