Skip to content

Instantly share code, notes, and snippets.

@Torxed
Last active August 29, 2015 14:03
Show Gist options
  • Save Torxed/259dc0a2765264fefd00 to your computer and use it in GitHub Desktop.
Save Torxed/259dc0a2765264fefd00 to your computer and use it in GitHub Desktop.
import pickle
class Game(object):
def __init__(self):
self.experience = 0
self.health = 0
self.loadedFromDisk = False
def give_xp(self, given_xp):
self.experience += given_xp
def save_variables(self):
with open('game.dump', 'wb') as fh:
pickle.dump({'xp' : self.experience, 'hp' : self.health}, fh)
def load_variables(self):
if not self.loadedFromDisk:
with open('game.dump', 'rb') as fh:
data = pickle.load(fh)
self.experience = data['xp']
self.hp = data['hp']
self.loadedFromDisk = True
gamehandle = Game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment