Learn to Code via Tutorials on Repl.it

← Back to all posts
Turn-Based Combat Intro
HarshGupta6 (0)

Intro

Hello everyone! Today, we are going to be learning about turn based combat systems.

What are the players?

First off, what is our hero and villain? For my example, it is Mario and Randy respectfully.
However, you can choose any hero or villain you want.

Important Modules

First, import these modules. They will be important in a second.

from random import *
from time import *

HP

You need to set HP for both players to get started. Add this to your code.

mario = {
    'currentHP' : 300,
    'maxHP' : 300
}
randy = {
    'currentHP' : 1000,
    'maxHP' : 1000
}

Items

You also need items in case Mario's in trouble. Add this to you mario dictionary.

'items' : {
    'Max Mushrooms' : 10
  }

(Note: These numbers were for my own taste. You can change the numbers if you want.)

Game Loop

We want to keep fighting until either of the two are down. Add this to you code.

while mario['currentHP'] > 0 and randy['currentHP'] > 0:

Choices

Mario will need to do something! Add this to your while loop.

  # Select a option for Mario:
  
  print("Mario's Turn:")
  print("Options:")
  print("1. Jump")
  print("2. Hammer")
  print("3. Fire Hand")
  mario_option = int(input("Which option do you chose? "))
  
  # Error Handling

  while mario_option not in [1, 2, 3]:
    mario_option = int(input("Nope. Input again: "))

Commands

The code should include the algorithm for attacks! Add this to your loop.

  # Which option is it?

  if mario_option == 1:
    randy_damage_for_jump = randint(40, 70)
    print("Wow! Jump did " + str(randy_damage_for_jump) + " damage!")
    randy['currentHP'] -= int(randy_damage_for_jump)
  elif mario_option == 2:
    randy_damage_for_hammer = randint(50, 60)
    print("Wow! Hammer did " + str(randy_damage_for_hammer) + " damage!")
    randy['currentHP'] -= int(randy_damage_for_hammer)
  elif mario_option == 3:
    randy_damage_for_fire_hand = randint(30, 90)
    print("Wow! Fire Hand did " + str(randy_damage_for_fire_hand) + " damage!")
    randy['currentHP'] -= int(randy_damage_for_fire_hand)

  # If Randy already died

  overKillOnRandy()

  if randy['currentHP'] == 0:
    break
  
  # Randy's Turn up next

  sleep(0.75)
  print("")

AI's Turn and Stats

The villain should also get a turn in battle and the stats should get displayed every turn. Add this to your loop.

  # Randy radomly selects an option

  print("Randy's Turn:")
  sleep(0.75)
  
  randy_choice = randint(1,2)
  
  # Which option is it?

  mario_damage_for_piggy_pile = randint(20, 60)
  mario_damage_for_multi_punch = randint(30, 50)
  
  if randy_choice == 1:
    print("Randy chose the piggy pile!")
    sleep(0.75)
    print("Wow! The piggy pile did " + str(mario_damage_for_piggy_pile) + " damage!")
    mario['currentHP'] -= int(mario_damage_for_piggy_pile)
  elif randy_choice == 2:
    print("Randy chose the multi-punch!")
    sleep(0.75)
    print("Wow! The multi-punch did " + str(mario_damage_for_multi_punch) + " damage!")
    mario['currentHP'] -= int(mario_damage_for_multi_punch)
  
  # Stats displayed

  sleep(0.75)
  overKillOnMario()
  
  print("")
  print("Mario has " + str(mario['currentHP']) + " HP left.")
  print("Randy has " + str(randy['currentHP']) + " HP left.")
  print("")
  sleep(0.75)

Endgame

Did we win or lose? That depends. Add this outside your loop.

# Results of Game

if randy['currentHP'] == 0:
  print("Mario has " + str(mario['currentHP']) + " HP left.")
  print("Randy has " + str(randy['currentHP']) + " HP left.")
  print("Mario wins and saves the day!")
elif mario['currentHP'] == 0:
  print("Randy beaten Mario up! Game Over!")

Conclusion and Variants

I hope this helps with your turn-based combat games! Here are some variants:

  • Add a healing system!
  • Add a SP system for special attacks!
  • Add a turn counter!
  • Add more heroes and villains to the game!
  • Add a play again and difficulty part of the game!
    Well, that wraps up this tutorial. I hope you have a great day! Until next time!