Python 角色对战游戏
人机对战,自选角色(战士、剑客、盗贼),电脑角色随机生成,每个角色两个技能,随机先手,考虑攻击、暴击、防御。
练习 类的继承,随机函数
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import random
import time
global player0
global player1
class Role:
def __init__(self, na, hp, dam, de):
self.name = na # 姓名
self.health = hp # 血量
self.damage = dam # 攻击
self.defense = de # 防御
class Tank(Role):
def __init__(self, na, hp, dam, de):
super().__init__(na, hp, dam, de)
self.vocation = '战士'
def attack(self, enemy):
print("%s当前生命值%d" % (self.name, self.health))
__dam = self.damage # 技能攻击力
zs1 = random.randint(1, 2)
if zs1 == 1:
print("%s大锤一轮,呼呼生风,径直向%s的胸口打去,打出伤害%d" % (self.name, enemy.name, __dam))
else:
__dam = __dam * 1.2
print("%s双锤并起,兵分两路,打向%s的左肋、右肋,打出伤害%d" % (self.name, enemy.name, __dam))
__hit = __dam - enemy.defense # 技能伤害
print("%s护甲为%d,造成真实伤害%d" % (enemy.name, enemy.defense, __hit))
enemy.health = enemy.health - __hit
if enemy.health < 0:
enemy.health = 0
print("%s剩余血量%d" % (enemy.name, enemy.health))
class Sword(Role):
def __init__(self, na, hp, dam, de):
super().__init__(na, hp, dam, de)
self.vocation = '剑士'
def attack(self, enemy):
print("%s当前生命值%d" % (self.name, self.health))
__dam = self.damage # 技能攻击力
rate = random.randint(1, 100)
if rate >= 75: # 暴击几率
print("%s打出了2倍暴击" % self.name)
__dam = __dam * 2 # 暴击倍数
zs1 = random.randint(1, 2)
if zs1 == 1:
print("%s哈哈一笑,突然间左腿微蹲,长剑呼的甩出,打在%s的腰间,打出伤害%d" % (self.name, enemy.name, __dam))
else:
__dam = __dam * 1.5
print("%s剑势如风,快若闪电,同时攻击%s上中下三路,打出伤害%d" % (self.name, enemy.name, __dam))
__hit = __dam - enemy.defense
print("%s护甲为%d,造成真实伤害%d" % (enemy.name, enemy.defense, __hit))
enemy.health = enemy.health - __hit
if enemy.health < 0:
enemy.health = 0
print("%s剩余血量%d" % (enemy.name, enemy.health))
class Thief(Role):
def __init__(self, na, hp, dam, de):
super().__init__(na, hp, dam, de)
self.vocation = '盗贼'
def attack(self, enemy):
print("%s当前生命值%d" % (self.name, self.health))
__dam = self.damage # 技能攻击力
rate = random.randint(1, 100)
if rate >= 75:
print("%s打出了4倍暴击" % self.name)
__dam = __dam * 4 # 暴击倍数
elif rate >= 50:
print("%s打出了3倍暴击" % self.name)
__dam = __dam * 3 # 暴击倍数
elif rate >= 25: # 暴击几率
print("%s打出了2倍暴击" % self.name)
__dam = __dam * 2 # 暴击倍数
zs1 = random.randint(1, 2)
if zs1 == 1:
print("%s手握小李飞刀,抬手便向%s射去,打出伤害%d" % (self.name, enemy.name, __dam))
else:
__dam = __dam * 1.8
print("%s双手各握一把匕首,一个闪步来到了%s身边,顺势刺出,打出伤害%d" % (self.name, enemy.name, __dam))
__hit = __dam - enemy.defense
print("%s护甲为%d,造成真实伤害%d" % (enemy.name, enemy.defense, __hit))
enemy.health = enemy.health - __hit
if enemy.health < 0:
enemy.health = 0
print("%s剩余血量%d" % (enemy.name, enemy.health))
def main():
while True:
print("*******************************")
print("**********欢迎来到决斗场**********")
print("*******************************")
print(" 输入 S 创建角色 ")
print("*******************************")
print(" 输入 Q 退出游戏 ")
print("*******************************")
ch = input("请输入您的选择:")
if ch == 'S' or ch == 's':
first()
break
elif ch == 'Q' or ch == 'q':
break
else:
print("请输入正确的选择!")
continue
# 职业字典
dict_vocation = {'T': '坦克', 'S': '剑客', 'D': '盗贼'}
def first():
global player1
while True:
name = input("勇士,请为您的角色命名:")
print("-------------------------------------------------")
print("T: 坦克,重甲,血多防高攻击低\n"
"S: 剑客,轻甲,血量、防御、攻击都一般,有几率出双倍暴击\n"
"D: 盗贼,布甲,血少防低攻击高,有几率出多倍暴击\n")
cf = input("%s,请选择您的职业:" % name)
print("-------------------------------------------------")
if cf == 'T' or cf == 't':
hp = random.randint(2500, 3000)
dam = random.randint(400, 600)
de = random.randint(120, 180)
player1 = Tank(name, hp, dam, de)
break
elif cf == 'S' or cf == 's':
hp = random.randint(2000, 2500)
dam = random.randint(600, 800)
de = random.randint(80, 120)
player1 = Sword(name, hp, dam, de)
break
elif cf == 'D' or cf == 'd':
hp = random.randint(1000, 2000)
dam = random.randint(800, 1000)
de = random.randint(40, 80)
player1 = Thief(name, hp, dam, de)
break
else:
print("输入错误,请重新输入!")
continue
show_player_info(_player=player1)
time.sleep(1)
# 初始化电脑角色
global player0
random1 = random.randint(1, 3)
if random1 == 1:
player0 = Tank('龙沧海', 3000, 500, 160)
elif random1 == 2:
player0 = Sword('风无痕', 2000, 700, 100)
else:
player0 = Thief('武小凤', 1500, 900, 80)
print("电脑选择了%s与你对战" % player0.name)
show_player_info(_player=player0)
random2 = random.randint(1, 2)
if random2 == 1:
print("止戈为武,以武会友,%s请你先出手!" % player0.name)
duel(_player0=player0, _player1=player1)
elif random2 == 2:
print("%s不讲武德,卑鄙的向你发起了偷袭" % player0.name)
duel(_player0=player1, _player1=player0)
def show_player_info(_player):
print("%s是一位%s\n生命值%d\n攻击力%d\n防御力%d"
% (_player.name, _player.vocation, _player.health, _player.damage, _player.defense))
print("****************************************")
def duel(_player0, _player1):
while True:
_player1.attack(_player0)
if _player0.health <= 0:
print("******************************")
print("恭喜%s获得胜利!" % _player1.name)
break
print("---------------------------------")
time.sleep(1)
_player0.attack(_player1)
if _player1.health <= 0:
print("******************************")
print("%s赢得了决斗!" % _player0.name)
break
print("---------------------------------")
time.sleep(1)
if __name__ == '__main__':
main()