一.定义玩家类
class Player:
def __init__(self, name):
self.blood = 100 # 血量
self.count_harm = 0 # 积累伤害
self.name = name
print("玩家 {} 已经上场--------".format(self.name))
二.定义攻击方式
def hand_hit(self, enemy): # 手攻击
# self.count_harm = self.count_harm + 10
self.count_harm += 10 # 增加积累伤害
enemy.blood -= 10 # 敌人受伤
def foot_hit(self, enemy): # 脚攻击
self.count_harm += 20 # 增加积累伤害
enemy.blood -= 20 # 敌人受伤
def belly_hit(self, enemy): # 肚皮攻击
self.count_harm += 40 # 增加积累伤害
enemy.blood -= 40 # 敌人受伤
三.定义血量
def show_info(player1, player2): # 显示血量以及伤害
print("玩家 {} 剩余血量:{}".format(player1.name, player1.blood))
print("玩家 {} 积累伤害:{}".format(player1.name, player1.count_harm))
print("玩家 {} 剩余血量:{}".format(player2.name, player2.blood))
print("玩家 {} 积累伤害:{}".format(player2.name, player2.count_harm))
四.主函数入口
bai1 = Player("meigui1")
bai2 = Player("meigui2")
while True:
er = input("谁先攻击:")
mode = input("用什么攻击(手0,脚1,肚皮2):")
if er == 'meigui':
skill_count = [bai1.hand_hit, bai1.foot_hit, bai1.belly_hit]
skill_count[int(mode)](bai2) # 调用技能 发动攻击
show_info(bai1, bai2) # 显示一些信息
elif er == 'meigui':
skill_count = [bai2.hand_hit, bai2.foot_hit, bai2.belly_hit]
skill_count[int(mode)](bai1) # 调用技能 发动攻击
show_info(bai1, bai2) # 显示一些信息
if bai1.blood <= 0 or bai2.blood <= 0:
break
五.效果展示
总结
以上就是今天的学习内容啦~
咱们下期再见老铁~
要全部源码的可以在评论区一键三连哦,谢谢支持!!!