python 3.x - text based adventure game, save game function -



python 3.x - text based adventure game, save game function -

i making text based adventure game. half way done , before much farther story line want implement save game function. i've been trying figure out afternoon can't seem find indication of how right, scenario anyways.

here decent portion of code, have more didn't paste here it's story pacing basically. want able have selection ie. 5 = save game saves players1 statistics , when loaded puts player in same spot in game /w senter code here

i can't code paste in here due length believe have pastebin link.

http://pastebin.com/gjpa2tee

any help much appreciated, i'm trying larn programming.

you seem have things define state : self.__player_health, self.__player_name, self.__health_potion, self.__big_wrench, self.__blue_keycard. need read more carefully, couldn't find state defines progression. defines state can save , read text file - if aiming @ simplicity.

one way go having dict construction store text, time wait , function (if available, fighting ), pages. using array store these pages, book. if have loop maintain reading pages - reading function. since story indexed, know are, save states , position.

unrelated : utilize import @ top instead of every time.

edit:

import sys, traceback import json userquited = false def _quit(dummy): global userquited userquited = true def _save(dummy): f=open("game.sav",'w+') json.dump(states, f) f.close def _continue(dummy): f=open("game.sav",'r+') states = json.load(f) f.close def user(name): states["player_name"] = name states = { "player_health" : 100, "player_name" : "", "health_potion" : 0, "big_wrench" : 0, "blue_keycard" : 0, "current_page" : "page1" } book = { "page1": { "text": """you in underwater research facility has been attacked unknown assailants. assistant head researcher, hired in assist dr. weathers lightning sword project. project lightning sword still remains in big mystery you. although worked closely doc never allowed see big picture. right that's to the lowest degree of worries, chaos ensues around you. alarms triggered while ago. sit down in room, hear short bursts of automatic rifle fire , 1 thing certain, have survive.\n """, "inputtext": "what player named?", "function": user, "next": "page2" }, "page2": { "text": "welcome, %(player_name)s please take do", "inputtext": "0 = quit, 1 = start new game, 2 = continue", "choices": { "0" : "quit", "1" : "page3", "2" : "continue" } }, "page3" : { "text" : """you standing in room, door locked , you're wondering if out of alive. gunfire keeps getting closer , know can't remain here, have create move , seek , out. otherwise it's matter of time before find , alternative doesn't promising.\n\n ready seek escape first should seek , useful things room utilize along way. room-office bit bigger walk in closet, textile flooring , grayness walls encompas view , things in room bed sitting on (not comfortable, looks more prisoners bed), framed image on wall , work desk has barely plenty space equipment , computer.""", "inputtext": "1 = walk on image frame, 2 = walk on desk, 3 = exit door", "choices": { "1" : "page4", "2" : "page5", "3" : "page7" } }, "quit" : { "text": "goodbye!", "inputtext": "", "function" : _quit }, "continue" : { "text": "welcome back!", "inputtext": "", "function" : _continue } } def processpage(page): reply = "" print(page["text"] % states) if len(page["inputtext"]) > 1 : reply = raw_input(page["inputtext"] % states) if "function" in page: page["function"](answer) if "next" in page: homecoming page["next"] if "choices" in page: selection in page["choices"]: if selection == answer: print page["choices"][choice] homecoming page["choices"][choice] homecoming "" def main(): global userquited while(userquited==false): states["current_page"] = processpage(book[states["current_page"] ]) if (states["current_page"] != "quit" , states["current_page"] != "continue"): _save("") main()

so lot of code repeating... i'm trying maintain in pattern. thought here loop print , navigate in story. each iteration, current_page saved other states. using go on game loaded file.

the rest little text game engine made based on code pattern. modify , create on top of it.

python python-3.x

Comments

Popular posts from this blog

c# - ASP.NET MVC Sequence contains no matching element -

java - Parsing XML, skip certain tags -

rest - How to invalidate user session on inactivity in a stateless server? -