Battles

Battles are the most basic form of conflict in an adventure, the player is confronted by enemies of various strengths with the promise that once defeated the story may progress. The simple way to initiate a battle is to call World.initate_battle but you can start your own battle by instancing the Battle and then calling Battle.game_loop.

class pyzork.battle.Battle(**kwargs)[source]

The battle class does need to be subclassed unless you need a very fine grained control over how battles go. For most users, simply using this and the various parameters it makes available to you should be enough.

Once you instantiated a Battle all you need to do is to call game_loop method to start the battle, the method will return if the player wins, if not it will raise EndGame.

Parameters:
  • player (Player) – The player fighting
  • enemies (List[Enemy]) – The list of enemies for the player to fight
  • location (Optional[Location]) – Where the player is fighting, this is optional in the case that the list of enemies is form this location and it needs to be updated once they get killed off.
  • priorities (Optional[Callable[[Battle], Callable[]]:) – Optional callable which determines in what order all the entities in the battle take turn.
player

The player in the battle

Type:Player
alive

The list of enemies still alive

Type:List[NPC]
dead

The list of NPCS that have died

Type:List[NPC]
turn

The number of turns that have passed.

Type:int
battle_loop()[source]

Heart of the battle system. Call this to start the battle

battle_parser() → bool[source]

Take input of the user and parse it against a set of possible actions

Returns:Whether this actions counts as taking a turn. If the action counts as taking a turn then performing it will end the player’s turn and move onto the rest of the priorities.
Return type:bool
end_turn()[source]

Increments the turns, remove dead stuff and decrement duration of modifiers

player_turn()[source]

Print possible options and let the user pick one through battle_parser

priorities()[source]

Overwritable method to determine in what order all the entities involved in this battle go. This function must return a list of entities which have a battle_logic method implmenented.

Returns:The list of turn entities.
Return type:List[Union[Player, Enemy]]
remove_dead(index: int)[source]

Remove a dead enemy from the list of living enemies and grant experience to the player

Parameters:index (int) – Index of the enemy to remove
win_condition() → bool[source]

You can override this function to change the win condition of the battle by default winning the battle requires that the player still be alive and that all enemies have been wiped out.

Returns:True if the battle has been won, false if it’s still going. If you need to interupt the battle without it resulting in a victory you will need to raise an error and catch it in the handlers
Return type:bool

Examples

There are lots of customization option in the Battle class.

Changing priorities

By default, in every battle the player goes first and then every enemy goes after in turn. However, if you want to for example to have the player take their turn after each enemy you could do something like there:

from pyzork import Player

from my_adventure.enemies import Goblin

def better_priorities(battle):
    priorities = []
    for enemy in battle.alive:
        priorities.append(battle.player)
        priorities.append(enemy)

    return priorities

battle = Battle(
    enemies=[Goblin(), Goblin(), Goblin()],
    player=Player(),
    priorities=better_priorities
)

battle.battle_loop()