1

I am relatively new to Python. I use Python 2.7, and I am making a turn based battle game. I am struggling with a problem currently--if you are familiar with Pokemon you may be able to relate to this easily.

So I have a loop, that allows the user to attack, then subsequently, the enemy (computer-controlled) will attack the user. In my game, the attacks (governed by a class) each have their own accuracy level.. I want to create attacks that will change the accuracy for a certain number of iterations. In order to achieve this, I need to figure out how to change a variable for n number of turns. Here is some code that represents what I want to do, simplified:

accuracy = 0
while True: 
    attack = raw_input('What attack shall you choose?')
    if attack == 'boost accuracy':
        #accuracy = 100 for 3 iterations, then returns back to 0

    #enemy does their attack

Note: I'm using a while True:loop because each monster has a health variable as well, and when that equals 0, or the monster is "dead", it breaks out of the loop.

I've tried using a turn = 0 (outside of loop), turn += 1 (inside of loop) type incrementation to fiddle around with this, but I can't seem to get it right. I feel as though this should be a rather simple solution and I am making it out to be much harder than it is. So, any ideas on how to change a variable for a certain number of iterations, then have it return to its original value?

3
  • What went wrong with the turn += 1 implementation? This seems like it would achieve what you're asking here. Commented Mar 11, 2015 at 18:57
  • The problem I was having is that if attack didn't equal 'boost accuracy', meaning I used a different attack, the accuracy would return to 0. Commented Mar 11, 2015 at 19:02
  • I've edited my answer to make it more useful. Commented Mar 11, 2015 at 19:31

1 Answer 1

2

Something like this will do.

boost_turns = 0
while True: 
    attack = raw_input('What attack shall you choose?')
    if attack == 'boost accuracy':
        boost_turns = 3
    if boost_turns:
        accuracy = 100
        boost_turns -= 1
    else:
        accuracy = 0
    # attack 

But I don't really like how this code looks. Your task looks like a great case for OOP. You should create a Pokemon class and, maybe, subclass it for each monster in case they are supposed to have different properties. This is a basic template

class Pokemon(object):
    def __init__(self, default_accuracy=0):
        self._default_accuracy = default_accuracy
        self._accuracy = default_accuracy
        self._boosted_turns = 0
        self._hp = 100

    def boost(self, turns=3, boost=100):
        self._boosted_turns = turns
        self._accuracy = boost

    def _spend_boost(self):
        self._boosted_turns -= 1

    def _reset_accuracy(self):
        self._accuracy = self._default_accuracy

    def get_damaged(self, damage):
        self._hp = (self._hp - damage > 0 and self._hp - damage) or 0

    def perform_attack(self, Pokemon, attack):
        if attack == 'boost accuracy':
            self.boost()
        if self._boost_turns:
            self._spend_boost()
        else:
            self._reset_accuracy()
        # specify attack behaviour 
Sign up to request clarification or add additional context in comments.

4 Comments

Hard mode: what if the pokemon has multiple moves that boost different stats? What if it can use two different boosting moves on consecutive turns, so their windows of effect overlap but don't perfectly coincide? What if the same boosting move can be used more than once for a cumulatively improving bonus, and it needs to gradually decrease as the time runs out for each instance? If you have one move that increases accuracy by 10, and one move that halves accuracy, how can you implement both so that the value returns to normal appropriately when both run out?
(btw I'm more interested in just getting the OP to think about these hairy complications, than actually getting Eli to address each one)
@Kevin that was the main reason to propose an OOP solution. It's more flexible. The OP can add/combine a couple of methods to cover various situations without actually doing much coding.
I made the question simplified so I could get a terse answer. I actually have implemented a class of various "monsters", an Attack class which uses a random range to determine damage, and is combined with a function to determine if it actually hits. I am now trying to expand the Attack class so that it can do many different types of things other than simply inflicted damage on the enemy, which is denoted by a "special" attribute in my Attack init function. So if "special" == "sleep", enemy cannot attack for 2 turns, if "special" == "confuse" enemy will have chance of hurting self etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.