0
\$\begingroup\$

First time that i am making a text based card game, the main code here is starting to feel a bit clunky.This is a turn-based/phase based system where an effect/ability activates in a specific state/phase. How it should work

  • phase:
    • Check ability
    • check if 1 of the players are dead ( If true ACTIVATE death effect/ability of card that died)
    • Do appropriate action in phase
    • next phase

So 2 questions:

  • if a card/player dies in any one of these states/phases, how can i interrupt the state to go to another one?

  • The apply_effect method works fine. But is there a cleaner way to accomplish what i am doing?

game = GamePhases("gameState")
print(game.state)
# game starting
print_health(player1, player2)

game.moveSelect()
print(game.state)
playerArray = [player1, player2]

while playerArray[0].getHp() > 0 and playerArray[1].getHp() > 0:
    print_health(playerArray[0], playerArray[1])
    attackArray = []

    # player move select
    while game.state == "MoveSelect":
        # do ting
        print("Choose random moves for both players")
        attackArray = [random.choice(playerArray[0].attacks), random.choice(playerArray[1].attacks)]

        game.preAttack()
        print(game.state)

    while game.state == "PreAttack":
        # activate effects function
        # how can i check if player is still alive?
        game.damageCalc()
        print(game.state)

    while game.state == "DamageCalc":
        # hit the D20 !!!
        # damage_calc does damage calculation, which attack is faster and who hits first based on attack used
        playerArray = damage_calculation(playerArray[0], playerArray[1], attackArray[0], attackArray[1])

        game.postAttack()

        print(game.state)
        print_health(playerArray[0], playerArray[1])

    while game.state == "PostAttack":
        # activate effects
        # check if player is still alive

        """
        def apply_effect(player_array: {__getitem__},
                 activator: {__eq__},
                 target: Any,
                 attack: {target, trigger},
                 ability: Any,
                 phase: Any) -> list[{__eq__}
        """
        # check player 1 post attack effect
        playerArray = apply_effect(playerArray, playerArray[0], playerArray[1], attackArray[0], playerArray[0].ability,
                                   "postAttack")

        # check player 2 post attack effect
        playerArray = apply_effect(playerArray, playerArray[1], playerArray[0], attackArray[1], playerArray[1].ability,
                                   "postAttack")

        print_health(playerArray[0], playerArray[1])
        game.moveSelect()

here is my applying_Effect function: What this does, is apply effect based on the attack targeting, then return the playerArray in the CORRECT order.

def apply_effect(player_array, activator, target, attack, ability, phase):
    # attack effect targeting
    if attack.target == "self" and attack.trigger == phase:
        activator = effects(activator, attack.effect, attack.getEffectVar1(), attack.getEffectVar2())
    elif attack.target == "opponent" and attack.trigger == phase:
        target = effects(activator, attack.tag, attack.getEffectVar1(), attack.getEffectVar2())
    elif attack.target == "both" and attack.trigger == phase:
        activator = effects(activator, attack.effect, attack.getEffectVar1(), attack.getEffectVar2())
        target = effects(activator, attack.tag, attack.getEffectVar1(), attack.getEffectVar2())

    # return players in the right order
    if activator == player_array[0]:
        return [activator, target]
    else:
        return [target, activator]
```
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.