Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
# Ruchella Kock # 12460796 # This file defines the Adventure class 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(" ")] route_id = current_route[current_route.rfind(" ") + 1: current_route.rfind(" ") + 2] conditional_item = current_route[current_route.rfind(" ") + 3:] 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) print(room) 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"]: # 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("Crowther") adventure.play()
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Tttg
2 hours ago | 0.33 KB
OoT rando seed 6/6
2 hours ago | 68.46 KB
Symbol Dump for Discord
18 hours ago | 0.41 KB
tes
21 hours ago | 0.02 KB
Untitled
1 day ago | 2.28 KB
GSA NS
1 day ago | 1.37 KB
WaterFul.m
MatLab | 1 day ago | 0.42 KB
WaterEmpty.m
MatLab | 1 day ago | 0.54 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!