3.1. textgame.room¶
This module contains only one class, textgame.room.Room
, that represents
a single room and its connections to other rooms.
You can create one like so
myroom1 = textgame.room.Room("room_01")
myroom2 = textgame.room.Room("room_02")
# add some information
myroom1.fill_info(
doors={"north": myroom2},
descript="You are at a beach stretching from north to south.",
sdescript="You're at the beach."
)
See textgame.room.Room.fill_info()
for more. It is also possible for a room to
have a connection to itself.
Room
has a method textgame.room.Room.check_restrictions()
that checks if
it’s dark inside the room and calls a special_func
. You can specify the special_func
to do whatever you wish should happen when a player enters this room. Say for example,
all the player’s money gets stolen if he/she enters myroom2
:
# NOTE: the first argument must be the player
def steal_money(player):
if "money" in player.inventory:
# don't delete the money, better store it in
# the storage room (see :class:`textgame.world.World`)
player.world.storage_room.add_item( player.inventory.pop("money") )
return "Someone just stole all your money."
# be sure to return a string
return ""
myroom2.set_specials(steal_money)
-
class
textgame.room.
Room
(ID)[source]¶ Bases:
object
- Parameters
ID (str) – unique identifier
-
add_connection
(dir, room, hidden=False)[source]¶ add a single connection to the room
- Parameters
dir – direction, must be in
textgame.globals.DIRECTIONS
room –
textgame.room.Room
objecthidden – specify if the connection is hidden
-
check_restrictions
(player)[source]¶ check if it’s dark and call the function set by
textgame.room.Room.set_specials()
- Parameters
player –
textgame.player.Player
object- Returns
empty string or the string returned by the special function
-
describe
(long=False)[source]¶ return the description (
long=True
) or short description (long=False
) of the room ortextgame.globals.DESCRIPTIONS.DARK_L
if the room is dark
-
fill_info
(descript='', sdescript='', value=5, dark=None, sound="It's all quiet.", hint='', hint_value=2, errors=None, doors=None, hiddendoors=None, locked=None, dir_descriptions=None)[source]¶ add all the descriptive information
- Parameters
descript – string describing the room
sdescript – string describing the room, but shorter
value (int or float) – how much player’s score increases on first visit
dark – dict specifying if the room is dark. Sould be formatted like
{"now": <bool>, "always": <bool>}
sound – string describing the sound in this room
hint – string giving a hint on what to do in this room
hint_value (int or float) – how much score this hint costs
errors – dict mapping directions to error messages that get printet if player tries to go in this direction eg
{"north":"Something blocks the way."}
doors – dict mapping directions to rooms
hiddendoors – dict mapping directions to rooms but it’s not possible to use these connections via
textgame.player.Player.go()
locked – dict mapping directions to the state of the connections, eg
{"north": {"locked":True, "key":123}}
see examplesdir_descriptions – dict mapping directions to descriptive messages about going in this directions, eg
{"up": "You spread your wings and start to fly."}
add all hidden connections to visible connections
-
set_specials
(func, **kwargs)[source]¶ set a custom function that gets called in
textgame.room.Room.check_restrictions()
- Parameters
func – function that takes a
textgame.player.Player
object as first argument. Must return a string!kwargs – optional keyword arguments for
func