r/learnpython • u/Char_TeamEmber • 20h ago
Looking for help or a resolution
so I wrote a python program years ago in school, and I was wondering if anyone on here could take a look at it for me. I'd like to improve it so that it brings you back to the start or the previous choice with an option to return to the start, but I don't remember anything because it's been years since I touched python. Feel free to play the game and get a feel for it.
Anyone have a fix or advice to fix it? I'll link the code here so that y'all can try it out yourselves (eventually I want to port this game to Nintendo DS).
0
Upvotes
1
u/PureWasian 18h ago edited 17h ago
Congrats, you made a decision tree text RPG :)
A lot of your code boils down to: ``` location = None gameover = False
while True: if location == None: user_input = input("prompt1: ") if user_input = "option_1": user_input = input("prompt2: ") location = "somewhere" print("flavor text") elif location == ...: ... ... ```
Problem: You do a lot of nested conditional statements and it's hard to keep track of.
Solution: You can use a dictionary mapping to make managing it more easy. It's a useful way to treat it more directly like a "state transition graph" where each location or action can "point" to each of the next possible states/phases of the game (or end on the gameover message)
Something like:
mapping = { "start": ["lake", "ravine"] "lake": [ "inspect_bridge", "jump_waterfall", "inspect_waterfall" ] "inspect_bridge": ["game_over"] ... }then you can add "going back" transitions onto the list, like adding from "lake" an option to go back to "start". Defining them outright also allows you print out the options explicitly for the player (if you wanted to), like: ``` mapping = {<see above>}
example current conditions
current_phase = "lake" options = mapping["lake"] user_input = None
loop until valid option
while user_input not in options: user_input = input(f"do: {options}") if user_input not in options: print("come again?")
go to the next state/phase
current_phase = mapping[user_input] ```
in terms of printing out the right "flavor text" you can also keep a mapping of these mapped to each state at the top of your file:
flavor_text = { "start": "Your journey begins. Lake or...", "lake": "You go to the lake...", ... }and then handling these together is as easy as adding a line to the code from above: ``` mapping = {<see above>} flavor_text = {<see above>}example current conditions
current_phase = "lake" options = mapping["lake"] user_input = None
flavor text for this phase
print(flavor_text[current_phase])
loop until valid option
while user_input not in options: user_input = input(f"do: {options}") if user_input not in options: print("come again?")
current_phase = mapping[user_input] ```
You're basically just separating your game's "data" (the flavor text and locations) from your game's "logic" of actually running the program. You can even put all of the data into a separate file and load those in so the top of your file isn't a gigantic bundle of strings to look at all in the same file as your game's runtime logic.
Regardless, just throw it all in a while loop and check when to set gameover, and now you can much more easily add whatever transitions or additional concepts much more readily.
And if you want bonus points, you can implement a singular, nested dictionary that holds everything relevant for each game phase or "state":