**
**
原代码:
import random
class Player:
# 玩家需要三个属性:名字,血量,伤害
def init(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
# 攻击方法
def attack(self, monster):
monster.hurt(self)
# 挨打方法
def hurt(self, monster):
self.hp -= monster.damage
if self.hp > 0:
print("{2}当前攻击造成的伤害为{3},"
"{0}掉血量为{3},"
"{0}剩余血量为{1}"
.format(self.name, self.hp,
monster.name, monster.damage))
else:
self.hp = 0
print("{2}当前攻击造成的伤害为{3},"
"{0}掉血量为{3},"
"{0}剩余血量为{1}"
.format(self.name, self.hp,
monster.name, monster.damage))
self.death()
# 死亡方法
def death(self):
print("{}真菜,游戏失败".format(self.name))
class Monster:
# 野怪需要三个属性:名字,血量,伤害
def init(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
# 攻击方法
def attack(self, player):
player.hurt(self)
# 挨打方法
def hurt(self, player):
self.hp -= player.damage
if self.hp > 0:
print("{2}当前攻击造成的伤害为{3},"
"{0}掉血量为{3},"
"{0}剩余血量为{1}"
.format(self.name, self.hp,
player.name, player.damage))
else:
self.hp = 0
print("{2}当前攻击造成的伤害为{3},"
"{0}掉血量为{3},"
"{0}剩余血量为{1}"
.format(self.name, self.hp,
player.name, player.damage))
self.death()
# 死亡方法
def death(self):
print("别想太多,主要是{}太简单,游戏胜利".format(self.name))
print(“游戏开始!”)
player = Player(“黑猫警长”, 2000, 200)
monster = Monster(“一只耳”, 3500, 30)
switch = True
while switch:
# 提示用户输入“p”,判断是否为“p”
p = input(“按下‘p’开始回合”)
if p == ‘p’:
# 利用循环随机次数(1-5)
for i in range(random.randrange(1, 6)):
# 玩家攻击野怪
player.attack(monster)
# 判断野怪是否死亡
if monster.hp <= 0:
# 随机掉落装备
r = random.randrange(0, 2)
if r:
print("{}掉落了另一只耳朵".format(monster.name))
# 跳出循环,打印“游戏胜利”
break
if monster.hp <= 0:
break
# 利用循环随机次数(1-2)
for i in range(random.randrange(1, 3)):
# 野怪攻击玩家
monster.attack(player)
# 判断玩家是否死亡
if player.hp <= 0:
# 如果死亡,跳出循环,打印“游戏失败”
break
if player.hp <= 0:
break
print(“游戏结束”)