def __init__(self): Scene.__init__(self) self.BACKGROUND = Colors.BLACK self.name = "ship" self.menu = list() self.v_orientation = MenuVOrientation.TOP self.h_orientation = MenuHOrientation.RIGHT self.theme = MenuTheme() self.menu_util = MenuUtil() self.planet = None # Menu definition lbl = dict() lbl["name"] = "lblMenu" lbl["type"] = MenuObjectType.TITLE lbl["text"] = "Planet" self.menu.append(lbl) lbl = dict() lbl["name"] = "lblItem" lbl["type"] = MenuObjectType.LABEL lbl["text"] = "Title Here " self.menu.append(lbl) self.item = lbl btn = dict() btn["name"] = "N/A" btn["type"] = MenuObjectType.CONTAINER btn["content"] = list() self.menu.append(btn) btn = dict() btn["name"] = "btnNext" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Next" self.menu.append(btn) btn = dict() btn["name"] = "btnPrevious" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Previous" self.menu.append(btn) btn = dict() btn["name"] = "btnClose" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Close" self.menu.append(btn) #Library and Objects self.library = LibraryManager().planets self.index = 0
def on_init(self): self.name = "menu" self.inited = True #Redefinie Background color self.BACKGROUND = Colors.BLACK #Title self.title = "This is a menu, click any button" self.title_font = pygame.font.SysFont("Arial", 24) #Menu Init SceneMenu.on_init(self) self.v_orientation = MenuVOrientation.CENTER self.h_orientation = MenuHOrientation.CENTER self.theme = MenuTheme() self.menu_util = MenuUtil() #**************************************** #Menu Content self.addLabel("lblMenu", "This is a Title" ) self.addMenu("btn1", "Click Rectangle", "Scene 1") self.addMenu("btnQuit", "Quit", None) # btnQuit : This is a reserved word #**************************************** #Load pygame image img = "example/images/pygame.png" self.image = pygame.image.load(img).convert_alpha() self.rect = self.image.get_rect(center = (self.width //2, 440)) #Last step of intitialisation self.menu_util.on_init(self.size, self.menu_list, self.v_orientation, self.h_orientation) self.inited = True
class App: FPS = 60 # Handle scenes scenes = dict() active_scene = None theme = MenuTheme() v_orientation = MenuVOrientation.CENTER h_orientation = MenuHOrientation.RIGHT def __init__(self): """ App object initialization function, here you set variables default values""" self._running = True self._display_surf = None self.image = None self.clock = pygame.time.Clock() def on_init(self): """Game initialisation function, here you load scenes, images, sounds, etc.""" full_size = (self.theme["window_width"], self.theme["window_height"]) # pygame initialisation pygame.init() self._display_surf = pygame.display.set_mode(full_size) # Set the window caption pygame.display.set_caption("JASK - Asset Manager") # Load scenes #*************************************************** new_scene = SceneSplash() new_scene.size = full_size new_scene.on_init() self.scenes["splash"] = new_scene new_scene = SceneMenu() new_scene.size = full_size new_scene.on_init() self.scenes["menu"] = new_scene new_scene = SceneShip() new_scene.size = full_size new_scene.on_init() self.scenes["ship"] = new_scene new_scene = SceneAsteroid() new_scene.size = full_size new_scene.on_init() self.scenes["asteroid"] = new_scene new_scene = ScenePlanet() new_scene.size = full_size new_scene.on_init() self.scenes["planet"] = new_scene new_scene = SceneStar() new_scene.size = full_size new_scene.on_init() self.scenes["star"] = new_scene new_scene = SceneMonster() new_scene.size = full_size new_scene.on_init() self.scenes["monster"] = new_scene new_scene = SceneStation() new_scene.size = full_size new_scene.on_init() self.scenes["station"] = new_scene # *************************************************** #Display First Scene self.active_scene = self.scenes["splash"] self.debug_font = pygame.font.SysFont("Arial", 14) #Load Library Content lm = LibraryManager() def on_event(self, event): """Function designed to hanlde events such as user input (keyboard, mouse, etc""" # Quit Event if event.type == pygame.QUIT: print("Event Quit") self._running = False elif event.type == pygame.USEREVENT: #Switch scene event if "goto" in event.__dict__: # Scene on_event #print("Goto : " + event.__dict__["goto"]) if event.__dict__["goto"] in self.scenes: self.active_scene = self.scenes[event.__dict__["goto"]] else: raise Exception('Scene' + event.__dict__["goto"] + " not found") #Event on active Scene if self.active_scene is not None: self.active_scene.on_event(event) def on_loop(self): """Function to specify the game logic""" # Scene on_loop if self.active_scene is not None: self.active_scene.on_loop() #Active scene is cleared if self.active_scene.next is None: #Handle splash to menu if self.active_scene.name == "splash": self.active_scene = self.scenes["menu"] self.active_scene = self.active_scene.next #Quit application if no more active scenes if self.active_scene is None: print("Quitting application") self._running = False def on_render(self): """Function specialized for surface rendering only""" # Scene on_render if self.active_scene is not None: self.active_scene.on_render(self._display_surf) #Display actual FPS text = f"FPS : {int(self.clock.get_fps())}" Util().draw_text(self._display_surf, text, self.debug_font, Colors.WHITE, 20, 20) # Display update instruction rect = self._display_surf.get_rect() pygame.display.update(rect) def on_cleanup(self): """Function to handle app uninitialized""" pygame.quit() def on_execute(self): """Main Execute function and main loop, calls on_init, on_event, on_loop, on_logic """ # Init application self.on_init() # Main Loop while self._running: # Handle Events for event in pygame.event.get(): self.on_event(event) # Game Logic self.on_loop() # Render code self.on_render() #Ensure FPS is respected self.clock.tick(self.FPS) self.on_cleanup()
def __init__(self): Scene.__init__(self) self.name = "menu" pygame.font.init() self.BACKGROUND = Colors.BLACK self.theme = MenuTheme() self.theme["btn_text_color"] = Colors.BLACK #dosent work self.menu_util = MenuUtil() self.menu = list() self.fast_menu = dict() #Menu definition lbl = dict() lbl["name"] = "lblMenu" lbl["type"] = MenuObjectType.TITLE lbl["text"] = "Menu" self.menu.append(lbl) btn = dict() btn["name"] = "btnShipObject" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Ships" btn["goto"] = "ship" self.menu.append(btn) self.fast_menu[btn["name"]] = btn btn = dict() btn["name"] = "btnAsteroidObject" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Asteroids" btn["goto"] = "asteroid" self.menu.append(btn) self.fast_menu[btn["name"]] = btn btn = dict() btn["name"] = "btnPlanetObject" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Planet" btn["goto"] = "planet" self.menu.append(btn) self.fast_menu[btn["name"]] = btn btn = dict() btn["name"] = "btnStarObject" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Star" btn["goto"] = "star" self.menu.append(btn) self.fast_menu[btn["name"]] = btn btn = dict() btn["name"] = "btnStationObject" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Station" btn["goto"] = "station" self.menu.append(btn) self.fast_menu[btn["name"]] = btn btn = dict() btn["name"] = "btnMonsterObject" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Monster" btn["goto"] = "monster" self.menu.append(btn) self.fast_menu[btn["name"]] = btn btn = dict() btn["name"] = "btnQuit" btn["type"] = MenuObjectType.BUTTON btn["text"] = "Quit" self.menu.append(btn)