Advertisement
TTpocToXaKep

2D room game

Feb 5th, 2023
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. # Room Game by https://pastebin.com/u/TTpocToXaKep
  2.  
  3. # Define the Room class
  4. class Room:
  5.     def __init__(self, name, description):
  6.         self.name = name
  7.         self.description = description
  8.         self.items = []
  9.  
  10. # Define the Player class
  11. class Player:
  12.     def __init__(self, name, current_room):
  13.         self.name = name
  14.         self.current_room = current_room
  15.         self.inventory = []
  16.    
  17.     def move_to_room(self, room):
  18.         self.current_room = room
  19.         print(f"You have moved to the {room.name}.")
  20.         print(room.description)
  21.    
  22.     def pick_up_item(self, item):
  23.         self.inventory.append(item)
  24.         print(f"You have picked up the {item}.")
  25.    
  26.     def drop_item(self, item):
  27.         self.inventory.remove(item)
  28.         print(f"You have dropped the {item}.")
  29.  
  30. # Initialize rooms
  31. kitchen = Room("Kitchen", "A well-lit room with a large table in the center.")
  32. living_room = Room("Living Room", "A cozy room with a fireplace and a sofa.")
  33. bedroom = Room("Bedroom", "A comfortable room with a large bed and a dresser.")
  34.  
  35. # Initialize player
  36. player = Player("Player", kitchen)
  37.  
  38. # Play the game
  39. while True:
  40.     print(f"You are in the {player.current_room.name}. What do you want to do?")
  41.     action = input("Move to another room, pick up item, or drop item: ")
  42.    
  43.     if action.lower() == "move to another room":
  44.         room_name = input("Which room would you like to move to? ")
  45.         if room_name.lower() == "kitchen":
  46.             player.move_to_room(kitchen)
  47.         elif room_name.lower() == "living room":
  48.             player.move_to_room(living_room)
  49.         elif room_name.lower() == "bedroom":
  50.             player.move_to_room(bedroom)
  51.         else:
  52.             print("That room does not exist.")
  53.    
  54.     elif action.lower() == "pick up item":
  55.         item = input("Which item would you like to pick up? ")
  56.         player.pick_up_item(item)
  57.         player.current_room.items.remove(item)
  58.    
  59.     elif action.lower() == "drop item":
  60.         item = input("Which item would you like to drop? ")
  61.         player.drop_item(item)
  62.         player.current_room.items.append(item)
  63.        
  64.     else:
  65.         print("Invalid action.")
Tags: game Room 2d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement