Skip to main content

Item() rundown

 The item() class holds the information of the item and also runs the item effect when it gets used. Constructor def __init__ ( self , command , name , cost , usage): self .effect = command self .name = name self .cost = cost self .usage = usage The effect holds the string name of the effect for the item,  usage holds the value of the Effect . The rest are self-explanatory. EffectHandler() def effectHandler ( self , user: PlayableCharacter , equip= True ): #learned how to specify type of parameter. if equip: if self .effect == "Def" : user.defense = user.defense + self .usage elif self .effect == "AtkPhy" or self .effect == "AtkMag" : user.attack = user.attack + self .usage else : if self .effect == "hp" : user = self .healing(user) return user This handles the effects of the item when it is used. If equip is true then it will check for the equip...

Combat.baseAttack()

 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

Popular posts from this blog

Shop() run down

 This class is responsible for running the shop and creating the Item() objects. Constructor: def __init__ ( self ): self .items = [] f = open ( "items.csv" ) fReader = csv.reader(f) for line in fReader: if line == []: continue self .items.append(Item(line[ 2 ] , line[ 0 ] , int (line[ 1 ]) , int (line[ 3 ]))) f.close() When the class is called the class list items are filled with Item() objects created by opening items.csv and taking the data in there to instantiate the items. Then we close the file. WhoIsShopping(): def whoIsShopping ( self , squad : list ): counter = 0 choices = [] print ( "Who is shopping?" ) for member in squad: if member.level > 0 : print ( str (counter) + ": " + member.displayName) choices.append(counter) counter += 1 selection = inputAndCheck( "Selection: " , choices) return squad[selection] T...

Item() rundown

 The item() class holds the information of the item and also runs the item effect when it gets used. Constructor def __init__ ( self , command , name , cost , usage): self .effect = command self .name = name self .cost = cost self .usage = usage The effect holds the string name of the effect for the item,  usage holds the value of the Effect . The rest are self-explanatory. EffectHandler() def effectHandler ( self , user: PlayableCharacter , equip= True ): #learned how to specify type of parameter. if equip: if self .effect == "Def" : user.defense = user.defense + self .usage elif self .effect == "AtkPhy" or self .effect == "AtkMag" : user.attack = user.attack + self .usage else : if self .effect == "hp" : user = self .healing(user) return user This handles the effects of the item when it is used. If equip is true then it will check for the equip...

combat.combatEndCheck()

def combatEndCheck ( self ): removalList = [] follow = False playerCounter = 0 for figther in self .listing: removeFlag = self .listing[figther].deathCheck() if removeFlag == True : removalList.append(figther) i = 0 for thing in self .order: if thing[ 0 ] == figther: break i += 1 self .order.pop(i) continue if isinstance ( self .listing[figther] , Monster): follow = True if isinstance ( self .listing[figther] , PlayableCharacter) and self .listing[figther].currentHealth > 0 : playerCounter += 1 for body in removalList: self .exp = self .exp + self .listing[body].experience self .listing.pop(body) if playerCounter <= 0 : follow = False return follow This is the final function we need to discuss for Combat() . It takes no parameters and it returns bool depending o...