I'm writing a game in Python and have come to the point where I need a way of saving the players progress. I was thinking of having the script create a text file and write a couple of lines (for storing stat variables, and the room number) when the player exits the game. But I also need it to check if that file exits on startup and, if so, apply the stored values to the corresponding variables. Can anybody help?
解决方案
Two different issues here:
What to store in the file and how to store it
The logic of when to store and when to load
The first issue is simpler to address as it's more generic. You have several good options here: one is using Python's ConfigParser class for Windows .ini - like configuration files. Alternatively you can use pickle to simply dump some sort of a configuration/settings data structure (may be a nested dict). Then there's the built-in SQLite binding. There are other options - it all depends on the level of complexity you want.
The second issue is more specific to your application. You can try to open the config file on startup and if it's there, read its contents. Later, you can periodically store the settings/progress into it. A word of advice: keep the complete set of settings in a consistent data structure at all times - even if only part of the settings are in the config file when you're reading it (for some reason), have default values for your settings.