I'm trying to complete the finishing touches on an assignment that uses python by adding a save/load function (that has to use files unfortunately). I have four function defined and given a shot at writing the code for each, but I've come up with nothing as it doesn't want to work and our instructors instructions for files are extremely basic.
def savegame():
f=open("savefile.txt","w")
f.write(playeroneright/playeroneleft/playertworight/playertwoleft/playertworight/computerright/computerleft)
f.close()
def opengame():
f=open("savefile.txt")
lines=f.readlines()
playeroneright=lines[0]
playeroneleft=lines[1]
playertworight=lines[2]
playertwoleft=lines[3]
computerright=lines[4]
computerright=lines[5]
f.close()
def newgame():
deletecontent("savefile.txt")
f=open("savefile.txt","w")
f.write(1)
f.write(1)
f.write(1)
f.write(1)
f.write(1)
f.write(1)
f.close()
opengame()
def deletecontent(pfile):
fn=pfile.name
pfile.close()
The first function (savegame) should take each variable and save the numerical value of it on a different line as it currently holds in the game (the game manipulates the variables, playeroneright and so one). It should save it in the file savefile.txt.
The second function (opengame) should take whatever is currently saved in the file and assign that file to the variable, basically a load function.
The third function (newgame) should reset the game to the starting point (where all variables take on the value 1), then open the game up using the above function.
The last function (deletecontent) is just used in the third function to clear the document so that it can assign all new values.
These are my attempts thus far, and I'm not very good at files yet but I am trying to figure them out a bit more. If anyone has any ideas (preferably simple, if that's possible), that would be awesome. Thank you!