0% found this document useful (0 votes)
10 views2 pages

Import Pygame

This document is a Python script that uses Pygame to create a graphical menu with multiple options. It initializes a window, renders text for menu items, and handles user interactions such as mouse clicks to select options or quit the menu. The script calculates positions for the text objects to display them properly within the window size.

Uploaded by

Atrak Shown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Import Pygame

This document is a Python script that uses Pygame to create a graphical menu with multiple options. It initializes a window, renders text for menu items, and handles user interactions such as mouse clicks to select options or quit the menu. The script calculates positions for the text objects to display them properly within the window size.

Uploaded by

Atrak Shown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

zzzzimport pygame # Initialize Pygame

pygame.init() # Set window size

window_size = (800, 600) # Create the window

screen = pygame.display.set_mode(window_size) # Define menu items

menu_items = ["Option 1", "Option 2", "Option 3", "Quit"] # Set font and size

font = pygame.font.Font(None, 30) # Create a list of rendered text objects

rendered_text = [font.render(menu_item, True, (255, 255, 255)) for menu_item in menu_items] #


Calculate the size of the largest text object

max_text_width, max_text_height = max([item.get_size() for item in rendered_text]) # Calculate the


number of items that can fit in the window

items_per_column = window_size[1] // (max_text_height + 10) # Calculate the number of columns


required to display all items

num_columns = (len(menu_items) + items_per_column - 1) // items_per_column # Calculate the width


of each column

column_width = max_text_width + 10 # Initialize a list to store the positions of each text object

text_positions = [] # Calculate the position of each text object

for i, item in enumerate(rendered_text):

x = (i // items_per_column) * column_width

y = (i % items_per_column) * (max_text_height + 10)

text_positions.append((x, y)) # Run the menu loop

menu_loop = True

while menu_loop:

for event in pygame.event.get():

if event.type == pygame.QUIT:

menu_loop = False

elif event.type == pygame.MOUSEBUTTONUP:

# Get the mouse position

mouse_pos = pygame.mouse.get_pos()

# Check if the mouse was clicked on a menu item

for i, item in enumerate(rendered_text):


x, y = text_positions[i]

if x <= mouse_pos[0] <= x + column_width and y <= mouse_pos[1] <= y + max_text_height:

if i == len(menu_items) - 1:

# Quit the menu loop

menu_loop = False

else:

# Handle the selected menu item

print(f"You selected {menu_items[i]}")

# Fill the screen with black

screen.fill((0, 0, 0))

# Draw the text objects

for item, position in zip(rendered_text, text_positions):

screen.blit(item, position)

# Update the screen

pygame.display.update() # Quit Pygame

pygame.quit()

You might also like