Hey I’ve been learning python for about 2 months and classes are something I’m not fully understanding, I’m doing this project:
https://www.codecademy.com/journeys/computer-science/paths/cscj-22-intro-to-programming/tracks/cscj-22-basic-python-data-structures-and-objects/modules/cscj-22-python-object-oriented-programming/projects/create-a-game-using-classes-and-objects
In the computer science course, I’ve been having trouble trying figuring out how two separate classes are supposed to interact with each other. I’m making a Zombie vs Alien game and can’t figure out how i would have them attack each other. In the example and lesson it has another instance in the class that interacts with each other but not two separate classes interacting
class Zombie:
def __init__(self, name, strength, speed, size):
self.name = name
self.strength = strength
self.speed = speed
self.size = size
self.health = self.strength + self.speed + self.size
self.is_dead = False
def lose_health(self):
self.health -= 2
print('{} has lost 2 health and now has {} health'.format(self.name, self.health))
if self.health <= 0:
self.is_dead = True
print('{} has died'.format(self.name))
def attack(self):
if (self.strength + self.speed) > (Alien.self.strength + Alien.self.speed):
Alien.lose_health()
class Alien:
def __init__(self, name, strength, speed, size):
self.name = name
self.strength = strength
self.speed = speed
self.size = size
self.health = self.strength + self.speed + self.size
self.is_dead = False
def lose_health(self):
self.health -= 2
print('{} has lost 2 health and now has {} health'.format(self.name, self.health))
if self.health <= 0:
self.is_dead = True
print('{} has died'.format(self.name))
zombie = Zombie('Terry', 7, 5, 2)
alien = Alien('Marion', 5, 3, 3)
zombie.attack(alien)
I know its not a lot but this is what I have, I didn’t think it would work especially:
(Alien.self.strength + Alien.self.speed)
There may be an easy solution and I don’t get it, Any help or guidance would be appreciated, classes seem very important to understand in python