1

I am having trouble with python. I am making a text-based adventure game, and I am trying to make a restart function when you die. I am trying to count deaths by doing deaths = deaths + 1 whenever you die, but the only way to restart is to re-run the entire script, which resets the death counter. Does anyone know how to restart a script without re-running the program?

  • Could you persist the data out (akin to bones in nethack) and then let them rerun it, pulling in the updated data? – user40980 Jul 18 '13 at 18:13
  • 1
    keep the death counter in the global scope, and put the game script in a procedure. Have a while loop that calls the game script procedure, and then wait for the game to end (either in a death or in a victory, and then handle the result (death: increment deaths, check if player wants to restart -- Victory: display a victory screen and ask to restart) – Ben313 Jul 18 '13 at 18:21
  • Rerun the script, but make the death count an argument it parses. If not set it defaults to 0, from then on when you restart it give it the new deathcount – Jimmy Hoffa Jul 18 '13 at 18:27
  • 1
    @Ben313 Thank you so much for your help. My script is running perfectly. – Josh Gordon Jul 18 '13 at 19:19
1

You can call your script from another script, and when you "restart", use the reload built-in method.

reload(mymodule)

This isn't truly ideal though for a game. What you're instead looking for is a level loader, with a main loop which handles state.

Does that make any sense? Its a fun challenge building your own level loader, and might be more effort than you want to put in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.