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 gameself.location
contains the room the player is currently in,self.oldlocation
contains the previous locationself.inventory
is a dict mapping the item’s IDs to the items the player is carryingself.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
-
go
(noun)[source]¶ decorated by
textgame.player.action_method()
change location to the room in the direction
noun
.noun
can be intextgame.globals.DIRECTIONS
or ‘back’. On different inputs, returntextgame.globals.MOVING.FAIL_NOT_DIRECTION
-
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
-
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 oftextgame.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
.