Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ruchella Kock
- # 12460796
- # This file defines the Adventure class
- ##OJOO close file
- from room import Room
- from room import Route
- from items import Items
- from inventory import Inventory
- import sys
- class Adventure():
- """
- This is your Adventure game class. It should contains
- necessary attributes and methods to setup and play
- Crowther's text based RPG Adventure.
- """
- def __init__(self, game):
- """
- Create rooms and items for the appropriate 'game' version.
- """
- self.items = self.load_items(f"data/{game}Items.txt")
- self.rooms = self.load_rooms(f"data/{game}Rooms.txt")
- self.current_room = self.rooms[0]
- rooms_visited = []
- self.rooms_visited = rooms_visited
- self.players_inventory = Inventory()
- def load_items(self, filename):
- """
- Load items from filename.
- Returns a collection of item objects.
- """
- # open the right adventure file
- with open(filename, "r") as f:
- list_of_items = []
- # go over the file and make a new_item everytime
- while(True):
- item_name = f.readline().strip()
- if item_name == "":
- break
- item_description = f.readline().strip()
- initial_room_id = f.readline().strip()
- f.readline()
- new_item = Items(item_name, item_description, initial_room_id)
- list_of_items.append(new_item)
- return list_of_items
- def load_rooms(self, filename):
- """
- Load items from filename.
- Returns a collection of item objects.
- """
- with open(filename, "r") as f:
- list_of_rooms = []
- while(True):
- list_of_routes = []
- id = f.readline().strip()
- if id == "":
- break
- name = f.readline().strip()
- description = f.readline().strip()
- if description != "-----":
- f.readline()
- current_route = "initialize"
- # make a route class for every route and add it to a list of routes
- while(current_route != '' and current_route != "\n"):
- current_route = f.readline().strip()
- direction = current_route[:current_route.find(" ")]
- if current_route.find("/") != -1:
- route_id = current_route[current_route.rfind(" ") + 1: current_route.rfind("/")]
- conditional_item = current_route[current_route.find("/") + 1:]
- else:
- route_id = current_route[current_route.rfind(" ") + 1:]
- conditional_item = ""
- new_route = Route(direction, route_id, conditional_item)
- list_of_routes.append(new_route)
- del list_of_routes[-1]
- room_inventory = Inventory()
- room = Room(id, name, description, list_of_routes, room_inventory)
- list_of_rooms.append(room)
- for item in self.items:
- for room in list_of_rooms:
- if room.id == item.initial_room_id:
- room.inventory.add(item)
- return list_of_rooms
- def won(self):
- """
- Check if the game is won.
- Returns a boolean.
- """
- return self.current_room.won()
- def move(self, direction):
- """
- Moves to a different room in the specified direction.
- """
- # check if it is a valid move if it is then move to that room
- if self.current_room.valid_move(direction, self.players_inventory) != None:
- current_room_id = self.current_room.valid_move(direction, self.players_inventory)
- for room in self.rooms:
- if room.id == current_room_id:
- self.current_room = room
- self.rooms_visited.append(self.current_room.id)
- else:
- return False
- # if its a forced movement then print description and move to the room
- if self.current_room.forced_movement() and not self.current_room.won():
- self.current_room.look(self.rooms_visited)
- adventure.move("FORCED")
- return self.current_room.name
- def play(self):
- """
- Play an Adventure game
- """
- print(f"Welcome, to the Adventure games.\n"
- "May the randomly generated numbers be ever in your favour.\n")
- self.current_room.printing(self.rooms_visited)
- # Prompt the user for commands until they've won the game.
- while not self.won():
- command = input("> ")
- command = command.upper()
- # Check if the command is a movement or not.
- if command in ["EAST", "WEST", "SOUTH", "NORTH", "UP", "DOWN", "IN", "OUT", "WAVE", "XYZZY", "PLUGH", "WAVE", "JUMP", "WATER", "SWIM"]:
- # if its a valid move then move to that room
- if adventure.move(command):
- self.current_room.printing(self.rooms_visited)
- else:
- print("Invalid move")
- # will print the description of the room the player is in
- elif command == "LOOK":
- self.current_room.look(self.rooms_visited)
- # will remind player how to play
- elif command == "HELP":
- print("You can move by typing directions such as EAST/WEST/IN/OUT")
- # will show player what is in the inventory
- elif command == "INVENTORY":
- # checks if players's inventory is empty
- if self.players_inventory.is_empty():
- print("Your inventory is empty.")
- else:
- print(self.players_inventory)
- # player can take an item and add it to their inventory
- elif command[0:4] == "TAKE":
- command_item = command.split()
- adventure.take_or_drop(command_item[1], self.players_inventory, self.current_room.inventory, "taken.")
- # player can drop an item and it will go back to the intitial room
- elif command[0:4] == "DROP":
- command_item = command.split()
- adventure.take_or_drop(command_item[1], self.current_room.inventory, self.players_inventory, "droped.")
- # with the quit command the program will stop running thus ending the game
- elif command == "QUIT":
- print("Thanks for playing")
- sys.exit()
- else:
- print("Invalid command")
- def take_or_drop(self, command, take_from_inventory, remove_from_inventory, string):
- """
- Take an item from the room inventory and add it to the players inventory
- Drop an item from the player inventory and add it to the room_inventory
- """
- # check if the item is in the inventory
- item = remove_from_inventory.find_item(command)
- # if it is then move it from one inventory to the other
- if item != None:
- take_from_inventory.add(item)
- remove_from_inventory.remove(item)
- print(f"{command} {string}")
- else:
- print("No such item.")
- if __name__ == "__main__":
- adventure = Adventure("Tiny")
- adventure.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement