3.3. textgame.player

This module contains a class textgame.player.Player that is used to define what a player is able to do in the game. Every of its methods that get called by textgame.parser.Parser must take a noun (string) as an argument and return either a string that describes the action or a textgame.parser.EnterYesNoLoop. For convenience, this module provides wrappers for Player methods:

class textgame.player.Player(world, initlocation)[source]

Bases: object

class to represent the player of the game

  • holds an instance of textgame.world.World so that its methods can have the widest possible impact on the game

  • self.location contains the room the player is currently in, self.oldlocation contains the previous location

  • self.inventory is a dict mapping the item’s IDs to the items the player is carrying

  • self.status tracks the player’s status: {"alive": True, "fighting": False, "trapped": False}

ask_hint(noun)[source]

decorated by textgame.player.player_method()

ask for a hint in the current location, if there is one, return textgame.parser.EnterYesNoLoop if the hint should really be displayed

attack(noun)[source]

decorated by textgame.player.action_method()

kill a monster based on randomness, the monster’s strength and on how long the fight has been going already. Die if killing fails too often.

If the history of the monster is -1, the monster’s ignoretext gets returned.

close(noun)[source]

decorated by textgame.player.action_method()

lock the door in direction noun if player has a key in inventory that fits

drop(noun)[source]

decorated by textgame.player.action_method()

see if something with the ID noun is in the inventory. If yes, remove it from inventory and add it to location

dropall()[source]

move all items in the inventory to current location

forget()[source]

set old location to current location

go(noun)[source]

decorated by textgame.player.action_method()

change location to the room in the direction noun. noun can be in textgame.globals.DIRECTIONS or ‘back’. On different inputs, return textgame.globals.MOVING.FAIL_NOT_DIRECTION

goback()[source]

change location to previous location if there’s a connection

has_light()[source]

returns true if player carries anything that lights a room up

list_inventory(noun)[source]

decorated by textgame.player.action_method()

return a pretty formatted list of what’s inside inventory

listen(noun)[source]

decorated by textgame.player.action_method()

get the current room’s sound

look(noun)[source]

decorated by textgame.player.action_method()

get the long description of the current location. Also spawn monsters and check check_restrictions (see textgame.room.Room.check_restrictions())

open(noun)[source]

decorated by textgame.player.action_method()

open the door in direction noun if player has a key in inventory that fits

show_score(noun)[source]

decorated by textgame.player.action_method()

take(noun)[source]

decorated by textgame.player.action_method()

see if something with the ID noun is in the items of the current location. If yes and if it’s takable and not dark, remove it from location and add it to inventory

takeall()[source]

move all items in the current location to inventory

textgame.player.action_method(f)[source]

wrapper for player methods

does the same as textgame.player.player_method() plus it adds to the return value of the original function the output of textgame.world.World.update(). This way it is guaranteed that time passes and fights get managed when the player does something.

Also, this saves the undecorated function in a new attribute f.undecorated.

textgame.player.player_method(f)[source]

wrapper for player methods

checks if the method has the right signature, adds a dummy argument if the method doesn’t care about nouns. Throws TypeError if there are too many arguments.