This also saves the players current location which is a reference to an instance of a class. Not sure if this is needed, but this is the code for the location file:
class Locations: def __init__ (self, x, y, north, south, east, west): self.x = x self.y = y self.n = north self.s = south self.e = east self.w = west #Initialise the locations a1_1 = Locations(1,1,0,0,1,0) a1_3 = Locations(1,3,0,0,1,0) a2_1 = Locations(2,1,0,1,1,1) a2_2 = Locations(2,2,1,1,0,0) a2_3 = Locations(2,3,1,0,1,1) a3_1 = Locations(3,1,0,0,0,1) a3_3 = Locations(3,3,0,0,0,1) #Set the directional variables to point to the location a player #would be in if they moved in that direction a1_1.e = a2_1 a1_3.e = a2_3 a2_1.s = a2_2 a2_1.e = a3_1 a2_1.w = a1_1 a2_2.n = a2_1 a2_2.s = a2_3 a2_3.n = a2_2 a2_3.e = a3_3 a2_3.w = a1_3 a3_1.w = a2_1 a3_3.w = a2_3
The method I use to save and then to load is below:
save = raw_input("Do you want to save?") if save == "y": SaveString = str(Player1.n) + "\n" + str(Player1.l) + "\n" + str(Player1.hp) + "\n" + \ str(Player1.a) + "\n" + str(Player1.d) + "\n" + str(Player1.xp) + "\n" + \ str(Player1.maxhp) + "\n" + str(Player1.heal) + "\n" + \ str(Player1.dmin) + "\n" + str(Player1.dmax) + "\n" + str(Player1.g) SaveGame = open("save.txt",'w') SaveGame.write(SaveString) SaveGame.close()
#Initialise the player with default stats Player1 = Player('Default',1,30,30,10,2,2,0,1,3,100) #The bit that loads a previously saved game - at the moment there can only be one saved game at a time LoadGame = raw_input("Do you want to continue on a previously saved adventure?") if LoadGame == "y": z = open('save.txt','r') x = z.readline() Player1.n = x[:-1] Player1.l = int(z.readline()) Player1.hp = int(z.readline()) Player1.maxhp = int(z.readline()) Player1.heal = int(z.readline()) Player1.a = int(z.readline()) Player1.d = int(z.readline()) Player1.xp = int(z.readline()) Player1.dmin = int(z.readline()) Player1.dmax = int(z.readline()) Player1.g = int(z.readline()) CurrentPosition print "\nWelcome back " + Player1.n + ", good to see you again."
There is nothing currently to save the location as my problem is that when I load the info back into the variables it is loaded as a string and that won't point to the class instance I need it to.
I hope that makes sense?
What I need to be able to do is convert the string into something that points to the correct instance of the class Locations.
Can anyone help?
(Just incase it is needed I have zipped up the complete set of files I use to run the game.)
Number of downloads: 115