Python制作简单的终端交互小游戏

Python制作简单的终端交互小游戏

因为最近的集训课程中,老师让我们把python,java,nodejs都需要掌握,本人最常使用的是java,python许久没有用过,就想写一段逻辑来帮助自己复习,于是就有了这个交互类的终端小游戏,功能并不完善,有时间会持续更新

首先是创建一个存放要用到的Class的py文件,这里面存放大多数要用到的类还有少部分方法。我将其命名为classes.py

import random

class Humen:
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender
        print("人物"+name+"创建成功")
        if(gender == "男"):
            self.atack = random.randint(80,100)
            self.critical = random.randint(10,15)
            self.defense = random.randint(7,10)
            self.doge = random.randint(5,10)
            self.hp = random.randint(400,500)
            self.hpnow = self.hp
            self.mp = random.randint(180,220)
            self.mpnow = self.mp
            self.skills = []
            self.atkNum = 1
            self.grade = 1
            self.epvalue = 0
        else:
            self.atack = random.randint(40,50)
            self.critical = random.randint(5,10)
            self.defense = random.randint(3,6)
            self.doge = random.randint(5,10)
            self.hp = random.randint(300,400)
            self.hpnow = self.hp
            self.mp = random.randint(280,320)
            self.mpnow = self.mp
            self.skills = []
            self.atkNum = 2
            self.grade = 1
            self.epvalue = 0
    
    # 展示人物信息
    def shows(self):
        print("等级为:",self.grade)
        print("距离下一次升级还有:",100-self.epvalue)
        print("血量为:",self.hpnow,"/",self.hp)
        print("蓝量为:",self.mpnow,"/",self.mp)
        print("攻击力为:",self.atack)
        print("防御力为:",self.defense)
        print("暴击率为:",self.critical)
        print("闪避率为:",self.doge)
        print("="*40)
    
    # 增加经验值,num为经验值数量
    def upgrade(self,num):
        gnum = num/100
        unum = int(gnum)
        epnum = 100%num
        if(unum < 1):
            if(num+self.epvalue<100):
                self.epvalue += num
            else:
                print("upgrade!",self.grade,">",self.grade+1)
                if(self.gender == "男"):
                    self.grade += 1
                    self.hp += 45
                    self.mp += 20
                    self.atack += 10
                    self.defense += random.randint(1,2)
                else:
                    self.grade += 1
                    self.hp += 35
                    self.mp += 30
                    self.atack += 5
                    self.defense += 1
                self.mpnow = self.mp
                self.hpnow = self.hp
                self.epvalue = self.epvalue+num-100
        else:
            if(epnum+self.epvalue<100):
                print("upgrade!",self.grade,">",self.grade+unum)
                if(self.gender == "男"):
                    for i in range(unum):
                        self.grade += 1
                        self.hp += 45
                        self.mp += 20
                        self.atack += 10
                        self.defense += random.randint(1,2)
                else:
                    for i in range(unum):
                        self.grade += 1
                        self.hp += 35
                        self.mp += 30
                        self.atack += 5
                        self.defense += 1
                self.epvalue += epnum
            else:
                print("upgrade!",self.grade,">",self.grade+unum+1)
                if(self.gender == "男"):
                    for i in range(unum+1):
                        self.grade += 1
                        self.hp += 45
                        self.mp += 20
                        self.atack += 10
                        self.defense += random.randint(1,2)
                else:
                    for i in range(unum+1):
                        self.grade += 1
                        self.hp += 35
                        self.mp += 30
                        self.atack += 5
                        self.defense += 1
                self.epvalue = self.epvalue+epnum-100
            self.mpnow = self.mp
            self.hpnow = self.hp

# 怪物
class Monster:
    # 根据怪物的level生成对应的怪物
    def __init__(self,level,name="小怪007",isGod=0):
        self.atack = level * 10
        self.hp = level * 50
        self.grade = level
        self.atkNum = 1
        self.ep = 10
        self.name = name
        if(isGod == 1):
            self.atack *= 1.5
            self.hp *= 1.5
            self.grade = level
            self.atkNum = 1
            self.ep = 25

# 背包
class Backpack:
    def __init__(self):
        self.addhp = 5    #回血药剂
        self.addmp = 5   #回蓝药剂
        self.wapon = None #武器
        self.armor = None #盔甲

    def shows(self):
        print("回血药剂:",self.addhp)
        print("回蓝药剂:",self.addmp)
        print("="*40)

# 装备
class Wapon:
    def __init__(self,level,isGod=0):
        self.atack = random.randint(8,10) * level
        self.critical = 10
        if(isGod == 1):
            self.atack = random.randint(8,10) * level * 2
            self.critical = 20
    def shows(self):
        print("攻击力:",self.atack)
        print("暴击率:",self.critical)

class Armor:
    def __init__(self,level,isGod=0):
        self.defense = 5+level
        self.doge = 5
        self.hp = level * 50
        if(isGod == 1):
            self.defense = (5+level)*2
            self.doge = 10
            self.hp = level * 100
    def shows(self):
        print("血量:",self.hp)
        print("防御力:",self.defense)
        print("闪避率:",self.doge)

# 楼层
class Floor:
    def __init__(self,level):
        self.level = level
        self.treaProb = 5
        self.addProb = 10
        self.monProb = 100
        self.name = str(level) + "楼"
    

# 人打怪兽
def hATKm(Humen,Monster):
    if(Humen.gender == "男"):
        mul = 1
        isCri = random.randint(0,100)
        if(isCri <= Humen.critical):
            mul = 2
        hATK = mul*(Humen.atack+random.randint(-5,5))
        Monster.hp -= hATK
        print(Humen.name,"对怪兽",Monster.name,"造成了",hATK,"伤害")
    else:
        for i in range(2):
            mul = 1
            isCri = random.randint(0,100)
            if(isCri <= Humen.critical):
                mul = 2
            hATK = mul*(Humen.atack+random.randint(-5,5))
            Monster.hp -= hATK
            print(Humen.name,"对",Monster.name,"造成了",hATK,"伤害")

# 怪兽打人
def mATKh(Humen,Monster):
    isDoge = random.randint(0,100)
    if(isDoge <= Humen.doge):
        print(Monster.name,"的攻击被闪避了")
    else:
        mul = (250-Humen.defense)/250
        mATK = mul*Monster.atack
        Humen.hpnow -= mATK
        print(Monster.name,"对",Humen.name,"造成了",mATK,"伤害")

# 判断战斗双方血量都大于0
def allCan(Humen,Monster):
    if(Humen.hpnow <= 0):
        return False
    elif(Monster.hp <= 0):
        return False
    else:
        return True

# 战斗至一方死亡
def battle(Humen,Monster):
    # 战斗开始判断等级高低
    Hgrade = Humen.grade
    Mgrade = Monster.grade
    while(allCan(Humen,Monster)):
        if(Hgrade>=Mgrade):
            hATKm(Humen,Monster)
            Mgrade+=1
        else:
            mATKh(Humen,Monster)
            Hgrade+=1
    if(Humen.hpnow < 0):
        print("人物死亡!")
        print("="*40)
        return False
    else:
        if(Humen.grade - Monster.grade >= 2):
            print("人物获胜! 但是等级超过该层怪物两级,不获得经验")
            return True
        else:
            print("人物获胜!经验加",Monster.ep)
            Humen.upgrade(Monster.ep)
            return True


然后创建一个py文件封装人物和他的物品,我将其命名为prepare.py。这里面有一个显示装备的方法,其实应该放到classes.py里更加好看,但是写了很多了后面不好改就每改了qwq

import classes as cla

class Pre:
    wanjia = cla.Humen("程汉博","男")
    backpack = cla.Backpack()
    flevel = 1

    def showEquip(self):
        wapon = self.backpack.wapon
        armor = self.backpack.armor
        if(wapon == None):
            print('-'*20)
            print("无武器")
            print('----------')
        else:
            print('-'*20)
            print('武器:')
            wapon.shows()
            print('----------')
        if(armor == None):
            print("无盔甲")
            print('-'*20)
        else:
            print("盔甲:")
            armor.shows()
            print('-'*20)

最后是一个测试类,我将其命名为test.py

import classes as cla
import prepare as pre
import random

# 循环
def loop(Pre):
    print("下一步:1(探索) 2(查看) 3(下一层) 4(使用) 88(结束)")
    order = input()
    if(order == '88'):
        print("再见!")
        return False
    elif(order == '1'):
        floor = cla.Floor(Pre.flevel)
        print("第",floor.name)
        thisPro = random.randint(0,100)
        if(thisPro <= 5):
            waponPro = int(random.randint(1,10)/8)
            armorPro = int(random.randint(1,10)/8)
            wapon = cla.Wapon(Pre.flevel,waponPro)
            armor = cla.Armor(Pre.flevel,armorPro)
            print("找到装备! 1(查看) 2(装备武器) 3(装备盔甲) 88(退回上一级)")
            treaOrder = input()
            if(treaOrder == '1'):
                print("当前装备:")
                Pre.showEquip()
                print('-'*20)
                print("武器:")
                wapon.shows()
                print("-"*20)
                print("盔甲:")
                armor.shows()
                print("-"*20)
                print("是否装备? 2(装备武器) 3(装备盔甲) 88(不装备)")
                choise = input()
                if(choise == '2'):
                    if(Pre.backpack.wapon != None):
                        Pre.wanjia.atack -= Pre.backpack.wapon.atack
                        Pre.wanjia.critical -= Pre.backpack.wapon.critical
                    Pre.backpack.wapon = wapon
                    Pre.wanjia.atack += wapon.atack
                    Pre.wanjia.critical += wapon.critical
                    print("="*40)
                    loop(Pre)
                elif(choise == '3'):
                    if(Pre.backpack.armor != None):
                        Pre.wanjia.hp -= Pre.backpack.armor.hp
                        if(Pre.wanjia.hp-Pre.wanjia.hpnow>=Pre.backpack.armor.hp):
                            pass
                        else:
                            Pre.wanjia.hpnow = Pre.wanjia.hp
                        Pre.wanjia.defense -= Pre.backpack.armor.defense
                        Pre.wanjia.doge -= Pre.backpack.armor.doge
                    Pre.backpack.armor = armor
                    Pre.wanjia.hp += armor.hp
                    Pre.wanjia.hpnow += armor.hp
                    Pre.wanjia.defense += armor.defense
                    Pre.wanjia.doge += armor.doge
                    print("="*40)
                    loop(Pre)
                elif(choise == '88'):
                    print("="*40)
                    loop(Pre)
                else:
                    print("请重新输入")
                    choice = input()
                    if(choise == '2'):
                        Pre.backpack.wapon = wapon
                        print("="*40)
                        loop(Pre)
                    elif(choise == '3'):
                        Pre.backpack.armor = armor
                        print("="*40)
                        loop(Pre)
                    else:
                        print("放弃了选择自动跳过")
                        print("="*40)
                        loop(Pre)
            elif(treaOrder == '2'):
                if(Pre.backpack.wapon != None):
                    Pre.wanjia.atack -= Pre.backpack.wapon.atack
                    Pre.wanjia.critical -= Pre.backpack.wapon.critical
                Pre.backpack.wapon = wapon
                Pre.wanjia.atack += wapon.atack
                Pre.wanjia.critical += wapon.critical
                print('='*40)
                loop(Pre)
            elif(treaOrder == '3'):
                if(Pre.backpack.armor != None):
                    Pre.wanjia.hp -= Pre.backpack.armor.hp
                    if(Pre.wanjia.hp-Pre.wanjia.hpnow>=Pre.backpack.armor.hp):
                        pass
                    else:
                        Pre.wanjia.hpnow = Pre.wanjia.hp
                    Pre.wanjia.defense -= Pre.backpack.armor.defense
                    Pre.wanjia.doge -= Pre.backpack.armor.doge
                Pre.backpack.armor = armor
                Pre.wanjia.hp += armor.hp
                Pre.wanjia.hpnow += armor.hp
                Pre.wanjia.defense += armor.defense
                Pre.wanjia.doge += armor.doge
                print('='*40)
                loop(Pre)
            elif(treaOrder == '88'):
                print("="*40)
                loop(Pre)
            else:
                print("输入错误自动放弃")
                print("="*40)
                loop(Pre)
        elif(thisPro <= 10):
            Pre.backpack.addhp += 1
            Pre.backpack.addmp += 1
            print("找到回血和回蓝药剂各一瓶!")
            print("="*40)
            loop(Pre)
        else:
            print("遭遇怪物")
            monPro = int(random.randint(0,10)/9)
            mon = cla.Monster(floor.level,"xdd",monPro)
            if(cla.battle(Pre.wanjia,mon)):
                print('='*40)
                loop(Pre)
            else:
                print("游戏结束")
    elif(order == '2'):
        askWhat(Pre)
    elif(order == '3'):
        if(Pre.wanjia.grade >= Pre.flevel):
            print("进入下一层")
            Pre.flevel += 1
            print("="*40)
            loop(Pre)
        else:
            print("玩家等级不够")
            loop(Pre)
    elif(order == '4'):
        useWhat(Pre)
    else:
        print("退出请输入88")
        print('='*40)
        loop(Pre)

# 查询时调用
def askWhat(Pre):
    print("查询:1(人物状态) 2(背包) 3(装备) 88(退到上一级)")
    order = input()
    if(order == '1'):
        Pre.wanjia.shows()
        loop(Pre)
    elif(order == '2'):
        Pre.backpack.shows()
        loop(Pre)
    elif(order == '3'):
        Pre.showEquip()
        print("="*40)
        loop(Pre)
    elif(order == '88'):
        loop(Pre)

# 使用时调用
def useWhat(Pre):
    print("使用:1(回血药剂) 2(回蓝药剂) 3(技能)")
    order = input()
    if(order == '1'):
        if(Pre.backpack.addhp == 0):
            print("药剂不够!")
            print("="*40)
            loop(Pre)
        else:
            if(Pre.wanjia.hpnow == Pre.wanjia.hp):
                print("人物血量是满的,不需要使用")
                print("="*40)
                loop(Pre)
            elif(Pre.wanjia.hpnow < Pre.wanjia.hp/2):
                Pre.backpack.addhp -= 1
                Pre.wanjia.hpnow += Pre.wanjia.hp/2
                print("回复了",Pre.wanjia.hp/2,"的血量  目前血量:",Pre.wanjia.hpnow,"/",Pre.wanjia.hp)
                print("="*40)
                loop(Pre)
            else:
                Pre.backpack.addhp -= 1
                Pre.wanjia.hpnow = Pre.wanjia.hp
                print("人物血量恢复至满  目前血量:",Pre.wanjia.hpnow,"/",Pre.wanjia.hp)
                print("="*40)
                loop(Pre)
    elif(order == '2'):
        if(Pre.backpack.addmp == 0):
            print("药剂不够!")
            print("="*40)
            loop(Pre)
        else:
            if(Pre.wanjia.mpnow == Pre.wanjia.mp):
                print("人物蓝量是满的,不需要使用")
                print("="*40)
                loop(Pre)
            elif(Pre.wanjia.mpnow < Pre.wanjia.mp/2):
                Pre.backpack.addmp -= 1
                Pre.wanjia.mpnow += Pre.wanjia.mp/2
                print("回复了",Pre.wanjia.mp/2,"的蓝量  目前蓝量:",Pre.wanjia.mpnow,"/",Pre.wanjia.mp)
                print("="*40)
                loop(Pre)
            else:
                Pre.backpack.addmp -= 1
                Pre.wanjia.mpnow = Pre.wanjia.mp
                print("人物蓝量恢复至满  目前蓝量:",Pre.wanjia.mpnow,"/",Pre.wanjia.mp)
                print("="*40)
                loop(Pre)
    # 使用技能
    elif(order == '3'):
        print("技能功能尚未增加")
        print("="*40)
        loop(Pre)
    else:
        print("输入有误,请重新输入")
        useWhat(Pre)

prep = pre.Pre()
loop(prep)

因为这段逻辑是我在测试classes类的时候写的,写这写着就写成了方法,也没有放到classes.py里面去了,所以显得很臃肿,估计过两天我也看不懂了。。。

上一篇:2021-01-30


下一篇:Pandas的分组练习