二手产品经理

二手产品经理

THIS IS RENO

oop - 65天 - 在线学习python100天

记录#

今天的练习是关于编写游戏人物的类。虽然不算难,但是有些地方可能会让您感到困惑。练习的要求如下:

  1. 我的游戏需要一个角色,包括姓名、生命值和魔法值。
  2. 在初始化角色时,需要设置这些值。
  3. 需要一个方法来输出这些数据。
  4. 需要创建一个名为Player的子类,它继承自Character类,并且还有一个额外的生命值属性。
  5. Player类还应该有一个方法来检查角色的状态,并返回 “是” 或 “否”。
  6. 还应该创建一个名为Enemy的子类,它有额外的类型力量属性。
  7. Enemy类应该有两个子类:
    1. Orc,它有一个速度属性。
    2. Vampire,它有一个白天/黑夜跟踪器属性。
  8. 实例化一个Player对象、两个Vampire对象和三个Orc对象。您可以选择它们的姓名。
  9. 打印出它们的属性值。

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()


加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。