- 属性:
name 玩家的名字
blood 玩家血量
- 方法:
tong() 捅对方一刀,对方掉血10滴
kanren() 砍对方一刀,对方掉血15滴
chiyao() 吃一颗药,补血10滴
__str__ 打印玩家状态。
- 步骤
第一步 需要先去定义一个类【角色类】
第二步 创建2个【西门吹雪、叶孤城】实例化对象
#导入时间库
import time
#定义类【角色类】
class Role:
def __init__(self,name,hp):
self.name=name
self.hp=hp
pass
def tong(self,enemy):
enemy.hp-=10
info='[%s]捅了[%s]一刀'%(self.name,enemy.name)
print(info)
pass
def kanren(self,enemy):
enemy.hp-=15
info='[%s]砍了[%s]一刀'%(self.name,enemy.name)
print(info)
pass
def chiyao(self):
self.hp+=10
info='[%s]吃了补血药 增加10滴血'%(self.name)
print(info)
pass
def __str__(self):
return '%s 还剩下 %s血量'%(self.name,self.hp)
pass
#创建2个【西门吹雪、叶孤城】实例化对象
xmcx=Role('西门吹雪',100)
yygc=Role('一叶孤城',100)
while True:
if xmcx.hp<=0 or yygc.hp<=0:
break
xmcx.tong(yygc)
print(xmcx)
print(yygc)
print('*********************************')
print()
yygc.kanren(xmcx)
print(xmcx)
print(yygc)
print('*********************************')
print()
xmcx.chiyao()
print(xmcx)
print(yygc)
print('*********************************')
print()
xmcx.chiyao()
print(xmcx)
print(yygc)
print('*********************************')
print()
time.sleep(1)
pass
print('对战结束........')