PythonnLearner
PythonnLearner

Reputation: 57

Pokemon battle simulator, but it doesn't call the function in the class

So I was trying to simulate a fight in pokemon, but when I tried to run it, it doesn't call the function and says that the function is not defined. I don't get what's causing the problem so any help will be very nice.

class:

    import random
    class Pokemon:

      def __init__(self, hindex, hpokemon, htype, hhp, hattack, hspattack, hdeff, hspdeff, hmove):
          self.index = hindex
          self.name = hpokemon
          self.type = htype
          self.hp = hhp
          self.attack = hattack
          self.spattack = hspattack
          self.deff = hdeff
          self.spdeff = hspdeff
          self.move = hmove
      def attack(self, other):
          a = random.randint(1,2,3)
          if a == 1:
              damage = 0*(0.3*(self.attack/other.deff)*self.move)
          elif a == 2:
              damage = 0.3*(self.attack/other.deff)*self.move
          elif a == 3:
              damage = 2*(0.3*(self.attack/other.deff)*self.move)
          health = other.hp - damage
          return health
        
    Bulbasaur = Pokemon('001', 'Bulbasaur', 'Plant', 45, 49, 65, 49, 65, 40)
    Ivysaur = Pokemon('002', 'Ivysaur', 'Plant', 60, 62, 80, 63, 80, 40)
    Venusaur = Pokemon('003', 'Venusaur', 'Plant', 80, 82, 100, 83, 100, 40)

    Squirtle = Pokemon('004', 'Squirtle', 'Water', 44, 48, 50, 65, 64, 40)
    Wartoise = Pokemon('005', 'Wartoise', 'Water', 59, 63, 65, 80, 80, 40)
    Blastoise = Pokemon('006', 'Blastoise', 'Water', 79, 83, 85, 100, 105, 40)

    Charmander = Pokemon('007', 'Charmander', 'Fire', 39, 52, 60, 43, 50, 40)
    Charmeleon = Pokemon('008', 'Charmeleon', 'Fire', 58, 64, 80, 58, 65, 40)
    Charizard = Pokemon('009', 'Charizard', 'Fire', 78, 84, 109, 78, 85, 40)

    Pikachu = Pokemon('010', 'Pikachu', 'Electric', 35, 55, 50, 40, 50, 40)
    Magnemite = Pokemon('011', 'Magnemite', 'Electric', 25, 35, 95, 70, 55, 40)
    Ampharos = Pokemon('012', 'Ampharos', 'Electric', 90, 75, 115, 85, 90, 40)

    Snorlax = Pokemon('013', 'Bulbasaur', 'Normal', 45, 49, 65, 49, 65, 40)
    Lickylicky = Pokemon('014', 'Bulbasaur', 'Normal', 45, 49, 65, 49, 65, 40)
    Jigglypuff = Pokemon('015', 'Bulbasaur', 'Normal', 45, 49, 65, 49, 65, 40)
     
    pokemon = [Bulbasaur, Ivysaur, Venusaur, Squirtle, Wartoise, Blastoise, Charmander, Charmeleon, Charizard, Pikachu, Magnemite, Ampharos, Snorlax, Lickylicky, Jigglypuff]

code:

    def iichoice(pokemon):
       print("Pseudo-dex Opened.")
       print("What would you like to do?")
       choice = input("Your Choice: ")
       print("")
        
       if choice == '1':
           attack(pokemon[1], pokemon[2])

Upvotes: 0

Views: 130

Answers (1)

Daniyal Ishfaq
Daniyal Ishfaq

Reputation: 247

The function attack is defined inside the class Pokemon. You need to call this function from an object of Pokemon class and give the other pokemon as argument to that function.

Bulbasaur = Pokemon('001', 'Bulbasaur', 'Plant', 45, 49, 65, 49, 65, 40)
Ivysaur = Pokemon('002', 'Ivysaur', 'Plant', 60, 62, 80, 63, 80, 40)
Bulbasaur.attack(Ivysaur)

Upvotes: 3

Related Questions