├── assets ├── Background.png ├── Options Rect.png ├── Play Rect.png ├── Quit Rect.png └── font.ttf ├── button.py └── main.py /assets/Background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baraltech/Menu-System-PyGame/551c6f3262cdb0d91b0803fa4a1beb8136c90bc1/assets/Background.png -------------------------------------------------------------------------------- /assets/Options Rect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baraltech/Menu-System-PyGame/551c6f3262cdb0d91b0803fa4a1beb8136c90bc1/assets/Options Rect.png -------------------------------------------------------------------------------- /assets/Play Rect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baraltech/Menu-System-PyGame/551c6f3262cdb0d91b0803fa4a1beb8136c90bc1/assets/Play Rect.png -------------------------------------------------------------------------------- /assets/Quit Rect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baraltech/Menu-System-PyGame/551c6f3262cdb0d91b0803fa4a1beb8136c90bc1/assets/Quit Rect.png -------------------------------------------------------------------------------- /assets/font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baraltech/Menu-System-PyGame/551c6f3262cdb0d91b0803fa4a1beb8136c90bc1/assets/font.ttf -------------------------------------------------------------------------------- /button.py: -------------------------------------------------------------------------------- 1 | class Button(): 2 | def __init__(self, image, pos, text_input, font, base_color, hovering_color): 3 | self.image = image 4 | self.x_pos = pos[0] 5 | self.y_pos = pos[1] 6 | self.font = font 7 | self.base_color, self.hovering_color = base_color, hovering_color 8 | self.text_input = text_input 9 | self.text = self.font.render(self.text_input, True, self.base_color) 10 | if self.image is None: 11 | self.image = self.text 12 | self.rect = self.image.get_rect(center=(self.x_pos, self.y_pos)) 13 | self.text_rect = self.text.get_rect(center=(self.x_pos, self.y_pos)) 14 | 15 | def update(self, screen): 16 | if self.image is not None: 17 | screen.blit(self.image, self.rect) 18 | screen.blit(self.text, self.text_rect) 19 | 20 | def checkForInput(self, position): 21 | if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom): 22 | return True 23 | return False 24 | 25 | def changeColor(self, position): 26 | if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom): 27 | self.text = self.font.render(self.text_input, True, self.hovering_color) 28 | else: 29 | self.text = self.font.render(self.text_input, True, self.base_color) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | from button import Button 3 | 4 | pygame.init() 5 | 6 | SCREEN = pygame.display.set_mode((1280, 720)) 7 | pygame.display.set_caption("Menu") 8 | 9 | BG = pygame.image.load("assets/Background.png") 10 | 11 | def get_font(size): # Returns Press-Start-2P in the desired size 12 | return pygame.font.Font("assets/font.ttf", size) 13 | 14 | def play(): 15 | while True: 16 | PLAY_MOUSE_POS = pygame.mouse.get_pos() 17 | 18 | SCREEN.fill("black") 19 | 20 | PLAY_TEXT = get_font(45).render("This is the PLAY screen.", True, "White") 21 | PLAY_RECT = PLAY_TEXT.get_rect(center=(640, 260)) 22 | SCREEN.blit(PLAY_TEXT, PLAY_RECT) 23 | 24 | PLAY_BACK = Button(image=None, pos=(640, 460), 25 | text_input="BACK", font=get_font(75), base_color="White", hovering_color="Green") 26 | 27 | PLAY_BACK.changeColor(PLAY_MOUSE_POS) 28 | PLAY_BACK.update(SCREEN) 29 | 30 | for event in pygame.event.get(): 31 | if event.type == pygame.QUIT: 32 | pygame.quit() 33 | sys.exit() 34 | if event.type == pygame.MOUSEBUTTONDOWN: 35 | if PLAY_BACK.checkForInput(PLAY_MOUSE_POS): 36 | main_menu() 37 | 38 | pygame.display.update() 39 | 40 | def options(): 41 | while True: 42 | OPTIONS_MOUSE_POS = pygame.mouse.get_pos() 43 | 44 | SCREEN.fill("white") 45 | 46 | OPTIONS_TEXT = get_font(45).render("This is the OPTIONS screen.", True, "Black") 47 | OPTIONS_RECT = OPTIONS_TEXT.get_rect(center=(640, 260)) 48 | SCREEN.blit(OPTIONS_TEXT, OPTIONS_RECT) 49 | 50 | OPTIONS_BACK = Button(image=None, pos=(640, 460), 51 | text_input="BACK", font=get_font(75), base_color="Black", hovering_color="Green") 52 | 53 | OPTIONS_BACK.changeColor(OPTIONS_MOUSE_POS) 54 | OPTIONS_BACK.update(SCREEN) 55 | 56 | for event in pygame.event.get(): 57 | if event.type == pygame.QUIT: 58 | pygame.quit() 59 | sys.exit() 60 | if event.type == pygame.MOUSEBUTTONDOWN: 61 | if OPTIONS_BACK.checkForInput(OPTIONS_MOUSE_POS): 62 | main_menu() 63 | 64 | pygame.display.update() 65 | 66 | def main_menu(): 67 | while True: 68 | SCREEN.blit(BG, (0, 0)) 69 | 70 | MENU_MOUSE_POS = pygame.mouse.get_pos() 71 | 72 | MENU_TEXT = get_font(100).render("MAIN MENU", True, "#b68f40") 73 | MENU_RECT = MENU_TEXT.get_rect(center=(640, 100)) 74 | 75 | PLAY_BUTTON = Button(image=pygame.image.load("assets/Play Rect.png"), pos=(640, 250), 76 | text_input="PLAY", font=get_font(75), base_color="#d7fcd4", hovering_color="White") 77 | OPTIONS_BUTTON = Button(image=pygame.image.load("assets/Options Rect.png"), pos=(640, 400), 78 | text_input="OPTIONS", font=get_font(75), base_color="#d7fcd4", hovering_color="White") 79 | QUIT_BUTTON = Button(image=pygame.image.load("assets/Quit Rect.png"), pos=(640, 550), 80 | text_input="QUIT", font=get_font(75), base_color="#d7fcd4", hovering_color="White") 81 | 82 | SCREEN.blit(MENU_TEXT, MENU_RECT) 83 | 84 | for button in [PLAY_BUTTON, OPTIONS_BUTTON, QUIT_BUTTON]: 85 | button.changeColor(MENU_MOUSE_POS) 86 | button.update(SCREEN) 87 | 88 | for event in pygame.event.get(): 89 | if event.type == pygame.QUIT: 90 | pygame.quit() 91 | sys.exit() 92 | if event.type == pygame.MOUSEBUTTONDOWN: 93 | if PLAY_BUTTON.checkForInput(MENU_MOUSE_POS): 94 | play() 95 | if OPTIONS_BUTTON.checkForInput(MENU_MOUSE_POS): 96 | options() 97 | if QUIT_BUTTON.checkForInput(MENU_MOUSE_POS): 98 | pygame.quit() 99 | sys.exit() 100 | 101 | pygame.display.update() 102 | 103 | main_menu() --------------------------------------------------------------------------------