This is the basic command that is run for all monster attacks.
Parameters
It simply takes the current monster as a parameter named attacker.
The rundown
def baseAttack(self, attacker):
targetingNum = int(random.random() * 100)
while targetingNum < 1:
targetingNum = int(random.random() * 100)
targets = []
I start by generating a random number into targetingNum and ensuring it’s greater than 1.
for fighter in self.listing:
if isinstance(self.listing[fighter], PlayableCharacter):
targets.append(fighter)
Then I add all the available playable characters a monster can attack using a loop that iterates over the listing. Those characters’ names are added to targets.
if self.taunt == []:
dividers = [81, 61, 41, 21]
else:
dividers = []
counter = 100
for peep in targets:
if peep == self.taunt[0][0]:
counter -= 34
dividers.append(counter)
else:
counter -= 14
dividers.append(counter)
If there is nothing in taunt, then I simply set up some evenly separated numbers for our characters in dividers. Otherwise, I go through the targets and compare those names to the name found in the taunt. If there is a match the match will receive a bigger number in divider than the other characters making it more likely for them to be hit.
flag = False
for divider in dividers:
if flag:
break
if targetingNum > divider:
target = targets[dividers.index(divider)]
flag = True
Now I set up an indicator bool called flag for another loop. This loop checks each number of the divider to see if it was less than the targetingNum. How this works is that each character in targets has a corresponding number (at the same index) in dividers and the numbers are decreasing as you iterate through dividers. This makes it so that I only need to check greater than (because all the unwanted numbers higher than previous ones have already been checked), and, using the index of the number, I can quickly set target to the corresponding name of the target. Once this target is found the flag is set to true so it can break us out of the loop.
try:
if target not in self.protected:
if self.listing[target].level > 0 and self.listing[target].currentHealth > 0:
self.attackDispaly(attacker, target)
if (attacker.attack - self.listing[target].defense >= 0):
self.listing[target].currentHealth = self.listing[target].currentHealth - attacker.attack + self.listing[target].defense
else:
self.listing[target].currentHealth = self.listing[target].currentHealth - 1
except UnboundLocalError:
pass
Should a target fail to be selected, the first lines of
the code that are in the try statement will return a UnboundLocalError.,
so I added an exception that will do nothing. I did this knowing that if a
specific target wasn’t selected, then the monster wasn’t lucky and their attack
missed all the characters. If there is a specifically selected target name in target,
we first check to make sure they aren’t protected then we apply the damage by
taking attack of the attacker and subtracting it from the defender’s
defense. If the result for that subtraction is grater than zero than subtract
it from the target’s currentHealth. If the result is less than zero,
then we subtract 1 form the defender’s currentHealth.
Comments
Post a Comment