r/learnpython 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

4 comments sorted by

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":

  • the valid next "states"
  • the flavor text for that choice
  • whether or not that "state" ends the game.

1

u/PureWasian 18h ago edited 17h ago

No clue on porting to DS, but not sure if that's a Python relevant question. If you end up having to use a different language, the same general process applies (static data is maintained separate from the runtime logic)

1

u/Char_TeamEmber 8h ago edited 8h ago

I thank you so much for your feedback. I really do appreciate it. However I’m getting lost with the dictionary mapping and everything after that. Do you mean I just have it check values (like location) and base everything off of that? Could you perhaps explain it simply, as if I am a child?

(Sorry I’m so out of the loop šŸ˜…)

EDIT: it might be good if I can see what things are separated or grouped together, I’m a visual learner. What things are in what functions, etc.

1

u/PureWasian 7h ago edited 7h ago

You're good! More or less that's the idea, yes.

A dictionary basically takes a "key" and uses that to map it to a "value." A basic example: inventory = { "emeralds": 10 "diamonds": 64 "iron_ingots": 24 } This lets us easily keep track of how many of each resource I have, as well as add/update/remove stuff: print(inventory["diamonds"]) # 64 inventory["copper"] = 32 inventory["emeralds"] = 50 del inventory["iron_ingots"]

For your purposes, your game is basically a State Transition Graph. Just like the visuals you see in Detroit: Bevome Human or Road To Empress 2.

At its very barebones, imagine you have this: A → B ↓ C

We can represent this as: mapping = { "A": ["B", "C"] }

Building on that same idea, we can represent this: A → B → E ↓ ↓ C → D as mapping = { "A": ["B", "C"] "B": ["D", "E"] "C": ["D"] }

If you frame your game like this where each letter is each one of your scenarios (location+action), it's easier to make a much more complex "web" of connections to each "state" in your game instead of doing it as a tangle of hard-coded, nested if statements.