Advertisement
TheGreatestZenMaster

PythonGame

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