Log in

View Full Version : Making save files in Python 2.7?


Reina
October 12th, 2012, 04:49 AM
Warning: programmer thread.

I've been playing around with file relationships and Python 2.7, and as a little project decided to make a text-based game through Command Prompt that allows you to save your progress at certain checkpoints and pick up where you left off.

I've been doing that by writing in random keyphrases into a .txt file, then making my program read specific lines and check for matches. Does anyone here have a better method?

Here's a small snippet from my code for you guys to get a better feel for it: (Oh dear, there's no support for indentation here... my apologies for the jumbled up format.)

print "What would you like to do?"
print "[NewGame/Continue]"
action = raw_input("> ")
if action == "NewGame":
NewAccount(" ")
elif action == "Continue":
Continue(" ")
yn = raw_input("> ")
if yn == "Y":
f = open('C:/Users/Me/Desktop/filewrite.txt', 'r')
for count in range(1,4):
f.readline()
time.sleep(1)
print "..."
time.sleep(1)
print "File loaded. Please type",
print f.readline(),
print "to load save."
savefile = raw_input("> ")
if savefile == "asdf123":
room1(" ")
elif savefile == "13412341":
room2(" ")
elif savefile == "okjlflwoi":
room3(" ")
elif yn == "N":
print "Would you like to make a new account?"
print "[Y/N]"
yn2 = raw_input("> ")
if yn2 == "Y":
NewAccount(" ")
elif yn2 == "N":
Continue(" ")

I know, I'm sort of a noob to Python, and I probably did a lot wrong. I'd love some feedback nonetheless! ^.^

TheMatrix
October 12th, 2012, 08:03 PM
Hmm, well, it's not the best way of doing things. I suggest doing more simple things for now, until you can get the Python basics down.

When you do, you may want to consider something like SQLite or a DBM file type of system. You can also use a serializer, save a data structure consisting of game data to that, and then read that on the next program load.

Reina
October 13th, 2012, 01:29 AM
Hmm, well, it's not the best way of doing things. I suggest doing more simple things for now, until you can get the Python basics down.

When you do, you may want to consider something like SQLite or a DBM file type of system. You can also use a serializer, save a data structure consisting of game data to that, and then read that on the next program load.

I was trying to practice interactions between files, but I agree that for this specific project that would be best. I'll look into that.

Also, thank you so much for fixing that disgusting formatting. I'm new to VT and wasn't sure if there was a way to format code.