0

I am simulating a person running with an enemy chasing the person. I am struggling in finding the code for the movement of the enemy.

class Person ():
    def __init__(self, limits, name)
        self.row = 0
        self.col = 0
    
    def run(self, limits):
        #movement code etc.

I want to be able to take the self.row coordinate from the Person, and be able to use that variable in class Enemy, to determine the movement of Enemy. Throughout the simulation I'll be changing the self.row and self.col of Person so I want the Enemy to be able to follow.

class Enemy ():
    def __init__(self, limits, name)
        self.row = 0
        self.col = 0

    def run(self, limits):
        if self.row > #the Person's self.row
            self.row -= 1

How can I use the variables from class Person and use them in class Enemy to determine the movement?

Edit: If I make an object under class Person, how do I access that object, specifically each self.row or self.col from class Enemy?

7
  • To access to Person attribute, you can create a Person object person = Person(20, 30) and then get the row by using person.row Commented May 18, 2022 at 4:53
  • 1
    There are no "variables from class Person" - everything you're storing in that class is in the form of instance attributes, which are specific to each individual instance of the class. The Enemy would need to be given a specific Person instance whose location would direct its movement. Commented May 18, 2022 at 4:54
  • Don't have Person and Enemy do this kind of computation. They don't need to know about each other. You need a function (or a "Battlefield" class) to do this which uses both types of objects and understands how they interact. Commented May 18, 2022 at 5:20
  • @TimRoberts would I make the Battlefield class the parent class or another completely independent one? Commented May 18, 2022 at 5:24
  • A Person is not a kind of Battlefield, or vice versa. Battlefield would CONTAIN Person and Enemy objects. Commented May 18, 2022 at 5:32

0

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.