Now finally I can do the beloved turn(), this function executes a turn for the specified character.
Local Variables and Parameters
def turn(self, ally):
choices = []
chosen = 0
i = 0
Ally, must be a character() object. Choices are a list where the options will be stored for when I call inputandCheck(). Chosen is where the returned value of input and check will be stored. i is used as a variable for counting through my loops.
The rundown
if isinstance(ally, Monster):
print("Turn: " + ally.displayName)
self.baseAttack(ally)
First, I right away check if the ally is a monster and if it is, I display that it’s the monster’s turn and then have it call for a baseAttack().
else:
if ally.currentHealth > 0:
try:
print("up next:" + self.intiativeDisplay(ally.displayName))
print("Current turn: " + self.quickdisplay(ally))
if ally.displayName not in self.protected:
for skill in ally.skills:
if ally.skillCosts[i] <= ally.currentEnergy:
print(str(i) + ": " + skill)
choices.append(i)
i += 1
protect = i
if self.saftey():
print(str(protect)+ ": Fall back")
choices.append(i)
chosen = inputAndCheck("What will " + ally.displayName + " do? ", choices)
if chosen == protect:
self.saftey(availability= False, Ally= ally.displayName)
else:
ally.currentEnergy = ally.currentEnergy - ally.skillCosts[chosen]
self.skillHandler(ally, ally.skills[chosen])
Else, I assume the ally is a PlayableCharacter() and I check if they have more than zero hp. The try block is there just in case a nonplayable character gets through. Then I display important info like who’s turn it is and who’s coming up next using the display functions in the class.
If they aren’t protected, I use a for loop to check the energy cost of the character’s skills with the character’s current energy. If the character has enough energy I display the skill as an option and add the current i value to choices. I also use i in this loop to create an additional option for my fallback gimmick. If there isn’t anyone already in the back line and the ally isn’t the last PlayableCharacter() standing (self.saftey()) then I display falling back as an option.
chosen = inputAndCheck("What will " + ally.displayName + " do? ", choices)
if chosen == protect:
self.saftey(availability= False, Ally= ally.displayName)
else:
ally.currentEnergy = ally.currentEnergy - ally.skillCosts[chosen]
self.skillHandler(ally, ally.skills[chosen])
Now that the options are displayed, I call input and check for the player input via inputAndCheck(). And store it in chosen. If the protected value was chosen then I put the character in the back line by adding them to the protected class variable (via self.saftey()). Otherwise, I use the chosen value to get the cost of the skill and subtract it from the characters total and fetch the name of the skill so I can plug it into the skillHandler() to execute the skill’s effect.
else:
choices = [0,1]
print("0: Stay back")
print("1: go back to front")
chosen = inputAndCheck("What will " + ally.displayName + " do? ", choices)
if chosen == 0:
self.safteyRecovery(ally.displayName)
else:
self.saftey(Ally= ally.displayName, Removal= True)
If they were protected earlier, then they only have two options: Leave the back line or stay back. I display those options on the screen and pass an inputAndCheck(). If they stay in the back (option 0) then I gave them some healing and energy generation (self.safteyRecovery()). If they go back (option 1) they get removed from the protected list.
counter = -1
for defender in self.defRaise:
counter += 1
if defender[0] == ally.displayName:
defender[1] = defender[1] - 1
if defender[1] <= 0:
ally.defense = ally.defense - defender[2]
break
try:
self.defRaise.pop(counter)
except IndexError:
pass
counter = -1
for attacker in self.atkRaise:
counter += 1
if attacker[0] == ally.displayName:
attacker[1] = attacker[1] - 1
if attacker[1] <= 0:
ally.attack = ally.attack - attacker[2]
break
try:
self.atkRaise.pop(counter)
except IndexError:
pass
counter = -1
for annoyance in self.taunt:
counter += 1
if annoyance[0] == ally.displayName:
annoyance[1] = annoyance[1] - 1
if annoyance[1] <= 0:
break
try:
self.taunt.pop(counter)
except IndexError:
pass
These last couple of loops are to tick down any passive effects on the character, so it checks all the listings and if the character name is not found there then it moves on. If it does find a match I tick the count of the effect down by one if it goes down to zero, I remove the effect from the character. Counter here is to help keep track of the index so I can remove them from the list easily after the loop finishes. The reason I wait until after the loop to remove things is that python doesn’t like it when you try to change a list or dictionary while iterating over it at the same time.
This is the most central portion of this class. as you
can see it calls a lot of other functions to make it do what it does. I’ll be
going over all the display functions next, then I will dissect the remaining 6
big ones: targetChooser(),skillHandler(), baseAttack(), safety(),
and safetyRecovery(), combatEndCheck().
Comments
Post a Comment