Advertisement
pleabargain

python3 choose your own adventure dragon's cave

Jun 2nd, 2013
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.24 KB | None | 0 0
  1. '''
  2. Got this script from http://inventwithpython.com/blog/2010/01/14/new-choose-your-own-adventure-game/
  3.  
  4. I've made some notes and typo fixes
  5.  
  6. This text game choose your own adventure is written in python 3
  7. '''
  8. #the random is used throughout the script to randomize the game behavior
  9. import random
  10.  
  11. #this function is called many times throughout the script and handles user interaction
  12. def choosePath(numberOfPaths):
  13.     choice = 0
  14.     while choice < 1 or choice > numberOfPaths:
  15.         print('1 to ' + str(numberOfPaths) + '> ', end='')
  16.         choice = input()
  17.         if choice != '1' and choice != '2' and choice != '3' and choice != '4' and choice != '5':
  18.             choice = 0
  19.         if choice == '1' or choice == '2' or choice == '3' or choice == '4' or choice == '5':
  20.             choice = int(choice)
  21.     print()
  22.     return choice
  23.  
  24. #makes the user press enter and probably read the text before going on
  25. def pause():
  26.     print('Press enter to continue.')
  27.     input()
  28.  
  29. def intro():
  30.     print('You have ventured to the realm of dragons, and have finally arrived at the Dragon Cave.')
  31.     print('With your trusty sword at your side and trusty backpack on your back, you look upon the fearsome entrance to the cave.')
  32.     print()
  33.     print('Read carefully. The adventure changes each time you play.')
  34.     print('Treasure or certain death await!')
  35.     print()
  36.     pause()
  37.     front()
  38.  
  39. def front():
  40.     print('You are standing in front of the entrance of the cave.')
  41.     if skulls == 'present':
  42.         print('There are a bunch of human skulls and bones lying around.')
  43.     if skulls == 'absent':
  44.         print('There is a sign here that reads, "Welcome, visitors!"')
  45.     print()
  46.     print('What will you do?')
  47.     print('  1 Go into the cave.')
  48.     print('  2 Climb on top of the cave.')
  49.     path = choosePath(2)
  50.     if path == 1:
  51.         insideOfCave()
  52.     if path == 2:
  53.         topOfCave()
  54.  
  55. def topOfCave():
  56.     print('After climbing up, you are standing on top of the cave.')
  57.     print('There is a small hole that seems to be a chimney nearby.')
  58.     if dragonLocation == 'upper':
  59.         print('There is a lot of smoke coming out of the hole.')
  60.     print()
  61.     print('What will you do?')
  62.     print('  1 Climb down the hole.')
  63.     print('  2 Climb back down to the front entrance.')
  64.     path = choosePath(2)
  65.     if path == 1:
  66.         goDownChimney()
  67.     if path == 2:
  68.         front()
  69.  
  70. def insideOfCave():
  71.     print('You are inside front chamber of the cave. You can see the exit of the cave which would take you outside.')
  72.     print('There are two paths that lead deeper into the cave, one that goes up and the other that goes down.')
  73.     print()
  74.     print('What will you do?')
  75.     print('  1 Go outside of the cave.')
  76.     print('  2 Take the upper path deeper into the cave.')
  77.     print('  3 Take the lower path deeper into the cave.')
  78.     path = choosePath(3)
  79.     if path == 1:
  80.         front()
  81.     if path == 2:
  82.         upperArea()
  83.     if path == 3:
  84.         lowerArea()
  85.  
  86. def goDownChimney():
  87.     #the dragon's location is determined in the while loop below
  88.     if dragonLocation == 'upper':
  89.         print('You climb down into the smokey chimney. The smoke is really thick!... (press enter)')
  90.         #this is the pause function. input() is what calls it        
  91.         input()
  92.         print('The smoke keeps getting thicker. It is getting hard to breathe... (press enter)')
  93.         input()
  94.         print('You have become lost in the smoke, and start coughing. You cannot get any air!... (press enter)')
  95.         input()
  96.         print('ACK! You have suffocated to death! It was kind of dumb to climb into that smokey chimney.')
  97.         return
  98.     if dragonLocation == 'lower':
  99.         print('You climb down the chimney. It seems to lead you somewhere inside the cave.')
  100.         upperArea()
  101.  
  102. def upperArea():
  103.     print('You are standing in a chamber near the top of the cave. There is a rope ladder to a chimney hole in the ceiling that leads outside.')
  104.     if dragonLocation == 'upper':
  105.         print('The dragon is standing in the chamber! Smoke comes out of his nostrils and floats up and out the hole in the ceiling.')
  106.         pause()
  107.         faceDragon()
  108.     if dragonLocation == 'lower':
  109.         print()
  110.         print('What will you do?')
  111.         print('  1 Climb up and out of the hole.')
  112.         print('  2 Go down to the front of the cave.')
  113.         path = choosePath(2)
  114.         if path == 1:
  115.             topOfCave()
  116.         if path == 2:
  117.             insideOfCave()
  118.  
  119.  
  120. def lowerArea():
  121.     print('You are in the lower chamber of the cave. There is a path leading back to the front of the cave.')
  122.     print('This chamber is very large, and there is an underground lake in the chamber. The water is dark and')
  123.     print('murky. You cannot tell how deep it is or what may be in it. In the middle of the lake, you can barely')
  124.     print('see a small island and something on it that is shiny.')
  125.     print()
  126.     if rock == 'present':
  127.         print('There is a large rock near the shore.')
  128.         print('There is a creaky old boat on the shore.')
  129.     if rock == 'sunk':
  130.         print('There is a submerged boat at the bottom of the water near the shore.')
  131.     if rock == 'holding':
  132.         print('There is a creaky old boat on the shore.')
  133.     if rock == 'boat':
  134.         print('There is a creaky old boat on the shore with a large rock in it.')
  135.     print()
  136.     print('What will you do?')
  137.     print('  1 Go to back to the front of the cave.')
  138.     print('  2 Swim the lake to the island.')
  139.     print('  3 Take the creaky old boat to the island.')
  140.     if rock == 'present':
  141.         print('  4 Pick up the large rock.')
  142.     if rock == 'holding':
  143.         print('  4 Put the large rock in the boat.')
  144.     print()
  145.     path = choosePath(4)
  146.     if path == 1:
  147.         front()
  148.         return
  149.     if path == 2:
  150.         swim()
  151.         return
  152.     if path == 3 and rock == 'sunk':
  153.         print('That boat does not look like it is going anywhere.')
  154.         print()
  155.         pause()
  156.         lowerArea()
  157.         return
  158.     if path == 3 and rock != 'sunk':
  159.         rideBoat()
  160.         return
  161.     if path == 4 and rock == 'present':
  162.         takeRock()
  163.         return
  164.     if path == 4 and rock == 'holding':
  165.         putRockInBoat()
  166.         return
  167.  
  168. def takeRock():
  169.     global rock
  170.     print('You pick up the large rock and put it in your trusty backpack. Ooof! It sure is heavy.')
  171.     print()
  172.     pause()
  173.     rock = 'holding'
  174.     lowerArea()
  175.  
  176. def putRockInBoat():
  177.     global rock
  178.     print('You set the large rock down in the creaky old boat. The boat rocks from side to side, no pun intended.')
  179.     print()
  180.     pause()
  181.     if boat == 'sinks':
  182.         print('The creaky old boat begins to sink under the weight of the rock!')
  183.         print('Whew! It is a good thing you did not try to take that boat to the island.')
  184.         print()
  185.         rock = 'sunk'
  186.         pause()
  187.         lowerArea()
  188.         return
  189.     if boat == 'floats':
  190.         print('The creaky old boat seems to be holding up under the weight of the rock.')
  191.         print()
  192.         rock = 'boat'
  193.         pause()
  194.         lowerArea()
  195.         return
  196.  
  197. def swim():
  198.     print('You dive into the water. Yipes! It is icy cold!')
  199.     pause()
  200.     if rock == 'holding':
  201.         print('You begin to swim to the island, but the heavy rock you are holding begins to drag you down!... (press enter)')
  202.         input()
  203.         print('ACK! You cannot get the straps off of your backpack!... (press enter)')
  204.         input()
  205.         print('You begin to drown in the murky, dark waters of the underground lake... (press enter)')
  206.         input()
  207.         print('You have drowned! It was kind of dumb to swim the lake with that heavy rock in your backpack.')
  208.         pause()
  209.         return
  210.     if rock != 'holding':
  211.         print('You begin to swim to the island. The island is further away than you thought... (press enter)')
  212.         input()
  213.         print('You are getting tired from all that swiming. You are not sure you can make it... (press enter)')
  214.         input()
  215.         print('You finally get to the shore of the island. Whew! You do not think it is a good idea to try that swim again!')
  216.         pause()
  217.         island()
  218.  
  219. def rideBoat():
  220.     print('You get into the creaky old boat and begin to row towards the island... (press enter)')
  221.     input()
  222.     print('Half way to the island, you notice that the boat has a small leak!... (press enter)')
  223.     input()
  224.     print('You row faster and faster as the boat slowly begins to sink.... (press enter)')
  225.     input()
  226.  
  227.     if boat == 'sinks':
  228.         print('The boat is sinking, and your shoe is stuck to some gum on the bottom of the boat!... (press enter)')
  229.         input()
  230.         print('You try to get your shoe off, but not before your head sinks under the murky water... (press enter)')
  231.         input()
  232.         print('What a dumb way to die. Your adventuring skills were no match for the gum. You have died.')
  233.         pause()
  234.         return
  235.     if boat == 'floats':
  236.         print('The boat sinks, but it floated long enough for you to get close to the shore of the island. After a short swim in the icey waters, you climb onto the island.')
  237.         pause()
  238.         island()
  239.         return
  240.  
  241.  
  242. def island():
  243.     print('You are standing on the island in the middle of the underground lake. The shiny glint comes from a large pile of gold and jewels!')
  244.     print('You see there is a rope ladder from the ceiling above the island.')
  245.     pause()
  246.     if dragonLocation == 'upper':
  247.         print('You start putting as much treasure as your backpack can hold.')
  248.         print('You climb the rope ladder, and you see that it leads to a hole in the ceiling that goes outside of the cave.')
  249.         print()
  250.         print('You have escaped from the Dragon Cave with the treasure! Congratulations! You have won the game!')
  251.         pause()
  252.         return
  253.     if dragonLocation == 'lower':
  254.         print('But in front of the treasure is the dragon!')
  255.         pause()
  256.         faceDragon()
  257.         return
  258.  
  259. def faceDragon():
  260.     print()
  261.     print('The dragon is huge! He stands ten feet tall, and has tough green scales and giant claws.')
  262.     print('His leathery wings are folded back over his massive body, and he smiles at you with giant teeth as whisps of smoke come out of his nostrils.')
  263.     pause()
  264.     print('Your trusty sword is at your side, but the dragon is so scary you think you might soil your trusty pants.')
  265.     print()
  266.     print('The dragon smiles at you, and then speaks...')
  267.     pause()
  268.     print()
  269.     print('"Welcome adventurer. You have come a long way to my cave. Seeking treasure and fortune, are you? Well, I have plenty of it myself."')
  270.     pause()
  271.     print()
  272.     print('"All this gold and jewels is cluttering up my place, and I would like to get rid of it. Why not trade me that fancy sword for as much treasure as you can put into your backpack?"')
  273.     print('"You can climb this rope ladder to exit the cave. You can trust me. I do not want to eat you. I am a friendly vegetarian dragon. What do you say?"')
  274.     print()
  275.     print('What will you do?')
  276.     print('  1 Trust the dragon, and give him your sword in exchange for treasure.')
  277.     print('  2 Attack the dragon with your trusty sword.')
  278.     path = choosePath(2)
  279.     if path == 1:
  280.         print()
  281.         print('You hand over your trusty sword to the dragon. With all that treasure, you could buy a hundred trusty swords!... (press enter)')
  282.         #why input here?
  283.         input()
  284.         print('The dragon takes the sword and places it on a rack high on the wall, out of your reach... (press enter)')
  285.         input()
  286.         print('He turns to you and smiles. This dragon sure does smile a lot... (press enter)')
  287.         input()
  288.         print('The dragon begins to open his mouth... (press enter)')
  289.         input()
  290.         print()
  291.         if skulls == 'present':
  292.             print("'Giant waves of flame come from the dragon's mouth! You are incinerated instantly!'")
  293.             print()
  294.             print('The dragon pours barbecue sauce on your well-done corpse. Guess he was\'nt a vegetarian after all.')
  295.             print('After gnawing the meat from your skeleton, he tosses your skull and bones outside the front of his cave.')
  296.             print()
  297.             print('You have died.')
  298.             pause()
  299.             return
  300.         if skulls == 'absent':
  301.             print('"Thanks a lot!", the dragon says. "I have started a trusty sword collecting hobby, and yours is the first!"')
  302.             print('"Take as much treasure as you want!"')
  303.             print()
  304.             print('You stuff as much gold and jewels as your trusty backpack can carry, and climb the rope ladder up to a hole in the ceiling that leads outside.')
  305.             print('The dragon waves goodbye as you leave. You have survived the Dragon Cave with enough treasure to retire. Congratulations!')
  306.             print()
  307.             pause()
  308.             return
  309.     if path == 2:
  310.         print()
  311.         print('You are no fool. You pretend to hand over your sword... (press enter)')
  312.         input()
  313.         print('But at the last minute, you swing it at the dragon\'s neck!... (press enter)')
  314.         input()
  315.         if skulls == 'present':
  316.             print('In one quick swipe, you cut off the dragon\'s head!')
  317.             pause()
  318.             print()
  319.             print('With the dragon slayed, you stuff as much gold and jewels into your trusty backpack as it can hold.')
  320.             print('The rope ladder leads up to a hole in the ceiling that goes outside of the cave.')
  321.             print('You have survived the Dragon Cave with enough treasure to retire. Congratulations!')
  322.             print()
  323.             pause()
  324.             return
  325.         if skulls == 'absent':
  326.             print('But the dragon dodges your blade!')
  327.             pause()
  328.             print('"Fiend!", the dragon shouts. He is really angry at you, and breathes a wave of flame onto you.')
  329.             print('You are incinerated instantly!')
  330.             pause()
  331.             print('The dragon pours barbecue sauce on your well-done corpse, and then wraps you in foil and puts you in his refrigerator.')
  332.             print('It is a little known fact that dragons also have refrigerators in their caves, along with gold and jewels.')
  333.             pause()
  334.             print('The dragon doesn\'t eat you, because he is a vegetarian after all. But he will bring you to the next dragon pot luck dinner.')
  335.             pause()
  336.             print()
  337.             print('You have died.')
  338.             return
  339.  
  340. #This creates some of the randomness in the game
  341. while True:
  342.     # Randomly generate the game variables
  343.     if random.randint(1,2) == 1:
  344.         dragonLocation = 'upper'
  345.     else:
  346.         dragonLocation = 'lower'
  347.  
  348.     if random.randint(1,2) == 1:
  349.         skulls = 'present'
  350.     else:
  351.         skulls = 'absent'
  352.  
  353.     if random.randint(1,2) == 1:
  354.         boat = 'sinks'
  355.     else:
  356.         boat = 'floats'
  357. #I'm not sure what this does but
  358. #rock is a global, I'm not sure what that means... see line 157
  359.     rock = 'present'
  360.  
  361.     # Start the game
  362.     intro()
  363.  
  364.     print()
  365.     print('Would you like to play again? Y/N')
  366.     playAgain = input()
  367.     if playAgain == 'Y' or playAgain == 'y':
  368.         continue
  369.     if playAgain == 'N' or playAgain == 'n':
  370.         break
  371.     break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement