Created
November 30, 2022 07:33
-
-
Save Pinacolada64/46162f97f2c8bbe47b604a32a524ecb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# help for https://www.reddit.com/r/learnpython/comments/z86k01/im_trying_to_make_a_chose_your_own_adventure_game/ | |
# game rewritten to use functions for areas, and a simple state machine: | |
def ask_to_play(): | |
while True: | |
answer = input("would you like to play (yes/no) ") | |
if answer.lower().strip() == "yes": | |
state = "forest path" | |
# exit while loop | |
return state | |
if answer == "no": | |
print("boring") | |
# quit game: | |
state = "game lost" | |
# exit while loop | |
return state | |
print("invalid answer") | |
def forest_path(): | |
while True: | |
answer = input("you reach a forrest with two paths, go left or right? ").lower().strip() | |
if answer == "left": | |
state = "goblin" | |
return state | |
elif answer == "right": | |
print("you get lost and fall off a ledge and die") | |
# game over | |
state = "game lost" | |
return state | |
print("invalid choice") | |
def goblin(): | |
while True: | |
answer = input("you found a goblin, would you like to fight? (yes/no) ").lower().strip() | |
if answer == "no": | |
print("smart choice, you escaped") | |
state = "hurt man" | |
return state | |
if answer == "yes": | |
state = "hurt man" | |
return state | |
print("Invalid choice") | |
def hurt_man(): | |
# part 1 | |
while True: | |
answer = input("you find a man who is hurt, will you help? (yes/no) ") | |
if answer == "yes": | |
print("he joins you on your journey") | |
# next part | |
break | |
elif answer == "no": | |
state = "alone" | |
return state | |
print("Invalid choice") | |
# part 2 | |
while True: | |
answer = input("he asks if he can join you? (yes/no) ") | |
if answer == "yes": | |
print("he joins you on your journey") | |
state = "rope" | |
return state | |
elif answer == "no": | |
state = "alone" | |
return state | |
def alone(): | |
while True: | |
answer = input("you continue to walk alone, you find a cave do you enter? (yes/no) ") | |
if answer == "yes": | |
state = "lost" | |
return state | |
if answer == "no": | |
state = "lost" | |
return state | |
print("Invalid choice") | |
def lost(): | |
while True: | |
answer = input("you get lost, Yell for help? (yes/no) ") | |
if answer == "yes": | |
print("but no one came") | |
state = "game lost" | |
# quit game | |
return state | |
elif answer == "no": | |
print("you start to starve, eat your own leg, and bleed out, and die") | |
# quit game | |
state = "game lost" | |
return state | |
print("invalid choice") | |
def rope(): | |
while True: | |
answer = input("you find a cliff with some rope, do you, (climb down " | |
"together/one at a time/or not at all)? ") | |
if answer == "together": | |
print("the rope breaks and you both fall to your deaths") | |
# game lost: | |
state = "game lost" | |
return state | |
elif answer == "one at a time": | |
print("you both make it down safely") | |
state = "cave" | |
# exit loop: | |
return state | |
elif answer == "not at all": | |
print("you continue to walk together") | |
state = "cave" | |
# exit loop: | |
return state | |
print("invalid choice") | |
def cave(): | |
answer = input("you continue to walk alone, you find a cave do you enter? (yes/no) ") | |
while True: | |
if answer == "yes": | |
state = "yell" | |
return state | |
elif answer == "no": | |
state = "campfire" | |
return state | |
print("invalid choice") | |
def yell(): | |
while True: | |
answer = input("you get lost, Yell for help? (yes/no) ") | |
if answer == "yes": | |
print("but no one came") | |
# quit game | |
state = "game lost" | |
return state | |
elif answer == "no": | |
print("you start to starve, eat your own leg, and bleed out, and die") | |
# quit game | |
state = "game lost" | |
return state | |
print("invalid choice") | |
def campfire(): | |
while True: | |
answer = input("you continue to walk and see a campfire, sit down? (yes/no) ") | |
if answer == "yes": | |
print("you fall asleep and wake up at home (good ending #1") | |
# (({{i want it to reset when it gets to here}})) | |
state = "game won" | |
return state | |
if answer == "no": | |
print("FIXME: finish this") | |
state = "game lost" | |
return state | |
print("invalid choice") | |
def game_lost(): | |
print("Game over. You lost.") | |
def game_won(): | |
print("Game over. You won.") | |
""" | |
to progress: | |
yes | |
left | |
no | |
yes | |
one at a time | |
""" | |
print("Welcome to the game.") | |
playing = True | |
# global state | |
state = "ask to play" | |
while playing is True: | |
print(f'Main loop: {state=}') | |
# check state of game: | |
if state == "ask to play": | |
# get returned state: | |
state = ask_to_play() | |
if state == "forest path": | |
state = forest_path() | |
if state == "goblin": | |
state = goblin() | |
if state == "hurt man": | |
state = hurt_man() | |
if state == "rope": | |
state = rope() | |
if state == "cave": | |
state = cave() | |
if state == "yell": | |
state = yell() | |
if state == "alone": | |
state = alone() | |
if state == "lost": | |
state = lost() | |
if state == "campfire": | |
state = campfire() | |
if state == "game lost": | |
game_lost() | |
# set playing to False which ends the program: | |
playing = False | |
if state == "game won": | |
game_won() | |
playing = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment