Tyoh
Tyoh

Reputation: 1

How do I call upon a certain argument within a function?

So I just started learning python and I thought why not make a text-based game to improve my skills. So in this game I want to make a fighting scene where you have a class( Knight, Rogue, or Wizard) and that class is stored in player_class. Each class has three I would say attributes. Each class has a speed, damage, and health. I want to make it so that if your speed is higher or equal to the enemy you attack first, but if the enemy's speed is higher than the enemy attacks first. So I thought I could do this by just calling player_class = userKnight and then in the fight if player_class(speed) >= enemy_type(espeed) then enemy_type(health) - player_class(damage). This doesn't work and if someone has a better method or can explain how I can implement this better would be awesome!

def userRogue(damage,speed,health):
    health = 100;
    damage = 50;
    speed = 150;
    return

def userKnight(damage,speed,health):
    health = 100;
    damage = 100;
    speed = 50;
    return

def userWizard(damage,speed,health):
    health = 100;
    damage = 75;
    speed = 75;
    return

def enemyRat(ehealth, espeed, edamage):
    ehealth = 100;
    edamage = 10;
    espeed = 70;
    return

def enemyDeath():
    if ehealth <= 0:
        print("Enemy is dead!")
    return

def playerDeath():
    if health <= 0:
        print("You have died!")
    return

user_name = input("What is your name: ")

print("1:)Rogue")
print("2:)Knight")
print("3:)Wizard")

player_class_select = int(input("Choose your class: "))

player_class = 0
enemy_type = 0

if player_class_select == 1:
    print("You chose Rogue!")
    player_class = userRogue

elif player_class_select == 2:
    print("You chose Knight!")
    player_class = userKnight

elif player_class_select == 3:
    print("You chose Wizard!")
    player_class = userWizard

else:
    print("That is not a choice!")


print("Time for your first battle...")
print("A enemy has appeared!")

enemy_type = enemyRat

if player_class(speed) >= enemy_type(espeed):
    enemy_type(ehealth) - player_class(damage)
    enemyDeath()
elif player_class(speed) < enemy_type(espeed):
    player_class(health) - enemy_type(edamage);
    playerDeath()
    ```

Upvotes: 0

Views: 80

Answers (1)

Barb
Barb

Reputation: 437

You should make one parent class for your characters as seen here:

class Player:
  def __init__(self, name, damage, speed, health):
    self.name = name 
    self.damage = damage
    self.speed = speed
    self.health = health

  def __str__(self):
    return 'Name: {} | Damage: {} | Speed: {} | Health: {}'.format(self.name, self.damage, self.speed, self.health)

rogue = Player('Rogue', 100, 50, 150) # Setting attributes to the parent class
knight = Player('Knight', 100, 100, 50) # name, damage, speed, health
wizard = Player('Wizard', 100, 75, 75)
rat = Player('Rat', 100, 10, 70)

print(rogue)
print(knight)
print(wizard)
print(rat)

This creates your players and attributes:

Name: Rogue | Damage: 100 | Speed: 50 | Health: 150
Name: Knight | Damage: 100 | Speed: 100 | Health: 50
Name: Wizard | Damage: 100 | Speed: 75 | Health: 75
Name: Rat | Damage: 100 | Speed: 10 | Health: 70

Using this information is relatively easy to compare it to another value of another player/enemy:

if rogue.speed > rat.speed:
do something

Upvotes: 2

Related Questions