3.2. textgame.world

This module contains only one class, textgame.world.World that holds all rooms, items and monsters, is responsible for spawning monsters randomly in the rooms and managing daylight. At initialization you can pass a dict to textgame.world.World that’s formatted like this:

{
 "room_id1": {
     "descript": "You're in the great room.",
     "doors": {"south": "room_id2"}
 },
 "room_id2": {
     "descript": "You're in the small room.",
     "doors": {"north": "room_id1"}
 }
}

The items inside the dict automatically get converted to textgame.room.Room objects and linked to each other according to the doors dict. Every argument of textgame.room.Room.fill_info() can be put as a key-value pair inside this dict.

The same is true for items, weapons and monsters, see the example.

World has a member called storage_room of type textgame.room.Room that can be used to put stuff inside that should not be visible for the player.

class textgame.world.World(rooms=None, items=None, weapons=None, monsters=None, seed=None)[source]

Bases: object

Parameters
  • rooms – dict describing all rooms (see above)

  • items – dict describing all items

  • weapons – dict describing all weapons

  • monsters – you guessed it

  • seed (int) – seed for the random number generator. If None, a random seed is taken

create_items(descriptions, tag='items')[source]

create textgame.movable [Weapon,Item,Monster] objects based on description-dict (see above). This gets called on initialization if weapons,items,monsters was provided.

Parameters
  • descriptions (dict) –

  • tag – can be ‘items’, ‘monsters’, ‘weapons’

create_rooms(descriptions)[source]

create textgame.room.Room objects based on description-dict (see above). This gets called on initialization if rooms was provided.

manage_daylight()[source]

check if it’s nighttime and turn all rooms dark if yes

Return type

a message that night came in or an empty string

manage_fight(player)[source]

if there are active, harmful monsters around, this method checks if the player is fighting them and kills the player / the monster, depending on random numbers and the monster’s strenght

Return type

string describing the status of the fight

put_items_in_place()[source]

iterate over all items, see if their initlocation points to a room in this world and add it there. This is done automatically if the items are passed at initialization

put_monsters_in_place()[source]

iterate over all monsters, see if their initlocation points to a room in this world and add it there. This is done automatically if the monsters are passed at initialization

room(ID)[source]

get room by ID. If the room is not found, the logger prints an error

Parameters

ID – room ID

Return type

textgame.room.Room or None

set_room_restrictions(restrictions)[source]

takes a dict of the following form:

{
 "room_ID": {"func": my_func}
}

for every room_ID, find the corresponding room and call set_specials with the mapped dict on it. See textgame.room.Room.set_specials() for further reference.

spawn_monster(location)[source]

randomly spawn a monster in location

the condition for a monster to spawn is:

  • no other monsters in this room

  • random number must be smaller than monster’s spawn_prob

  • at least one of the strings in monster’s spawns_in list must be contained in the location’s ID

  • monster’s spawns_at must be equal to the current daytime or ‘always’

  • monster must not be active already

Parameters

locationtextgame.room.Room in which a monster should spawn

update(player)[source]

increase the time, call textgame.world.World.manage_fight() and textgame.world.World.manage_daylight()

Return type

output of manage_fight and manage_daylight (str)