Record#
Today's exercise is about writing a class for game characters. Although it's not difficult, there may be some parts that might confuse you. The requirements for the exercise are as follows:
- My game needs a character, including name, health, and magic points.
- When initializing the character, these values need to be set.
- A method is needed to output this data.
- A subclass named
Player
needs to be created, which inherits from theCharacter
class and has an additional attribute for health points. - The
Player
class should also have a method to check the character's status and return "Yes" or "No". - A subclass named
Enemy
should also be created, which has additional attributes fortype
andstrength
. - The
Enemy
class should have two subclasses:Orc
, which has aspeed
attribute.Vampire
, which has aday/night
tracker attribute.
- Instantiate a
Player
object, twoVampire
objects, and threeOrc
objects. You can choose their names. - Print out their attribute values.
CODE#
class gamer:
gname = None
health = None
magic = None
def __init__(self, gname, health, magic):
self.gname = gname
self.health = health
self.magic = magic
def print(self):
print(f"{self.gname} > {self.health} > {self.magic}")
class player(gamer):
alive = None
def __init__(self, gname, health, magic, alive):
self.gname = gname
self.health = health
self.magic = magic
self.alive = alive
def alive(self):
if self.alive:
print("alive No")
return True
else:
print("alive Yes")
return True
def print(self):
print(f"{self.gname} > {self.health} > {self.magic} > {self.alive}")
class enemy(gamer):
type = None
strength = None
def __init__(self, gname, health, magic, type, strength):
self.gname = gname
self.health = health
self.magic = magic
self.type = type
self.strength = strength
def print(self):
print(
f"{self.gname} > {self.health} > {self.magic} > {self.type} > {self.strength}"
)
class attribute(enemy):
orc = "speed"
def __init__(self, gname, health, magic, type, strength, orc):
self.gname = gname
self.health = health
self.magic = magic
self.type = type
self.strength = strength
self.orc = orc
def print(self):
print(
f"{self.gname} > {self.health} > {self.magic} > {self.type} > {self.strength} > {self.orc}"
)
class tracker(enemy):
Speed = None
def __init__(self, gname, health, magic, type, strength, Speed):
self.gname = gname
self.health = health
self.magic = magic
self.type = type
self.strength = strength
self.Speed = Speed
def print(self):
print(
f"{self.gname} > {self.health} > {self.magic} > {self.type} > {self.strength} > {self.Speed}"
)
david = player("david", "100", "50", "3")
david.print()
david.alive()
Boris = attribute("Boris", "45", "70", "Vampire", "3", "Night")
Boris.print()
Rishi = attribute("Rishi", "45", "10", "Vampire", "75", "day")
Rishi.print()
Ted = tracker("Ted", "75", "40", "orc", "80", "45")
Ted.print()
Station = tracker("Station", "75", "40", "orc", "80", "50")
Station.print()
Bill = tracker("Bill", "60", "5", "orc", "75", "90")
Bill.print()