Ex. 43 - battle system

Hello, forum members!
I’ve doing a drill-down for ex.43, and Zed suggested to add a simple battle system in game.
So I thought to do following:

  1. Added another elif to the room that guides player to another class(scene):
class CentralCorridor(Scene):
        elif action == "fight":
                You take off your shirt and prepare to fight him.
            return 'fight_scene'
  1. Added new scene with following code:
class FightScene(Scene):
    def enter(self):
        alien_hp = 10
        player_hp = 10
        round = 1
            You've decided to fight that alien.
            You have no weapon, so he throws away his blaster. Bare knuckles vs. raw power...
        print(dedent("""You are trying to hit him, and every time he will fight back"""))
        while alien_hp > 0 and player_hp > 0:
            print(f"\nRound {round}")
            player_hit = randint(1,3)
            print(f"You hit Alien and his HP is decreased by {player_hit}")
            alien_hp -= player_hit
            alien_hit = randint(1,3)
            print(f"Alien hits you and your HP is decreased by {alien_hit}")
            player_hp -= alien_hit
            round += 1
        if player_hp <= 0:
            return 'death'
            print("You won in a fierce fight and now going to move forward")
            return 'laser_weapon_armory'

Any suggestions how to make it more simple and / or better?

Thank you in advance!

1 Like

I don’t know, that seems good enough to me really. About the only thing you should do is change the variable “round” to something else. The word “round” is already a used by python to round a number.

If you want to get fancy, you can move this into a function, and pass in a dict for the alien/player HP stats. Then you keep running this battle function until it returns “false” meaning someone died. Once that’s done just print out the winner.

1 Like