TheGreatestZenMaster

buggymonsterpaste

Mar 20th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.40 KB | None | 0 0
  1. #------ Import Statements-------#
  2. import sys
  3.  
  4.  
  5. #--------- Game Classes ------- #
  6. class Player(object):
  7.     inventory = {}
  8.  
  9.     def __init__(self, name, race, sex, current_location):
  10.         self.name = name
  11.         self.race = race
  12.         self.sex = sex
  13.         self.current_location = current_location
  14.  
  15.         self.health = 0
  16.         self.max_hp = 0
  17.         self.attack = 0
  18.         self.base_attack = 0
  19.         self.armour = 0
  20.         self.xp = 0
  21.         self.level = 0
  22.         self.status = "Alive"
  23.         self.base_xp = 0
  24.  
  25.     def level_up(self):
  26.         """
  27.        increases stats to next level.
  28.        currently this assumes that levels scale linearly to infinity.
  29.        """
  30.         new_level_health = 50
  31.         new_level_hp = 50
  32.         new_level_attack = 10
  33.         new_level_base_attack = 10
  34.         new_level_armour = 5
  35.  
  36.         self.health += new_level_health
  37.         self.max_hp += new_level_hp
  38.         self.attack += new_level_attack
  39.         self.base_attack += new_level_base_attack
  40.         self.armour += new_level_armour
  41.         self.level += 1
  42.  
  43.     @staticmethod
  44.     def player_status():
  45.         print "Your stats are:\nHealth: %r, Armour: %r, Attack: %r, Level: %r, Experience: %r" % \
  46.               (player.health, player.armour, player.attack, player.level, player.xp)
  47.  
  48.     @staticmethod
  49.     def player_location():
  50.         print "You are in %r" % player.current_location
  51.  
  52.     def attack(self, monster):
  53.         monster.health -= (self.attack - monster.armour)
  54.         if monster.health <= 0:
  55.             print "You beat the monster!"
  56.             player.xp += monster.xp
  57.             prompt_levelup()
  58.             return False
  59.  
  60.  
  61. class Key(object):
  62.     def __init__(self, name, room, match):
  63.         self.name = name
  64.         self.room = room
  65.         self.match = match
  66.  
  67.  
  68. class Door(object):
  69.     def __init__(self, name, number, locked, match):
  70.         self.name = name
  71.         self.number = number
  72.         self.locked = locked
  73.         self.match = match
  74.  
  75.  
  76. class Room(object):
  77.     def __init__(self, name, number, victory):
  78.         self.self = self
  79.         self.number = number
  80.         self.name = name
  81.         self.victory = victory
  82.  
  83.  
  84. class Hall(object):
  85.     def __init__(self, name, number=0, victory=None):
  86.         self.self = self
  87.         self.number = number
  88.         self.name = name
  89.         self.victory = victory
  90.  
  91.  
  92. class Monster(object):
  93.     pass
  94.  
  95.  
  96. class Wolf(Monster):
  97.     def __init__(self, name, room):
  98.         self.self = self
  99.         self.room = room
  100.         self.name = name
  101.  
  102.         self.health = 0
  103.         self.max_hp = 0
  104.         self.attack = 0
  105.         self.base_attack = 0
  106.         self.armour = 0
  107.         self.xp = 0
  108.         self.level = 0
  109.         self.status = "Alive"
  110.  
  111.     def level_up(self):
  112.         new_level_health = 20
  113.         new_level_hp = 20
  114.         new_level_attack = 5
  115.         new_level_base_attack = 5
  116.         new_level_armour = 0
  117.         new_level_xp = 10
  118.  
  119.         self.health += new_level_health
  120.         self.max_hp += new_level_hp
  121.         self.attack += new_level_attack
  122.         self.base_attack += new_level_base_attack
  123.         self.armour += new_level_armour
  124.         self.xp += new_level_xp
  125.         self.level += 1
  126.  
  127.     def attack(self):
  128.         player.health -= (self.attack - player.armour)
  129.         if player.health <= 0:
  130.             print "You have died! Better luck next time!"
  131.             sys.exit()
  132.  
  133.  
  134. #----- Various static Values ------#
  135. hallway = Hall("Hallway")
  136.  
  137. list_of_keys = [Key("Key", "Room 1", 10001),
  138.                 Key("Key", "Room 2", 20002),
  139.                 Key("Key", "Room 3", 30003)]
  140.  
  141. list_of_rooms = [Room("Room 1", 1, False),
  142.                  Room("Room 2", 2, False),
  143.                  Room("Room 3", 3, False),
  144.                  Room("Room 4", 4, False)]
  145.  
  146. list_of_doors = [Door("Door 1", 1, False, None),
  147.                  Door("Door 2", 2, True, 10001),
  148.                  Door("Door 3", 3, True, 20002),
  149.                  Door("Door 4", 4, True, 30003)]
  150.  
  151. list_of_monsters = [Wolf("wolf", "Room 2"),
  152.                     Wolf("wolf", "Room 3"),
  153.                     Wolf("wolf", "Room 4")]
  154.  
  155. player = Player("NoName", "Male", "Human", None)
  156. player.level_up()
  157.  
  158. # Available actions for the loops
  159. list_of_actions_available_room = ["grab", "leave", "stats", "location", "keys", "help"]
  160. list_of_actions_available_true_main = ["enter", "exit", "stats", "location", "help"]
  161.  
  162.  
  163. #----- Opening Setup------#
  164. def opening_setup():
  165.     """This opening setup gives us a player name and also provides the opening text"""
  166.     name = raw_input("Whats your name?")
  167.     player.name = name
  168.     player.current_location = "Hallway"
  169.     generate_rooms_dict()
  170.     generate_keys_dict()
  171.     player.player_location()
  172.     # The opening text to give a little story(will change later once we have some actual story)
  173.     opening_text = "Hello and welcome to your adventure %r!\n" \
  174.                    "You must have had one heck of a night last night" \
  175.                    " because you don't know where you are or how you got here!\n" \
  176.                    "Never fear, I'm sure you can figure it out!" % player.name
  177.     print opening_text
  178.  
  179.  
  180. #------- Room Populating function ----#
  181. def generate_rooms_dict():
  182.     room_dict = {}
  183.     for room in list_of_rooms:
  184.         room_dict[room] = room.name
  185.     return room_dict
  186.  
  187.  
  188. def generate_doors_dict():
  189.     room_dict = generate_rooms_dict()
  190.     dict_of_doors_in_room = {}
  191.     if player.current_location == "Hallway":
  192.         for door in list_of_doors:
  193.             dict_of_doors_in_room[door] = door.name
  194.         return dict_of_doors_in_room
  195.     else:
  196.         room = player.current_location
  197.         for i in room_dict:
  198.             if i.name == room:
  199.                 room_number = i.number
  200.                 for door in list_of_doors:
  201.                     if room_number == door.number:
  202.                         door_name = door.name
  203.                         dict_of_doors_in_room[door] = door_name
  204.                         return dict_of_doors_in_room
  205.  
  206.  
  207. def generate_keys_dict():
  208.     dict_of_keys = {}
  209.     for key in list_of_keys:
  210.         dict_of_keys[key] = key.name
  211.     return dict_of_keys
  212.  
  213.  
  214. def generate_room_keys_dict():
  215.     dict_of_keys = generate_keys_dict()
  216.     dict_of_room_keys = {}
  217.     for key in dict_of_keys:
  218.         if key.room == player.current_location:
  219.             dict_of_room_keys[key] = key.name
  220.             return dict_of_room_keys
  221.  
  222.  
  223. def generate_monsters_dict():
  224.     dict_of_monsters = {}
  225.     for monster in list_of_monsters:
  226.         dict_of_monsters[monster] = monster.name
  227.     return dict_of_monsters
  228.  
  229.  
  230. def populate_room_monster_dict():
  231.     dict_of_room_monsters = {}
  232.     dict_of_monsters = generate_monsters_dict()
  233.     for monster in dict_of_monsters:
  234.         if monster.room == player.current_location:
  235.             dict_of_room_monsters[monster] = monster.name
  236.     return dict_of_room_monsters
  237.  
  238.  
  239. # ------- Status Functions ------#
  240. # These functions are there to display the info about the room/player on request
  241. def visible_keys():
  242.     dict_of_room_keys = generate_keys_dict()
  243.     count = 0
  244.     for key in dict_of_room_keys:
  245.         if key.room == player.current_location:
  246.             count += 1
  247.     if count == 0:
  248.         print "There are no keys"
  249.     else:
  250.         print "You can see %r keys." % count
  251.  
  252.  
  253. def visible_doors():
  254.     if player.current_location == "Hallway":
  255.         count = len(list_of_doors)
  256.         print "You can see %r doors numbered accordingly." % count
  257.     else:
  258.         print "You can see the door you came through"
  259.  
  260.  
  261. def door_status(door):
  262.         if door.locked:
  263.             print "Door number %r is locked!" % door.number
  264.         elif not door.locked:
  265.             print "Door number %r is unlocked!" % door.number
  266.  
  267.  
  268. def visible_monsters():
  269.     dict_of_room_monsters = populate_room_monster_dict()
  270.     for monster in dict_of_room_monsters:
  271.         print "You can see a %r!" % monster.name
  272.  
  273.  
  274. #-------- Player Functions --------#
  275. def xp_check():
  276.     """Checks player xp and necessary xp and calls the level up function if applicable"""
  277.     global player
  278.     xp_needed = player.level * 10 + 15
  279.     if player.xp >= xp_needed:
  280.         player.level_up()
  281.         player.xp -= xp_needed
  282.         print "You have leveled up!"
  283.     else:
  284.         xp_needed = player.level * 10 + 15
  285.         print "Not enough xp to level up!", "You need %r" % xp_needed, "You have %r" % player.xp
  286.  
  287.  
  288. def prompt_levelup():
  289.     check_levelup = raw_input("Would you like to see if you leveled up?")
  290.     if check_levelup == "yes":
  291.         xp_check()
  292.  
  293.  
  294. #------Help function ------#
  295.  
  296. def help_info():
  297.     """Provides a information about the available prompts should the player need it"""
  298.     help_info_dict = {"enter": "This command will move your character into the next room.",
  299.                       "exit": "This command exits the game! Careful!",
  300.                       "stats": "This command provides you with info about your character.",
  301.                       "grab": "This command lets you grab any object. Simply say which one.",
  302.                       "leave": "This command lets you leave through any unlocked door",
  303.                       "doors": "This command shows you what doors are visible.",
  304.                       "keys": "This command show you any keys you can see.",
  305.                       "location": "This command tells you your current location.",
  306.                       "fight": "This command lets you fight the monster"
  307.                       }
  308.     while True:
  309.         info_choice = raw_input("What would you like to know more about?").lower()
  310.         try:
  311.             print help_info_dict[info_choice]
  312.         except KeyError:
  313.             if info_choice == "back":
  314.                 return False
  315.             else:
  316.                 print "That's not a valid command!"
  317.  
  318.  
  319. #------- Actions Functions --------#
  320. def take_action(action):
  321.     """This function takes the input of the engine for
  322.    action and completes that action
  323.    Note: This function is only accessible inside the room loop
  324.    """
  325.     room_dict = generate_rooms_dict()
  326.     dict_of_room_keys = generate_room_keys_dict()
  327.     if action == "grab":
  328.         grab_object = raw_input("Grab what?")
  329.         if grab_object == "key":
  330.             for i in dict_of_room_keys:
  331.                 if grab_object.lower() == i.name.lower():
  332.                     player.inventory[i] = i.name
  333.                     print "You grabbed it!"
  334.                     del dict_of_room_keys[i]
  335.                     break
  336.         return True
  337.     elif action == "leave":
  338.         for i in room_dict:
  339.             if i.name == player.current_location:
  340.                 current_room = i
  341.                 if not current_room.victory:
  342.                     room_xp = 25
  343.                     player.xp += room_xp
  344.                     print "Congrats you beat this room!"
  345.                     print "You earned %r xp!" % room_xp
  346.                     print "You exit the room!"
  347.                     prompt_levelup()
  348.                     current_room.victory = True
  349.                     player.current_location = "Hallway"
  350.                     return False
  351.                 else:
  352.                     print "You exit the room!"
  353.                     current_room.victory = True
  354.                     player.current_location = "Hallway"
  355.                     return False
  356.     elif action == "stats":
  357.         player.player_status()
  358.         return True
  359.     elif action == "help":
  360.         help_info()
  361.         return True
  362.     elif action == "fight":
  363.         opponent_engine()
  364.         return True
  365.     elif action == "doors":
  366.         visible_doors()
  367.         return True
  368.     elif action == "location":
  369.         player.player_location()
  370.         return True
  371.     return True
  372.  
  373.  
  374. def battle_engine(monster):
  375.     while True:
  376.         Wolf.attack(monster)
  377.         Player.attack(player, monster)
  378.  
  379.  
  380. def opponent_engine():
  381.     dict_of_room_monsters = populate_room_monster_dict()
  382.     for monster in dict_of_room_monsters:
  383.         battle_engine(monster)
  384.     return False
  385.  
  386.  
  387. def game_engine():
  388.     """Game_engine is should maybe be called room_engine
  389.    as its only function is to act as a go between for the main loop
  390.    """
  391.     generate_doors_dict()
  392.     visible_keys()
  393.     while True:
  394.         print "Your available actions while in the room are %s" % list_of_actions_available_room
  395.         action = raw_input("What do you want to do?")
  396.         result = take_action(action)
  397.         if not result:
  398.             return False
  399.  
  400.  
  401. #------ Main Loops ---------- #
  402. def hall_room_transition():
  403.     room_choice = raw_input("Which room would you like to enter?(please enter a number)")
  404.     dict_of_doors = generate_doors_dict()
  405.     room_dict = generate_rooms_dict()
  406.     for i in dict_of_doors:
  407.         if i.number == int(room_choice):
  408.             door_choice = i
  409.             door_number = i.number
  410.             if door_choice.locked:
  411.                 print "The door seems to be locked. Maybe you need to use a key!"
  412.                 use_key = raw_input("Use a key? (yes or no)")
  413.                 if use_key.lower() == "yes":
  414.                     for x in player.inventory:
  415.                         if x.match == door_choice.match:
  416.                             door_choice.locked = False
  417.                             print "You used the key!"
  418.                             door_status(door_choice)
  419.                             enter_through_door = raw_input("Enter the room?")
  420.                             if enter_through_door == "yes":
  421.                                 print "You entered the room!"
  422.                                 for room in room_dict:
  423.                                     if room.number == door_number:
  424.                                         player.current_location = room.name
  425.                                         break
  426.                         else:
  427.                             print "There are no matching keys in your inventory!"
  428.                             break
  429.                 elif use_key.lower() == "no":
  430.                     print "OK, but the door is still locked!"
  431.             elif not door_choice.locked:
  432.                 print "You entered the room!"
  433.                 for room in room_dict:
  434.                     if room.number == door_number:
  435.                         player.current_location = room.name
  436.  
  437.  
  438. def action_main():
  439.     """This main function is the secondary loop that is operational while the player is in the room
  440.    """
  441.     while True:
  442.         if player.current_location == "Hallway":
  443.             hall_room_transition()
  444.         else:
  445.             generate_room_keys_dict()
  446.             game_engine()
  447.             return False
  448.  
  449.  
  450. def upper_main():
  451.     """This loop should never exit.
  452.    Exception: if the player specifies to exit
  453.    """
  454.     opening_setup()
  455.     while True:
  456.         raw_input("Press <Enter> to continue!")
  457.         print "Your available actions while in the hallway are %s" % list_of_actions_available_true_main
  458.         player.current_location = "Hallway"
  459.         take_action_main = raw_input("What do you want to do?")
  460.         if take_action_main == "enter":
  461.             action_main()
  462.         elif take_action_main == "exit":
  463.             sys.exit()
  464.         elif take_action_main == "stats":
  465.             player.player_status()
  466.         elif take_action_main == "help":
  467.             help_info()
  468.         elif take_action_main == "location":
  469.             player.player_location()
  470.         else:
  471.             print "That's not a valid command!"
  472.  
  473.  
  474. #------- Game Operation --------#
  475. upper_main()
Advertisement
Add Comment
Please, Sign In to add comment