def refresh(self): if resManager.getVar("SOUND") == True: strtmp = "Sound: ON" else: resManager.setVar("SOUND", False) strtmp = "Sound: OFF" self.listbox.getByIdx(1).setText(strtmp)
def update(self): if self.quit == True: return menustate.MenuState if self.listbox.peek(): filepath = self.listbox.poll() resManager.setVar("MAPFILE", filepath) return mainstate.MainState
def update(self): if self.listbox.peek(): signal = self.listbox.poll() if signal == "l": return loadstate.LoadState elif signal == "s": resManager.setVar("SOUND", not resManager.getVar("SOUND")) self.refresh() elif signal == "e": resManager.setVar("FINISHED", True)
def create(self): self.ctx = pygame.display.get_surface() mapFile = resManager.getVar("MAPFILE") request = resManager.loadJSONFile(mapFile) if request[0] == resManager.DIE: resManager.setVar("ERROR_INFO", "unable to load map file") return errorstate.ErrorState data = request[1] try: self.world = world.World(data["world"]["map"], data["world"]["background"], data["world"]["gravity"]) if self.world.getBlockAt( data["player"] ["position"]) in world.WorldBlock.GroupContainers.SOLIDS: raise Exception #Error, player can't be inside a solid block realPosition = [ data["player"]["position"][0] * self.world.tilesize[0], data["player"]["position"][1] * self.world.tilesize[1] ] self.player = player.Player( self.world, realPosition, (data["player"]["speed"], data["player"]["jumpspeed"]), data["player"]["lifes"], data["player"]["maxLifes"], data["player"]["maxScore"]) self.enemyFactory = enemyfactory.EnemyFactory( self.world, self.player) self.enemyFactory.createEnemies(data["enemies"]) self.world.create() self.world.camera.setFreely(data["camera"]["freely"]) self.world.camera.track(self.player) except Exception: resManager.setVar( "ERROR_INFO", "invalid map file") #Oops, we've got trouble loading this return errorstate.ErrorState self.quit = False
def destroy(self): resManager.setVar("ERROR_INFO", "")
def main(): #Global constants resManager.setVar("SIZE", (720, 480)) resManager.setVar("FPS", 60) resManager.setVar("TITLE", "PySquarePlatform") resManager.setVar("SOUND", True) resManager.setVar("FINISHED", False) pygame.init() pygame.display.set_mode(resManager.getVar("SIZE"), pygame.DOUBLEBUF) ctx = pygame.display.get_surface() pygame.display.set_caption(resManager.getVar("TITLE")) pygame.mixer.set_num_channels(64) icon = pygame.Surface((32, 32)) icon.fill(pygame.Color(0, 0, 0)) pygame.display.set_icon(icon) #Load data here resManager.loadImgs([ ("bg-default", "img/bg-default.png"), ("bg-dark", "img/bg-dark.png"), ("coin", "img/coin.png"), ("hud-heart", "img/hud-heart.png"), ("hud-coin", "img/hud-coin.png"), ("ground-spikes", "img/ground-spikes.png"), ("flag", "img/flag.png") ]) resManager.loadSounds([ ("jump", "sound/jump.ogg"), ("break", "sound/break.ogg"), ("listbox_move", "sound/listbox_move.ogg"), ("listbox_signal", "sound/listbox_signal.ogg") ]) resManager.createSyncAnimation("coin", resManager.getImg("coin"), 20, 24, ticksPerFrame=5, colorKey=(255, 255, 255)) resManager.createImgSurface("ground-spikes", resManager.getImg("ground-spikes"), colorKey=(255, 255, 255)) resManager.createImgSurface("flag", resManager.getImg("flag"), colorKey=(0, 255, 0)) state = changeState(menustate.MenuState) clock = pygame.time.Clock() FPS = resManager.getVar("FPS") while not resManager.getVar("FINISHED"): while pygame.event.peek(): e = pygame.event.poll() state.handle(e) if e.type == pygame.QUIT: resManager.setVar("FINISHED", True) resManager.updateSyncAnimations() newStateClass = state.update() if newStateClass == None: state.draw() else: state.destroy() state = changeState(newStateClass) clock.tick(FPS) pygame.quit()