I am making a program with a start and help menu, as well as other functions. The buttons on the "main menu" work perfectly, but once I enter the Help menu, the buttons I have programmed for screen navigation do not work properly. Please feel free to look at the code below. I keep getting an error that says "BackHelpRect not defined". BackHelpRect is a rectangle I have drawn around my image buttons (as for all the rest of my buttons) to sense for when the mouse is touching them. It works for the main menu buttons but once I go into the help menu, the rectangles for those buttons "disappear". If you have a solution, please respond, it would be greatly appreciated. :)
Sorry I have attached my entire code, but everything is called within other functions and it all works in sync with each other. Sorry if it is very long.
import pygame
import time
import smtplib
import webbrowser
pygame.init() # Starts pygame.
white = (255, 255, 255)
blue = (0, 100, 175)
black = (0, 0, 0)# Initialising Python with colours and a window size.
Event = []
AllTasks = []
MenuStatus = ""
def OpenMainMenu(): # To open Main Menu.
MenuStatus = "Main Menu" # In case the code needs to know what menu is currently projected.
pygame.display.set_caption("Main Menu") # Naming the New Window.
CurrentMenu = pygame.image.load(r'D:\Holiday Planner Screen Resources\Main Menu.jpg') # Opening a custom made Screen.
Screen.blit(CurrentMenu, (0, 0))
CreateEventB()
OpenEventB()
HelpB()
pygame.display.update()# Loads the Main Menu Screen.
ButtonInteract()
def CreateEventB(): # To Open the "Create Menu Button".
CreateButton = pygame.image.load(r'D:\Holiday Planner Screen Resources\Create Event.jpg') # Open the custom button design.
CreateButton = pygame.transform.scale(CreateButton, (174, 85))
global CreateRect
CreateRect = Screen.blit(CreateButton, (425, 325))
pygame.display.update(CreateRect)
def OpenEventB(): # To Open the "Create Menu Button".
OpenButton = pygame.image.load(r'D:\Holiday Planner Screen Resources\Open Event.jpg') # Open the custom button design.
OpenButton = pygame.transform.scale(OpenButton, (179, 85))
global OpenRect
OpenRect = Screen.blit(OpenButton, (225, 325))
pygame.display.update(OpenRect)
def HelpB():
HelpButton = pygame.image.load(r'D:\Holiday Planner Screen Resources\Help Button.jpg') # Open the custom button design.
HelpButton = pygame.transform.scale(HelpButton, (151, 51))
global HelpRect
HelpRect = Screen.blit(HelpButton, (335, 415))
pygame.display.update(HelpRect)
def BackHelp():
BackHelpB = pygame.image.load(r'D:\Holiday Planner Screen Resources\BackHelp.jpg') # Open the custom button design.
BackHelpB = pygame.transform.scale(BackHelpB, (235, 84))
global BackHelpRect
BackHelpRect = Screen.blit(BackHelpB, (45, 490))
pygame.display.update(BackHelpRect)
def NextHelp():
NextHelpB = pygame.image.load(r'D:\Holiday Planner Screen Resources\NextHelp.jpg') # Open the custom button design.
NextHelpB = pygame.transform.scale(NextHelpB, (233, 85))
global NextHelpRect
NextHelpRect = Screen.blit(NextHelpB, (545, 490))
pygame.display.update(NextHelpRect)
def FinishHelp():
FinishHelpB = pygame.image.load(r'D:\Holiday Planner Screen Resources\FinishHelp.jpg') # Open the custom button design.
FinishHelpB = pygame.transform.scale(NextHelpB, (230, 85))
global FinishHelpRect
FinishHelpRect = Screen.blit(FinishHelpRect, (545, 490))
pygame.display.update(FinishHelpRect)
def ButtonInteract(): # A function that helps to connect button interactions to their respective functions.
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if CreateRect.collidepoint(event.pos):
CreateEvent()
if event.type == pygame.MOUSEBUTTONDOWN:
if OpenRect.collidepoint(event.pos):
Event, AllTasks = OpenEvent("BerlinTestTrip")
print(Event, AllTasks)
if event.type == pygame.MOUSEBUTTONDOWN:
if HelpRect.collidepoint(event.pos):
MenuStatus = "Help menu 1"
Help()
def ButtonsForHelp(): # A seperate button interaction pathway for help buttons as they are housed on a different screen.
global BackHelpRect
global NextHelpRect
global FinishHelpRect
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if BackHelpRect.collidepoint(event.pos): # Button navigaton.
if MenuStatus == "Help menu 1":
OpenMainMenu()
if MenuStatus == "Help menu 2":
MenuStatus = "Help menu 1"
Help()
if MenuStatus == "Help menu 3":
MenuStatus = "Help menu 2"
Help()
if event.type == pygame.MOUSEBUTTONDOWN:
if NextHelpRect.collidepoint(event.pos): # Button navigaton.
if MenuStatus == "Help menu 1":
MenuStatus = "Help menu 2"
Help()
if MenuStatus == "Help menu 2":
MenuStatus = "Help menu 3"
Help()
if event.type == pygame.MOUSEBUTTONDOWN:
if FinishHelpRect.collidepoint(event.pos): # Button navigaton.
if MenuStatus == "Help menu 3":
OpenMainMenu()
def CreateEvent():
print ("Created")
def OpenEvent(EventSaveName):
print ("Opened")
def Help():
pygame.display.set_caption("Help Menu") # Naming the New Window.
CurrentMenu = pygame.image.load(r'D:\Holiday Planner Screen Resources\Help Menu 1.jpg') # Opening a custom made Screen.
Screen.blit(CurrentMenu, (0, 0))
pygame.display.update()
if MenuStatus == "Help menu 1":
CurrentMenu = pygame.image.load(r'D:\Holiday Planner Screen Resources\Help Menu 1.jpg') # Opening a custom made Screen.
Screen.blit(CurrentMenu, (0, 0))
pygame.display.update()# Loads the menu Screen.
BackHelp()
NextHelp()
if MenuStatus == "Help menu 2":
CurrentMenu = pygame.image.load(r'D:\Holiday Planner Screen Resources\Help Menu 2.jpg') # Opening a custom made Screen.
Screen.blit(CurrentMenu, (0, 0))
pygame.display.update()# Loads the menu Screen.
BackHelp()
NextHelp()
if MenuStatus == "Help menu 3":
CurrentMenu = pygame.image.load(r'D:\Holiday Planner Screen Resources\Help Menu 3.jpg') # Opening a custom made Screen.
Screen.blit(CurrentMenu, (0, 0))
pygame.display.update()# Loads the menu Screen.
BackHelp()
FinishHelp()
ButtonsForHelp()
# MAIN BODY
Screen = pygame.display.set_mode((824, 620))
OpenMainMenu()
pygame.display.update()
Thanks.