二手产品经理

二手产品经理

THIS IS RENO

oop - 65 days - Online learning python for 100 days

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:

  1. My game needs a character, including name, health, and magic points.
  2. When initializing the character, these values need to be set.
  3. A method is needed to output this data.
  4. A subclass named Player needs to be created, which inherits from the Character class and has an additional attribute for health points.
  5. The Player class should also have a method to check the character's status and return "Yes" or "No".
  6. A subclass named Enemy should also be created, which has additional attributes for type and strength.
  7. The Enemy class should have two subclasses:
    1. Orc, which has a speed attribute.
    2. Vampire, which has a day/night tracker attribute.
  8. Instantiate a Player object, two Vampire objects, and three Orc objects. You can choose their names.
  9. 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()


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.