Python文字游戏:升级、打怪、属性点、装备等(一)

这是我的一些想法,后面还有改进,大家有什么想法一起分享 

重新开些,原来的问题比较多,代码比较乱,近期重新整

import random
import math


class Character:
    def __init__(self, name, lv, hp, HP, ATK, DEF, exp=0, EXP=0, ptr=0, luck=0):
        # 基础属性
        self.name = name  # 名字
        self.lv = lv  # 等级
        self.exp = exp  # 当前经验值
        self.EXP = EXP  # 升级所需经验值
        self.hp = hp  # 当前生命
        self.HP = HP  # 总生命
        self.ATK = ATK  # 攻击力
        self.DEF = DEF  # 防御力
        # 高级属性
        self.ptr = ptr  # 穿透
        self.luck = luck if luck < 100 else 100  # 幸运(0~100)
        self.crit = round(math.tan(luck / 148.2), 2)  # 暴击率
        self.thump = round(math.tan(luck / 148.2) / 3, 2)  # 重击率

    def __repr__(self):  # 显示角色信息
        return f"{self.name}\n" \
               f"等级:{self.lv}\t经验:{self.exp}/{self.EXP}\n" \
               f"生命:{self.hp}/{self.HP}\n" \
               f"攻击:{self.ATK}\t防御:{self.DEF}\n" \
               f"幸运:{self.luck}\t穿透:{self.ptr}\n" \
               f"暴击率:{self.crit}\t重击率:{self.thump}\n"

    def level_up(self):  # 角色升级
        if self.exp >= self.EXP:
            self.lv += 1
            self.exp = self.exp - self.EXP
            self.EXP = int((self.EXP + self.lv) / (self.lv / (self.lv + 1)))
            self.HP += 10
            self.hp = self.HP
            self.ATK += 5
            self.DEF += 2
            print(self.name, "升级了!!")
            print("生命值增加", 10)
            print("攻击力增加", 5)
            print("防御力增加", 2)
            print(self.__repr__())  # 打印升级后信息
            return True
        return False

    def update(self):  # 怪物强化
        self.lv += 1
        self.HP += self.lv
        self.hp = self.HP
        self.exp += self.lv
        self.ATK += int(self.lv / 2)
        self.DEF += int(self.lv / 2 - 1)


# ###################战斗#########################
def battle(P, E):  # 战斗  P:Player  E:Enemy
    x = random.randint(1, 10)  # 先后手概率参数
    # 损血 = 攻击 × (1 - 减伤 %)
    # 减伤 % = 抗性 ÷ (抗性 + 权重) × 100
    # 穿透
    # 重击(无视护甲)
    # 爆伤加成
    w1 = 200  # 玩家权重
    w2 = 400  # 敌人权重

    if x > 1:  # 先手
        while True:
            DEF1 = P.DEF - E.ptr if P.DEF > E.ptr else 0  # 玩家减去穿透后的防御
            DEF2 = E.DEF - P.ptr if E.DEF > P.ptr else 0  # 敌人减去穿透后的防御
            crit_rate = random.randint(1, 100) / 100
            ATK1 = P.ATK * 2 if P.crit > crit_rate else P.ATK  # 玩家暴击攻击
            ATK2 = E.ATK * 2 if E.crit > crit_rate else E.ATK  # 敌人暴击攻击

            ptr_rate = random.randint(1, 100) / 100  # 重击率
            if P.ptr > ptr_rate:  # 重击后防御为0
                DEF2 = 0
            if E.ptr > ptr_rate:
                DEF1 = 0
            r1 = DEF1 / (DEF1 + w1)  # 减伤率
            r2 = DEF2 / (DEF2 + w2)  # 减伤率

            # 玩家伤害
            P_damage = int(ATK1 * (1 - r2))
            E.hp = E.hp - P_damage
            print(f"{P.name}对{E.name}造成了{P_damage}点伤害")
            if E.hp < 0:
                print(E.name, "死亡")
                P.exp += E.exp
                print(f"{P.name}获得{P.exp}经验")
                if P.level_up():  # 是否升级
                    E.update()
                break
            # 敌人伤害
            E_damage = int(ATK2 * (1 - r1))
            P.hp = P.hp - E_damage
            print(f"{E.name}对{P.name}造成了{E_damage}点伤害")
            if P.hp <= 0:
                print(f"{P.name}已阵亡")
                break
    else:
        print(f"{P.name}遭遇怪物偷袭!!!!!!")
        while True:
            DEF1 = P.DEF - E.ptr if P.DEF > E.ptr else 0  # 玩家减去穿透后的防御
            DEF2 = E.DEF - P.ptr if E.DEF > P.ptr else 0  # 敌人减去穿透后的防御
            crit_rate = random.randint(1, 100) / 100
            ATK1 = P.ATK * 2 if P.crit > crit_rate else P.ATK  # 玩家暴击攻击
            ATK2 = E.ATK * 2 if E.crit > crit_rate else E.ATK  # 敌人暴击攻击

            ptr_rate = random.randint(1, 100) / 100  # 重击率
            if P.ptr > ptr_rate:  # 重击后防御为0
                DEF2 = 0
            if E.ptr > ptr_rate:
                DEF1 = 0
            r1 = DEF1 / (DEF1 + w1)  # 减伤率
            r2 = DEF2 / (DEF2 + w2)  # 减伤率

            E_damage = int(ATK1 * (1 - r1))
            P.hp = P.hp - E_damage
            print(f"{E.name}对{P.name}造成了{E_damage}点伤害")
            if P.hp <= 0:
                print(f"{P.name}已阵亡")
                break
            P_damage = int(ATK2 * (1 - r2))
            E.hp = E.hp - P_damage
            print(f"{P.name}对{E.name}造成了{P_damage}点伤害")
            if E.hp < 0:
                print(E.name, "死亡")
                P.exp += E.exp
                print(f"{P.name}获得{P.exp}经验")
                if P.level_up():  # 是否升级
                    E.update()
                break
    E.hp = E.HP
    print("**" * 22)


# ##################初始化##################
def init_player(name):  # 初始化角色
    name = name
    lv = 1
    hp = 100
    ATK = 10
    DEF = 5
    exp = 0
    EXP = 10
    ptr = 4
    luck = 80
    P = Character(name, lv, hp, hp, ATK, DEF, exp, EXP, ptr, luck)
    return P


def init_enemy(name):  # 初始化敌人
    name = name
    lv = 1
    hp = random.randint(10, 20)
    ATK = random.randint(5, 10)
    DEF = random.randint(1, 5)
    exp = 5
    E = Character(name, lv, hp, hp, ATK, DEF, exp, EXP=0)
    return E


if __name__ == '__main__':
    Aron = init_player("奈非天")
    Zom = init_enemy("僵尸")
    print(Aron)
    print(Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)

    print(Aron)
    print(Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)
    battle(Aron, Zom)

    print(Aron)
    print(Zom)

你可能感兴趣的:(python)