I wrote this to teach myself how to make a menu with buttons and I want to know if I've done well and if there's any way to make it shorter and/or more efficient.
import pygame, sys
from pygame import *
run = True
global newBG
newBG = False
#-------------------------------------------------------------------------------
def mainLoop():
global win, newW, newH, newBG
newW, newH = (960 , 508)
win = pygame.display.set_mode ((newW, newH) , pygame.RESIZABLE)
pygame.display.set_caption("SMC")
backGround = 0
while run:
global mouse, currentScreen
mouse = pygame.mouse.get_pos()
currentScreen()
while newBG:
drawBackground()
newBG = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.VIDEORESIZE:
win = pygame.display.set_mode ( (event.w, event.h) , pygame.RESIZABLE )
newW, newH = event.w, event.h
drawBackground()
uiPlate()
if event.type == pygame.MOUSEBUTTONDOWN:
for b in buttonList:
b.click()
pygame.display.update()
#------------------------------------------------------------------------------
def mainMenu():
global backGroundImage, buttonList, currentScreen
backGroundImage = pygame.image.load('Background.jpg')
ButtonPic = pygame.image.load('Button.jpg')
ButtonHoverPic = pygame.image.load('ButtonHover.jpg')
ButtonPressPic = pygame.image.load('ButtonPress.jpg')
buttonList = [
button(screenSwap, secondMenu, ButtonPic, ButtonHoverPic, ButtonPressPic, .1, .1, .1, .1),
button('Hello', None, ButtonPic, ButtonHoverPic, ButtonPressPic, .1, .25, .1, .1),
button('Hello to you!', None, ButtonPic, ButtonHoverPic, ButtonPressPic, .1, .50, .1, .1)
]
for b in buttonList:
b.drawButton()
#------------------------------------------------------------------------------
def secondMenu():
global backGroundImage, buttonList
backGroundImage = pygame.image.load('Button.jpg')
ButtonPic = pygame.image.load('Background.jpg')
ButtonHoverPic = pygame.image.load('ButtonHover.jpg')
ButtonPressPic = pygame.image.load('ButtonPress.jpg')
buttonList = [
button(screenSwap, mainMenu, ButtonPic, ButtonHoverPic, ButtonPressPic, .75, .1, .1, .1),
button('Goodbye', None, ButtonPic, ButtonHoverPic, ButtonPressPic, .75, .25, .1, .1),
button('Goodbye to you friend!', None, ButtonPic, ButtonHoverPic, ButtonPressPic, .75, .50, .1, .1)
]
for b in buttonList:
b.drawButton()
#------------------------------------------------------------------------------
uiX = 0
uiY = 0
uiW = 960
uiH = 508
def uiPlate():
global uiW, uiH, uiX, uiY
uiW = (int(newW))
uiH = (int(uiW*0.5296875))
if uiH > newH:
uiH = int(newH)
uiW = (int(uiH/0.5296875))
uiX = (int((newW - uiW)/2))
uiY = (int((newH - uiH)/2))
## pygame.draw.rect(win, (100, 255, 100,), (uiX, uiY, uiW, uiH))
## print(uiW, uiH, uiX, uiY)
#------------------------------------------------------------------------------
def drawBackground():
global backGround
backGround = pygame.transform.scale(backGroundImage, (newW, newH))
win.blit((backGround), (0,0))
#------------------------------------------------------------------------------
class button:
global uiX, uiY, uiW, uiH
def __init__(self, action, actionProps, ButtonPic, ButtonHoverPic, ButtonPressPic, x, y, w, h):
self.ButtonPic = ButtonPic
self.ButtonHoverPic = ButtonHoverPic
self.ButtonPressPic = ButtonPressPic
self.bx = int(uiX + (x * uiW))
self.by = int(uiY + (y * uiH))
self.bw = int(w * uiW)
self.bh = int(h * uiH)
self.action = action
self.actionProps = actionProps
def drawButton(self):
click = pygame.mouse.get_pressed()
ButtonPic = pygame.transform.scale(self.ButtonPic, (self.bw, self.bh))
ButtonHoverPic = pygame.transform.scale(self.ButtonHoverPic, (self.bw, self.bh))
ButtonPressPic = pygame.transform.scale(self.ButtonPressPic, (self.bw, self.bh))
win.blit(ButtonPic, (self.bx, self.by))
if ((self.bx < mouse[0] < (self.bx + self.bw)) and (self.by < mouse[1] < (self.by + self.bh))):
win.blit(ButtonHoverPic, (self.bx, self.by))
if click[0]:
win.blit(ButtonPressPic, (self.bx, self.by))
def click(self):
if ((self.bx < mouse[0] < (self.bx + self.bw)) and (self.by < mouse[1] < (self.by + self.bh))):
if self.actionProps != None:
self.action(self.actionProps)
else:
print(self.action)
#------------------------------------------------------------------------------
def screenSwap(screen):
global currentScreen, newBG
currentScreen = screen
newBG = True
#------------------------------------------------------------------------------
currentScreen = mainMenu
mainLoop()
Sorry about the length, if it's too much to bother looking through, I'd like to be shown some code for a well-made menu system that I can compare to.
mainMenuandsecondMenuare incorrectly indented. Please fix this before review. \$\endgroup\$