def getResourcePack(): global resourcePack if not cfg.configuration == {}: resourcePack = cfg.configuration["Core"]["defaultresource"] else: resourcePack = "default" logger.addLog("No specified resource pack, using default!", logger.loglevel["warning"])
def fontLoad(): global resourcePack global path if not resourcePack == "": fontpath = path + "\\resources\\" + resourcePack + "\\fonts\\default.ttf" pyglet.font.add_file(fontpath) tt = ttLib.TTFont(fontpath) global font font = shortName(tt) else: logger.addLog("Resource pack not found!", logger.loglevel["warning"])
def init(): global itempacks, folder itempacks = {} folder = "//items//" for itmfolder in [ f.name for f in os.scandir(os.getcwd() + folder) if f.is_dir() ]: if itmfolder != "__pycache__": try: itempacks[itmfolder] = ItemPack(folder[2:] + itmfolder, itmfolder) except ItemPackCorrupt as e: logger.addLog(e.message, logger.loglevel["warning"]) except DependencyNotFound as e: logger.addLog(e.message, logger.loglevel["warning"])
def startGame(self): logger.addLog("Resolution is " + str(reswidth) + "x" + str(resheight), logger.loglevel["debug"]) if fullscreen: logger.addLog("Fullscreen is enabled", logger.loglevel["debug"]) else: logger.addLog("Fullscreen is disabled", logger.loglevel["debug"]) director.run(mainmenu.loadingScreen())
def loadLvl(level, gamemode): global scroller global player_layer global player global fullmap global gamemoderun global start global r global mapcollider global tilemap_decorations, tilemap_walls global bgLayer try: fullmap = tiles.load(level.datapath) tilemap_walls = fullmap["walls"] tilemap_decorations = fullmap["decorations"] scroller.add(bgLayer, z=-2) scroller.add(tilemap_decorations, z=-1) scroller.add(tilemap_walls, z=0) scroller.add(player_layer, z=1) start = tilemap_walls.find_cells(player_start=True)[0] r = player.get_rect() r.midbottom = start.midbottom player.position = r.center mapcollider = mapcolliders.RectMapCollider(velocity_on_bump="slide") player.collision_handler = mapcolliders.make_collision_handler( mapcollider, tilemap_walls) gamemoderun = player.do(gamemode.main()) except Exception as e: # TODO: if cfg.configuration["Debug"]["developer"] == "False": logger.addLog( "An error was caught rendering the level.\n" + str(e), logger.loglevel["error"]) else: raise e
def game_loading(): logger.addLog("Starting game loading!", logger.loglevel["debug"]) # Start loading, dispatch events when aspect of loading completed logger.addLog("Init items", logger.loglevel["debug"]) # check to see if all item packs are in item pack db, and if all item xml is without error # add item packs and individual items to multidimensional array import items items.init() logger.addLog("Init levels", logger.loglevel["debug"]) # check to see if all levels are in level db, otherwise raise warning, and do not load # Render level thumbnails, and put them as sprite objects into array import levels levels.init() logger.addLog("Init game modes", logger.loglevel["debug"]) # check to see if all game modes exist in game mode db # render game mode metadata and add to multidimensional array dict import modes modes.init() events.mainmenuevents.onProgressFinished()
def animLoad(): global resourcePack global path if not resourcePack == "": animpath = path + "\\resources\\" + resourcePack + "\\animations\\animations.anim" try: with open(animpath, "rb") as file: global animations #animations = pickle.load(file) except pickle.PickleError: logger.addLog("Animation file not decodable", logger.loglevel["warning"]) except FileNotFoundError: logger.addLog("Animation file not found!", logger.loglevel["warning"]) # Switch to default resource pack else: logger.addLog("Resource pack not found!", logger.loglevel["warning"])
def init(): global levels, folder levels = {} folder = "//levels//" for lvlfolder in [ f.name for f in os.scandir(os.getcwd() + folder) if f.is_dir() ]: if lvlfolder != "__pycache__": try: levels[lvlfolder] = Level(folder[2:] + lvlfolder, lvlfolder) except LevelCorrupt as e: logger.addLog(e.message, logger.loglevel["warning"]) except DependencyNotFound as e: logger.addLog(e.message, logger.loglevel["warning"]) except LevelDataCorrupt as e: logger.addLog(e.message + "\n" + str(e.origEx), logger.loglevel["warning"])
def init(): global gamemodes, folder gamemodes = {} folder = "//modes//" for mode in [f.name for f in os.scandir(os.getcwd() + folder)]: if "_" not in mode: try: module = importlib.import_module("modes." + mode) if not mode in list(gamemodes.keys()): gamemodes[mode] = GameMode(module, mode) else: logger.addLog("Game mode " + mode + " is a duplicate of a previously added game mode, gamemode will not be loaded!") continue except PlatformerControllerNotFound: logger.addLog("PlatformerController does not exist in game mode " + mode + ", game mode will not be loaded!", logger.loglevel["warning"]) continue except ModuleNotFoundError: logger.addLog("Game mode " + mode + " is not properly structured, game mode will not be loaded!", logger.loglevel["warning"]) continue
def multiplayerButtonClicked(self): logger.addLog("Multiplayer not yet implemented!")
def progressFinished(self): logger.addLog("Loading finished, displaying Main Menu", logger.loglevel["debug"]) director.replace(FadeTransition(mainmenu.MainMenuScreen(), duration = 0.001))
if cfg.configuration["Debug"]["error"] == "False": import error sys.excepthook = error.fatalError import events import time import logger logger.init() reswidth, resheight = [int(res) for res in cfg.resolution.split("x")] fullscreen = True if cfg.configuration["Core"]["fullscreen"] == "True" else False vsync = True if cfg.configuration["Core"]["vsync"] == "True" else False showfps = True if cfg.configuration["Core"]["showfps"] == "True" else False director.init(width=reswidth, height=resheight, caption="Game", fullscreen=fullscreen, autoscale=True, resizable=True, vsync=vsync) director.set_show_FPS(showfps) logger.addLog("Init resources", logger.loglevel["debug"]) import resources resources.resourceLoad() import mainmenu import gamemenu import leveleditor import settings import controls import renderer renderer.init() import levels import items
def __init__(self, folder, idx): super().__init__() self.folder = folder self.idx = idx if not os.path.exists(self.folder): raise ItemPackCorrupt( "Folder not found! Item pack will not be loaded!") if "main.xml" in os.listdir(self.folder): self.main = os.path.join(self.folder, "main.xml") else: raise ItemPackCorrupt("main.xml not found in item pack " + self.folder + ", item pack will not be loaded!") try: self._itm = et.parse(self.main) except et.ParseError: raise ItemPackCorrupt("main.xml in item pack " + self.folder + " is corrupt, item pack will not be loaded!") self.tags = {} self.items = [] self.item_xml = {} self.item_data = {} self.item_res = {} for item in list(self._itm.getroot()): if item.tag == "name": self.name = item.text if item.tag == "desc" or item.tag == "description": self.desc = item.text if not hasattr(self, "required"): self.required = {} if item.tag == "required": for a in item.attrib: if not item.attrib[a] in self.required: self.required[item.attrib[a]] = [] self.required[item.attrib[a]].append(item.text) for listreq in self.required: for req in range(len(self.required[listreq])): if not os.path.isfile(os.getcwd() + "\\" + self.folder + "\\" + self.required[listreq][req]): print(os.getcwd() + "\\" + self.folder + "\\" + self.required[listreq][req]) raise DependencyNotFound("File " + self.required[listreq][req] + " is not found, item pack " + self.folder + " will not be loaded!") if item.tag == "item" and item.text not in self.items: self.item_xml[item.text[:-4]] = item.text self.items.append(item.text) if item.tag == "thumbnail": pyglet.resource.path.append(os.getcwd() + "\\" + self.folder) pyglet.resource.reindex() self.thumbnail = item.text self.tags[item.tag] = item.text self.tags.update(self._itm.getroot().attrib) if not hasattr(self, "name"): self.name = "Item Pack" if not hasattr(self, "desc"): self.desc = "No description" if self.items == []: logger.addLog( "Item pack " + self.name + " at path " + self.folder + " has no declared items.", logger.loglevel["info"]) else: for itempath in self.items: res = cocos.tiles.load_tiles(self.folder + "\\" + itempath) self.item_res[itempath[:-4]] = res self.item_data[itempath[:-4]] = res.contents[None][ itempath[:-4]] # ~ Is this needed? if not hasattr(self, "background"): self.background = (100, 120, 150, 255)
def installer(installType, dialog="folder"): if installType == modes.GameMode: typeDict = modes.gamemodes typeModule = modes elif installType == items.ItemPack: typeDict = items.itempacks typeModule = items elif installType == levels.Level: typeDict = levels.levels typeModule = levels else: return False if dialog.lower() == "folder": installPath = easygui.diropenbox(title="Select expansion pack folder") elif dialog.lower() == "file": installPath = easygui.fileopenbox( title="Select expansion pack .zip file") else: return False if installPath == "" or installPath == None: return False installPathList = installPath.split("\\") if installPath.lower().endswith(".zip"): newExtensionName = installPathList[-1][:-4] isZip = True else: newExtensionName = installPathList[-1] isZip = False if newExtensionName in list(typeDict.keys()): message.showMessage(newExtensionName + " is already installed!") logger.addLog(newExtensionName + " is already installed!", logger.loglevel["info"]) return False else: message.showMessage("Installing " + installType.modeType + newExtensionName) try: if os.path.isdir(installPath): shutil.copytree( installPath, os.getcwd() + typeModule.folder + newExtensionName) # DEBUG: while os.path.getsize(os.getcwd() + typeModule.folder + newExtensionName) < os.path.getsize( os.getcwd() + typeModule.folder + newExtensionName): print("copying") else: if isZip: with zipfile.ZipFile(installPath, "r") as zip_ref: zip_ref.extractall(os.getcwd() + typeModule.folder + newExtensionName) else: message.showMessage( "The chosen extension is not a folder or zip file.") logger.addLog( newExtensionName + " is not a valid folder or .zip file, cannot be installed!", logger.loglevel["warning"]) return False try: if installType == modes.GameMode: module = importlib.import_module("modes." + newExtensionName) modes.gamemodes[newExtensionName] = modes.GameMode( module, newExtensionName) elif installType == items.ItemPack: items.itempacks[newExtensionName] = items.ItemPack( typeModule.folder[2:] + newExtensionName, newExtensionName) elif installType == levels.Level: levels.levels[newExtensionName] = levels.Level( typeModule.folder[2:] + newExtensionName, newExtensionName) corrupt = False except modes.PlatformerControllerNotFound: message.showMessage("Game Mode " + newExtensionName + " is corrupt, and has not been installed.") logger.addLog( "PlatformerController does not exist in game mode " + newExtensionName + ", game mode will not be loaded!", logger.loglevel["warning"]) corrupt = True except ModuleNotFoundError: message.showMessage("Game Mode " + newExtensionName + " is corrupt, and has not been installed.") logger.addLog( "Game mode " + newExtensionName + " is not properly structured, game mode will not be loaded!", logger.loglevel["warning"]) corrupt = True except items.ItemPackCorrupt as e: message.showMessage("Item Pack " + newExtensionName + " is corrupt, and has not been installed.") logger.addLog(e.message, logger.loglevel["warning"]) corrupt = True except items.DependencyNotFound as e: message.showMessage("Item Pack " + newExtensionName + " is corrupt, and has not been installed.") logger.addLog(e.message, logger.loglevel["warning"]) corrupt = True except levels.LevelCorrupt as e: message.showMessage("Level " + newExtensionName + " is corrupt, and has not been installed.") logger.addLog(e.message, logger.loglevel["warning"]) corrupt = True except levels.DependencyNotFound as e: message.showMessage("Level " + newExtensionName + " is corrupt, and has not been installed.") logger.addLog(e.message, logger.loglevel["warning"]) corrupt = True except levels.LevelDataCorrupt as e: message.showMessage("Level " + newExtensionName + " is corrupt, and has not been installed.") logger.addLog(e.message + "\n" + e.origEx, logger.loglevel["warning"]) corrupt = True finally: if corrupt: shutil.rmtree(os.getcwd() + typeModule.folder + newExtensionName) return False message.showMessage( "Installed " + installType.modeType + " " + newExtensionName + ". Please restart the game to use the new content.") logger.addLog( installType.modeType + " " + newExtensionName + " installed!", logger.loglevel["debug"]) return True except Exception as e: message.showMessage("An error occured installing " + installType.modeType + " " + newExtensionName) logger.addLog( "An error occured installing " + installType.modeType + " " + newExtensionName, logger.loglevel["warning"]) logger.addLog( "Error installing " + installType.modeType + " " + newExtensionName + "\nException:\n" + str(e), logger.loglevel["debug"]) return False