def loadLevelGeometry(self): w, h = base.SCREEN_WIDTH, base.SCREEN_HEIGHT fname, tname, codes, background = levelfile.loadLevelFile( self.levelFileName) tname = os.path.join('levels/tilesets', tname + '.png') fname = os.path.join('levels', fname + '.tga') background = os.path.join('levels', background + ".png") ## load the level from tga file try: self.tga_load_level(fname, 1) except IOError: raise ResourceException("Failed to level tga file \"" + fname + "\"") ## load the tileset try: self.tga_load_tiles(tname, (TILE_SIZE, TILE_SIZE), self.tdata) except IOError: raise ResourceException("Failed to load tileset from file \"" + tname + "\"") self.bounds = pygame.Rect(0, 0, len(self.tlayer[0]) * TILE_SIZE, (len(self.tlayer) * TILE_SIZE) + base.HUD_HEIGHT_INACTIVE) ## Load background tiles and randomize background. try: self.bg.tga_load_tiles(background, (BG_TILE_SIZE, BG_TILE_SIZE), self.tdata) except IOError: raise base.ResourceException( "failed to load level background from \"" + background + "\"") ## parallaxing moves twice as slow as the foreground, but should contain ## the entire field. bgW = 5 + (self.size[0] / 4 - 5) / 2 bgH = 4 + (self.size[1] / 4 - 4) / 2 self.bg.resize((bgW, bgH)) self.bg.bounds = pygame.Rect(0, 0, len(self.bg.tlayer[0]) * BG_TILE_SIZE, (len(self.bg.tlayer) * BG_TILE_SIZE) + base.HUD_HEIGHT_INACTIVE) for y in range(self.bg.size[1]): for x in range(self.bg.size[0]): self.bg.set((x, y), random.randint(0, 3))
def loadLevelFile(filename): f = None fn = os.path.join('levels',filename + ".lev") try: f = file(fn) except IOError: raise base.ResourceException("Cannot load level file \"" + fn + "\"") lines = f.readlines() if(len(lines) != 4): raise Exception("Invalid level file, the line count does not match four.", REQUIRED_FIELDS, "When the file has these lines:",lines) for i in range(0,len(lines)): if platform.system() == 'Windows': lines[i] = lines[i].strip("\n") elif platform.system() == 'Linux': lines[i] = lines[i].strip("\r\n") #assuming the files are save in Windows text f.close() return lines
def loop(self): gameVariables = self.main.gameVariables levelName = "" currentLevel = None running = True while running: while currentLevel == None: levelName = "gamelevel" + str( self.data['chapter']) + "_" + str(self.data['level']) if base.NEED_MORE_BIBLES == False: levelName = "gamelevel" + str(6) + "_" + str(1) #else: # levelName = "gamelevel" + str(self.data['chapter']) + "_" + str(self.data['level']) try: if self.data['difficulty'] != 3: if self.data['chapter'] == 3: if self.data['level'] == 3: levelName = "gamelevel" + str(3) + "_" + str(4) ## Load the new level from the dynamically loaded module currentLevel = initLevel(levelName) # save all the variables level and other scripts might have created in gameVariables for key, value in gameVariables.iteritems(): self.data[key] = value saveGame(self.data) except ImportError, err: # if we weren't looking for the first level of current major # number (x_0), then check for next major if self.data['level'] > 1: self.data['chapter'] += 1 self.data['level'] = 1 else: raise base.ResourceException("Error loading level " + levelName + "!") if base.SOUND == True: if self.data["chapter"] == 5: if self.playing != "MountainMusic": for item in music.Music: music.Stop(item) music.Play("MountainMusic") self.playing = "MountainMusic" if self.data["chapter"] == 4: if self.playing != "TempleMusic": for item in music.Music: music.Stop(item) music.Play("TempleMusic") self.playing = "TempleMusic" if self.data["chapter"] == 2: if self.playing != "CaveMusic": for item in music.Music: music.Stop(item) music.Play("CaveMusic") self.playing = "CaveMusic" if (((self.data["chapter"] == 1) or (self.data["chapter"] == 3) or (self.data["chapter"] == 6))): if self.playing != "JungleMusic": for item in music.Music: music.Stop(item) music.Play("JungleMusic") self.playing = "JungleMusic" chapter = int(self.data['chapter']) print "Loaded level", levelName if len(base.testLevelname) > 0: currentLevel.levelFileName = base.testLevelname # give the level access to the gameVariables currentLevel.pvars = gameVariables # run until user quits endState = currentLevel.run() if (endState == base.NEXTLEVEL): self.data['bibles'] = base.num_bibles self.data['bananas'] = base.numBananas self.data['level'] += 1 currentLevel = None base.testLevelname = "" if base.num_bibles >= base.NEEDED_BIBLES: base.NEED_MORE_BIBLES = False for key, value in gameVariables.iteritems(): self.data[key] = value saveGame(self.data) if base.NEED_MORE_BIBLES == True: base.DATA["chapter"] = randrange(1, 5) base.DATA["level"] = randrange(1, 2) self.data['bibles'] = base.num_bibles self.data['bananas'] = base.numBananas #self.data['level'] += 1 #currentLevel = None base.testLevelname = "" elif endState == base.GAMEOVER: base.num_bibles = int(self.data['bibles']) base.numBananas = int(self.data['bananas']) if base.testLevelname == "": currentLevel = initLevel(levelName) else: # special case for restarting the testlevel which uses custom level class currentLevel = initCustomLevel("testlevel") elif (endState == base.RESTARTLEVEL): if base.testLevelname == "": currentLevel = initLevel(levelName) else: # special case for restarting the testlevel which uses custom level class currentLevel = initCustomLevel("testlevel") elif endState == base.QUITGAME: # todo, game over screen? running = False elif endState == base.GOTOLEVEL: levelName = currentLevel.gotoLevelName currentLevel = initLevel(levelName) # need to keep track on these self.data['chapter'] = currentLevel.level_maj self.data['level'] = currentLevel.level_min base.testLevelname = "" elif endState == base.GOTOTESTLEVEL: if base.testLevelname == "": Exception("testLevelname not set") # user the testlevel.py level script levelName = "testlevel" currentLevel = initCustomLevel(base.testLevelname) # need to keep track on these self.data['chapter'] = currentLevel.level_maj self.data['level'] = currentLevel.level_min else: raise Exception("Unknown level end state", endState) elif endState == base.SHOWGAMEOVER: return GameOver(self.main)