user12291970
user12291970

Reputation:

How can i access a variable from one class and use it in another class?

Heres my first class

 class snake:  
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.change_x = 0
        self.change_y = 0

Heres my second class

 class food:
    def __init__(self, food_x, food_y, food_width, food_height):
        self.x = food_x
        self.y = food_y
        self.width = food_width
        self.height = food_height
    def collition_detection(self):
        pass

I want to be able to access the position (x, y) form snake class to food class so that when i make a "collition_detection" function in food class it wont throw an error at me saying "self.x" is not defined. Thanks.

Upvotes: 3

Views: 97

Answers (3)

Julian Herold
Julian Herold

Reputation: 69

A simple fix would be to make the position a class variable (shared between all instances of snake). You achieve this by simply declaring it as:

class snake:
    common_x = None
    common_y = None
    def __init__(self, x, y, width, height):
        common_x = x
        common_y = y
        self.width = width
        self.height = height

These variables are shared between all instances of snake and can be accessed via snake.common_x or snake1 = snake(args); snake1.common_x

This answers your question of how to access these variables from another class. Having said so, I would suggest you don't do it this way, since you might run into problems later on. Firstly, this allows you to only have one snake in your game. This can be fixed by having a list of positions in your class, but I still don't consider this an elegant approach. I think it would be best to have another class, e.g. 'game' which keeps track of all your game objects. This class would have a list of snakes, food, enemies (?), whatever you want to add. You can then connect all your elements to a game instance and that's how they'll be able to communicate.

class game:
    def __init__(snakes=[], food=[]):
        self.snakes = snakes
        self.food = food

class snake:
    def __init__(x,y,game):
        self.x = x
        self.y = y
        self.game = game
        self.game.snakes.append(self)

class food:
    def __init__(self,...):
        ....

    def collide(self):
        for snake in self.game.snakes:
            if self.x == snake.x and self.y == snake.y:
                # do collision handling

This is just an example on how to approach this, I'm not sure, if having collision handling done by the food object is really the best approach, but I guess you get the idea.

Upvotes: 1

NicholasM
NicholasM

Reputation: 4673

You can take a parameter in the method:

 class food:
    def __init__(self, food_x, food_y, food_width, food_height):
        self.x = food_x
        self.y = food_y
        self.width = food_width
        self.height = food_height

    def collition_detection(self, s):
        """ Return whether this food particle collides with the object `s`. """
        delta_x = math.abs(self.x - s.x)
        delta_y = math.abs(self.y - s.y)
        return (delta_x < (self.width + s.width) / 2) and (delta_y < (self.height + s.height) / 2)

A better approach might be to define a function that works for any two objects. This way, you can extend the functionality to any classes with attributes x, y, height, width.

def is_collision(obj1, obj2):
    """ Return whether obj1 and obj2 collide. """
    delta_x = math.abs(obj1.x - obj2.x)
    delta_y = math.abs(obj1.y - obj2.y)
    return (delta_x < (obj1.width + obj2.width) / 2) and (delta_y < (obj1.height + obj2.height) / 2)

You could then call it as follows:

s = Snake(...)
f_particle = food(...)

is_collision(s, f_particle)

Upvotes: 2

Kalma
Kalma

Reputation: 147

You have to instance the class. Then you can use its methods and functions, but not its variables. Create a function within class that returns what you need, instance that class from within the other (or main program) and get the values returned.

Here goes an example:

class A:
    def m(self, x, y):
        return x + y

class B:
    def __init__(self):
        self.a = A()

    def call_a(self):
        a = self.a.m(1, 2)
        return a

c = B()
result = c.call_a()
print(result)

Upvotes: 1

Related Questions