Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit d3b0771aac24a712143d2be66336e0c1dc17144c
parent 0d01389eaac81d1b915cb739b1687d098fb55992
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Fri,  2 Oct 2020 15:48:47 -0500

First parts of menu system

Diffstat:
Msrc/constants.py | 1+
Asrc/menu.py | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/src/constants.py b/src/constants.py @@ -32,3 +32,4 @@ BOARD_PATH = os.path.join(DATA_PATH, "map") JSON_PATH = os.path.join(DATA_PATH, "json") ENTITY_JSON_PATH = os.path.join(JSON_PATH, "ents") STATUS_JSON_PATH = os.path.join(JSON_PATH, "stats") +MENU_JSON_PATH = os.path.join(JSON_PATH, "menus") diff --git a/src/menu.py b/src/menu.py @@ -0,0 +1,72 @@ +import pygame, json, os +from . import images, vgo, manager +from .constants import MENU_JSON_PATH + +########### +# menu.py # +########### + +# This file contains: +# 1. The MenuManager class, which handles Menu objects and switches between them +# 2. The Menu class, which consists of a background, 0 or more decorative Entities, and an ordered series of Button objects + +################################## +# Section 1 - MenuManager Object # +################################## + +class MenuManager(manager.Manager): + """ + Manager for Menu objects. Mostly responsible for switching + between Menus and for updating their contents. The Menu + object itself controls sprite groups, but MenuManager will + access those groups to draw them. + """ + + def __init__(self, game): + + super().__init__(game) + + # MenuManager values + self.current_menu = None + self.loaded_menus = {} + + def load_menu_from_file(self, menufile): + """ + Load a Menu object from a provided menu definition + file. + """ + self.loaded_menus[menufile] = Menu(self, menufile) + + def switch_to_menu(self, menuname): + """ + Switch to the given loaded Menu. + """ + if menuname in self.loaded_menus.keys(): + self.current_menu = self.loaded_menus[menuname] + + def update_current_menu(self, surface = None): + """ + Update the current Menu object and all of its + sub-entities. + """ + if surface != None and self.current_menu != None: + surface.blit(self.current_menu.background, (0, 0)) + self.current_menu.update_menu(surface) + +########################### +# Section 2 - Menu Object # +########################### + +class Menu(object): + """ + Class that represents the menus that are interacted with in + menu mode. Contains a background sprite object as well as a + group buttons. + """ + + def __init__(self, manager, menufile): + + # Saved values + self.manager = manager + self.menu_file = menufile + self.definition = json.load(open(os.path.join(MENU_JSON_PATH, menufile)))