1

Fantasy Battle Game Flowchart

I have been working on this fantasy battle game which is working as expected although the while True loop that starts the game at the very end isn't iterating. I tried using continue with no success. I have included a flow chart to visually show the logic of the game. I'm not getting any error, the loop which should be infinite is just stopping at the end without starting over. Hoe can I make it iterate until it reaches one of the break?

"""
Fantasy Battle Game
Player against Dragon
"""

# Player
wizard = "Wizard"
elf = "Elf"
human = "Human"

# Player health
wizard_hp = 70
elf_hp = 100
human_hp = 150

# Player damage force
wizard_damage = 150
elf_damage = 100
human_damage = 20

# Dragon health and damage force
dragon_hp = 300
dragon_damage = 50

# Print the list of characters
print(wizard)
print(elf)
print(human)

# Input function to choose player
character = input("Choose your character: ")

# While loop to represent player profile
while True:
    if character == "Wizard":
        my_hp = wizard_hp
        my_damage = wizard_damage
        break
    elif character == "Elf":
        my_hp = elf_hp
        my_damage = elf_damage
        break
    elif character == "Human":
        my_hp = human_hp
        my_dammage = human_damage
        break
    else:
        print("Unknown Character")
        break

# Print player selection
print(character)

# print player health:
print(my_hp)

# print player damage force:
print(my_damage)

# Start game
while True:
    # Player start first battle against Dragon
    dragon_hp = dragon_hp - my_damage
    
    # If dragon health is positive show remaining health
    if dragon_hp > 0:
        print(f'{character} damaged the dragon!')
        print(f'The Dragon hitpoints are now {dragon_hp}')
    
    # If dragon health is negative or null - game over
    elif dragon_hp <= 0:
        break
        print(f'The Dragon lost the battle!')
        
    # Dragon start second battle against player
    my_hp = my_hp - dragon_damage
    
    # If player health is positive show remaining health
    if my_hp > 0:
        print(f'The Dragon strikes back at {character}')
        print(f'The {character} hitpoints are now {my_hp}')
        
    # If player health is negative - game over
    elif my_hp <= 0:
        break
        print(f'The {character} lost the battle!')
2
  • 2
    put the print statement before the break so you can see when it breaks out. Commented Jan 9, 2022 at 5:17
  • 2
    The first while loop is totally useless -- it breaks after every branch and so will never actually loop. You might as well remove it. In the second loop, if you want it to run based on some condition, you can put that as the while condition instead of True: while dragon_hp > 0 and my_hp >0 Commented Jan 9, 2022 at 5:19

1 Answer 1

4

Your main problem is that you are placing break statements before the print statements, meaning the while loop stops before it reaches the print statements.

e.g:

    # If dragon health is negative or null - game over
    elif dragon_hp <= 0:
        break #Break should not be here
        print(f'The Dragon lost the battle!')
    # If player health is negative - game over
    elif my_hp <= 0:
        break #Break should not be here
        print(f'The {character} lost the battle!')

Instead, try:

    # If dragon health is negative or null - game over
    elif dragon_hp <= 0:
        print(f'The Dragon lost the battle!')
        break
    # If player health is negative - game over
    elif my_hp <= 0:
        print(f'The {character} lost the battle!')
        break

Also, one tip: you should be using classes for your characters, as it is easier to manage classes. It keeps the characters and their variables together, so you don't have to constantly make more than 1 variable when creating a new character.

An example for your characters:

class character:
    def __init__(self,name,damg=0,hp=0):
        self.damage=damg
        self.hp=hp
        self.name=name

You would then create a new character like:

wizard=character("Wizard",150,70)

Call on each attribute like:

wizard.hp
# returns 70
wizard.name
# returns "Wizard"
wizard.damage
# returns 150

If you keep the characters like this, it means you can run more efficient code, for example I would just out the new character objects in a list, and then put in an attack for loop that iterates over the list of characters and prints out the damage they did, their health, and their name. E.g.

character_list=[wizard,elf,human] #some imaginary list
for x in character_list:
    print(f"{x.name} attacked the dragon!")
    print(f"{x.damage} was dealt.")
    print(f"{x.name} has {x.hp} left")

Also, you can edit these as normal. e.g., take our wizard from above:

# oh no! some event caused your wizard to lose health!
wizard.hp-=10
# returns 60 when called again

I hope that was a satisfactory explanation.

Sign up to request clarification or add additional context in comments.

4 Comments

Such a futile mistake! Thank you!
@elideli you should read my post again... More info on how to improve your code!
That is a really good answer! Thank you for your time!
@elideli no problem, glad to be of some help to you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.