Build the Battle Loop

STEP 4 β€” Build the Battle Loop

Move. Heal. Item. Panic. Repeat.

This loop keeps the game running until:

  • The enemy dies
  • The player dies
  • OR the player runs away like a sensible human
To QUIT the game, type 4.
battle_running = True

while battle_running:
    print("\nYour HP:", player["health"], "| Enemy HP:", enemy["health"])
    print("Choose:")
    print("1. Attack")
    print("2. Heal")
    print("3. Use Item")
    print("4. Quit")

    choice = input("> ")

    if choice == "1":
        attack(player, enemy)
    elif choice == "2":
        heal(player)
    elif choice == "3":
        use_item(player, inventory)
    elif choice == "4":
        print("You ran away!")
        break
    else:
        print("Invalid choice!")
        continue

    if enemy["health"] <= 0:
        print("You win!")
        break

    attack(enemy, player)

    if player["health"] <= 0:
        print("You lose!")
        break


Explanation:

  • The loop prints the current HP
  • The player chooses an action
  • The game runs that action
  • The enemy attacks back
  • The loop checks for win/lose
  • The loop continues until someone falls over

This is the core of every turn-based RPG ever made.

Jupyter Notebook - Step 4: Final Battle Loop
[* ] Engine ready. Paste your battle loop and click Run Cell.

Choose option press enter

>