二手产品经理

二手产品经理

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("大衛", "100", "50", "3")
david.print()
david.alive()
Boris = attribute("鮑里斯", "45", "70", "吸血鬼", "3", "夜晚")
Boris.print()
Rishi = attribute("里西", "45", "10", "吸血鬼", "75", "白天")
Rishi.print()
Ted = tracker("泰德", "75", "40", "獸人", "80", "45")
Ted.print()
Station = tracker("斯泰森", "75", "40", "獸人", "80", "50")
Station.print()
Bill = tracker("比爾", "60", "5", "獸人", "75", "90")
Bill.print()


載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。