I am in the process of making a text based game with Python, and I have the general idea down. But I am going to make the game in depth to the point where, it will take longer than one sitting to finish it. So I want to be able to make the game to where, on exit, it will save a list of variables (player health, gold, room place, etc) to a file. Then if the player wants to load the file, they go to the load menu, and it will load the file.
I am currently using version 2.7.5 of Python, and am on Windows.
I think you can just use a txt file to record the things you need in the game,just use file() and open() function.Or you can use the sqlite3 module in python to save your record. just try :
import sqlite3
holp that helps. :)
here is an example to use sqlite3 in python:
just change as you want to save to the file:
Or I think, If your game is not very Complex, Just use file() function.
To add to Bogdan's answer, an easier to understand way to just be to store a dictionary with all your data. So if you have this:
you would just do:
and it should load.
If I understand the question correctly, you are asking about a way to serialize objects. The easiest way is to use the standard module pickle:
Standard Python objects and simple classes with any level of nesting can be stored this way. If your classes have some nontrivial constructors it may be necessary to hint
pickle
at what actually needs saving by using the corresponding protocol.I have been working on something like this. It uses JSON for ease of use and scalablity. here it is:
this is the file save system i use in all my projects. i hope it helps
First, don't overthink this. You don't need to use anything complicated. As a preliminary step, research basic file input/output in python.
Second, I'm assuming you have a player class in your game? Or possibly an overall class which keeps track of game state. Well have that class store default values for your variables like health, gold etc. Then have a method which can modify this class like
def load_stats(player):
ordef load_stats(game):
something. and have it read in from a save file which can have any format you like and modify the variables of your player/game state.First test loading of game files and make sure you can get it so that your player class gets modified.
Then all you have to do is add a save game feature that lets you output these variables back to a file in your directory system somewhere.
Try doing this and let me know if you need any help afterwards.