blazex224

Python ex43_game_solution.py with Python Syntax Highlighting

Apr 24th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.63 KB | None | 0 0
  1. from sys import exit
  2. from random import randint
  3. from textwrap import dedent
  4.  
  5. class Scene(object): # Creating a class "Scene" that is-a "object"
  6.  
  7.     def enter(self): # and has-a function "enter"
  8.         print("This scene is not yet configured")
  9.         print("Subclass it and implement enter()")
  10.         exit(1) # This exits the program
  11.     # This class can act as a "base class"
  12.  
  13.  
  14. class Engine(object): # Creating a class "Engine" that is-a "object"
  15.  
  16.     def __init__(self, scene_map):
  17.         self.scene_map = scene_map
  18.         # I got this part right with the "__init__" in the "Map" class with "self.start_scene = start_scene"
  19.  
  20.     def play(self):
  21.         current_scene = self.scene_map.opening_scene()
  22.         last_scene = self.scene_map.next_scene('finished')
  23.  
  24.         while current_scene != last_scene:
  25.             next_scene_name = current_scene.enter()
  26.             current_scene = self.scene_map.next_scene(next_scene_name)
  27.         # be sure to print out the last scene
  28.         current_scene.enter()
  29.  
  30.  
  31. class Death(Scene):
  32.  
  33.     quips = [
  34.     "You died. You kinda suck at this.",
  35.     "Loser",
  36.     "You lost",
  37.     "Stop losing"
  38.     ]
  39.  
  40.     def enter(self):
  41.         print(Death.quips[randint(0, len(self.quips)-1)])
  42.         exit(1)
  43.  
  44.  
  45. class CentralCorridor(Scene):
  46.  
  47.     def enter(self):
  48.         print(dedent("""
  49.        The Gothons of Planet Percal #25 have invaded your ship and
  50.        destroyed your entire crew. You are the last surviving member and
  51.        your last mission is to get the neutron destruct
  52.        bomb from the Weapons Armory, put it in the bridge, and blow
  53.        the ship up after getting into an escape pod.
  54.  
  55.        You're running down the central corridor to the Weapons Armory when a
  56.        Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown
  57.        costume flowing around his hate filled body. He's blocking the door
  58.        to the Armory and about to pull a weapon to blast you.
  59.        """))
  60.  
  61.         action = input(">")
  62.  
  63.         if action == "shoot!":
  64.             print(dedent("""
  65.            Quick on the raw you yank your blaster and fire it at the Gothon.
  66.            His clown costume is flowing and moving around his body, which throws
  67.            off your aim. Your laster hits his costume but misses him entirely.
  68.            This completely ruins his brand new costume his mother bought him,
  69.            which makes him fly into an insane rage and blast you repeatedly
  70.            in the face then eats you.
  71.            """))
  72.             return 'death'
  73.  
  74.         elif action == 'dodge!':
  75.             print(dedent("""
  76.            Like a world class boxer you dodge, weave, slip, and slide
  77.            right as the Gothon's blaster cranks a laser past your head.
  78.            In the midle of your artful dodge your foot slips and you fall
  79.            and pass out. Then the Gothon eats you.
  80.            """))
  81.             return 'death'
  82.  
  83.         elif action == "tell a joke":
  84.             print(dedent("""
  85.            Lucky for you they made you learn Gothon insults in the
  86.            academy. You tell the one Gothon joke you know:
  87.            Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr,
  88.            fur fvgf nebhaq gur ubhfr. The Gothon stops, tries not
  89.            to laugh, then busts out laughing and can't move. While
  90.            he's laughing you run up and shoot him in the head putting
  91.            him down, and then jump through the Weapon Armory door.
  92.            """))
  93.             return 'laser_weapon_armory'
  94.  
  95.         else:
  96.             print("DOES NOT COMPUTE")
  97.             return 'central_corridor'
  98.  
  99.  
  100. class LaserWeaponArmory(Scene):
  101.  
  102.     def enter(self):
  103.         print(dedent("""
  104.        You do a dive roll into the Weapon Armory, crouch and scan
  105.        the room for more Gothons that might be hiding. It's dead quiet,
  106.        too quiet. You stand up and run to the far side of the room and find
  107.        the neutron bomb in its container. There's a keypad lock on the box
  108.        and you need the code to get the bomb out. If you get the code wrong
  109.        10 times then the lock closes forever and you can't get the bomb.
  110.        The code is 3 digits.
  111.        """))
  112.  
  113.         code = f"{randint(1,9)}{randint(1,9)}{randint(1,9)}"
  114.         guess = input("[keypad]> ")
  115.         guesses = 0
  116.  
  117.         while guess != code and guesses < 10: # This will make it guess 11 times, not 10
  118.             print("BZZZZ")
  119.             guesses += 1
  120.             guess = input("[keypad]> ")
  121.  
  122.         if guess == code:
  123.             print(dedent("""
  124.            The container clicks open and the seal breaks, letting gas out.
  125.            You grab the neutron bomb and run as fast as you can to the bridge
  126.            where you must place it in the right spot.
  127.            """))
  128.             return 'the_bridge'
  129.  
  130.         else: print(dedent("""
  131.        The lock buzzes one last time and then you hear a sickening melting
  132.        sound as the mechanism is fused together. You decide to sit there,
  133.        and finally the Gothons blow up the ship from their ship.
  134.        """))
  135.         return 'death'
  136.  
  137.  
  138.  
  139. class TheBridge(Scene):
  140.  
  141.     def enter(self):
  142.         print(dedent("""
  143.        You burst onto the Bridge with the neutron destruct bomb under
  144.        your arm and surprise 5 Gothons who are trying to take control
  145.        of the ship. Each of them has an even uglier clown costume
  146.        than the last. They haven't pulled their weapons out yet, as they
  147.        see the active bomb under your arm and don't want to set it off.
  148.        """))
  149.  
  150.         action = input("> ")
  151.  
  152.         if action == "throw the bomb":
  153.             print(dedent("""
  154.            In a panic you throw the bomb at the group of Gothons and make
  155.            a leap for the door. Right as you drop it a Gothon shoots you.
  156.            As you lay there, you watch them try to disarm it and suspect it
  157.            will blow up before they do.
  158.            """))
  159.             return 'death'
  160.  
  161.         elif action == "slowly place the bomb":
  162.             print(dedent("""
  163.            You point your blaster at the bomb under your arm and the Gothons
  164.            put their hands up and start to sweat. You inch backward to the door,
  165.            open it, and then carefully place the bomb on the floor, pointing
  166.            your blaster at it. You then jump back through the door, punch the close
  167.            button and blast the lock so the Gothons can't get out. Now that the bomb
  168.            is place you run to the escape pod to get off this tin can.
  169.            """))
  170.  
  171.             return 'escape_pod'
  172.  
  173.         else:
  174.             print("DOES NOT COMPUTE")
  175.             return "the_bridge"
  176.  
  177.  
  178. class EscapePod(Scene):
  179.  
  180.     def enter(self):
  181.         print(dedent("""
  182.        You rush through the ship desperately trying to make it to
  183.        the escape pod before the whole ship explodes. It seems like
  184.        hardly any Gothons are on the ship, so your run is clear
  185.        of interference. You get to the chamber with the escape pods,
  186.        and now need to pick one to take. Some of them could be damaged
  187.        but you don't have time to look. There's 5 pods, which one do
  188.        you take?
  189.        """))
  190.  
  191.         good_pod = randint(1,5)
  192.         guess = input("[pod #]> ")
  193.  
  194.  
  195.         if int(guess) != good_pod:
  196.             print(dedent("""
  197.            You jump into pod {guess} and hit the eject button. The pod
  198.            escapes out into the void of space, them implodes as the
  199.            hull ruptures.
  200.            """))
  201.             return 'death'
  202.  
  203.         else:
  204.             print(dedent("""
  205.            You jump into pod {guess} and hit the eject button. The pod
  206.            easily slides out into space heading to the planet below. As it
  207.            flies to the planet, you look back and see your ship implode then
  208.            explode like a bright star, taking out the Gothon ship at the
  209.            same time. You won!
  210.            """))
  211.  
  212.             return 'finished'
  213.  
  214. class Finished(Scene):
  215.     def enter(self):
  216.         print("You won! Good job!")
  217.         return 'finished'
  218.  
  219.  
  220. class Map(object):
  221.     scenes = {
  222.     'central_corridor': CentralCorridor(),
  223.     'laser_weapon_armory': LaserWeaponArmory(),
  224.     'the_bridge': TheBridge(),
  225.     'escape_pod': EscapePod(),
  226.     'death': Death(),
  227.     'finished': Finished(),
  228.     }
  229.  
  230.     def __init__(self, start_scene):
  231.         self.start_scene = start_scene
  232.  
  233.     def next_scene(self, scene_name):
  234.         val = Map.scenes.get(scene_name)
  235.         return val
  236.  
  237.     def opening_scene(self):
  238.         return self.next_scene(self.start_scene)
  239.  
  240.  
  241. a_map = Map('central_corridor')
  242. a_game = Engine(a_map)
  243. a_game.play()
  244.  
  245.  
  246. # 4/21/2018 10:42 AM
Add Comment
Please, Sign In to add comment