‘‘‘ Text For Class: League of Legends Garen vs Riven vs Teemo ‘‘‘ import random # 全局随机 import time class HeroName: # 英雄名字类 GameName = ‘League of Legends‘ # 游戏名字 def __init__(self,hero_name): # 初始化设定,自身和形参名 self.hero_name = hero_name # 形参名初始化 self.HP = 800 # 血量初始化 if hero_name not in [‘Garen‘,‘Riven‘,‘Teemo‘]: # 不是瑞文提莫盖伦 self.hero_name == ‘Teemo‘ # 都为提莫 Teemo_HP = random.randint(100,250) # 提莫血量随机整数 全闭[100到250] self.HP = Teemo_HP # 提莫血量赋名 self.__armor = 0 # 提莫格挡为0 self.__att = 1000 # 提莫攻击1000 elif hero_name == ‘Teemo‘: # 提莫 HP 随机 100-250 Teemo_HP = random.randint(100, 250) self.HP = Teemo_HP self.__armor = 0 # 格挡 0 self.__att = 1000 # 攻击 1000 elif hero_name == ‘Garen‘: # 盖伦 HP 800 self.__armor = 150 # 格挡 150 self.__att = 200 # 攻击 200 else : # 瑞文 self.__armor = 80 # 格挡 80 HP 800 self.__att = 300 # 攻击 300 @property def armor_count(self): return self.__armor def attack(self,enemy): n = 1 print(‘%s vs %s‘ % (self.hero_name, enemy.hero_name)) if self.hero_name == ‘Teemo‘ or enemy.hero_name == ‘Teemo‘: print(‘Teemo必须死‘) while True: if (enemy.HP > 0 and self.HP > 0): print(‘----------------------------------‘) print(‘第%s回合‘ %n) enemy.HP -= self.__att - enemy.armor_count info_1 = self.hero_name,enemy.hero_name,enemy.hero_name,enemy.HP,self.hero_name,self.HP print(‘%s攻击了%s,%s还剩%s滴血,%s还剩%s滴血‘% info_1) if enemy.HP > 0: self.HP -= enemy.__att - self.armor_count info_2 = enemy.hero_name, self.hero_name, self.hero_name, self.HP, enemy.hero_name, enemy.HP print(‘%s反击了%s,%s还剩%s滴血,敌人%s还剩%s滴血‘ % info_2) n += 1 # time.sleep(3) else: print(‘胜利,敌人%s已经阵亡‘ % enemy.hero_name) break else: print(‘你的角色%s已经阵亡‘ % self.hero_name) break class Game: # 定义游戏类 name = ‘League of Legends , Garen vs Riven vs Teemo‘ @classmethod # 能拿到类中定义的属性 def start(cls): print(‘--- %s ---‘% cls.name) # League of Legends , Garen vs Riven vs Teemo H1 = HeroName(‘Garen‘) H2 = HeroName(‘Riven‘) H3 = HeroName(‘Teemo‘) choose = random.randint(1,4) if choose == 1: enemy = H2.hero_name # print(‘盖伦打瑞文‘) H1.attack(H2) elif choose == 2: enemy = H1.hero_name # print(‘瑞文打盖伦‘) H2.attack(H1) elif choose == 3: H3 = HeroName(‘Teemo‘) enemy = H1.hero_name # print(‘提莫打盖伦‘) H3.attack(H1) else: H3 = HeroName(‘Teemo‘) enemy = H3.hero_name # print(‘盖伦打提莫‘) H1.attack(H3) Game.start() # 游戏开始 ‘‘‘ # -- (Problems in testing) ------------------------------------------------------------------------------------------ # -- (1.) ------------------------------------------------------------------------------------------------ # -- (import random *module) ---------------------------------------------------------------------------------------------- # -- (# auth code) ---------------------------------------------------------------------------------------------- import random # import random *module 声明随机模块 def auth_code(n): # 定义生成码函数,位形n 接 外实传n res=‘‘ # 声明一个空字符串 # 因为下面随机出来一个是整型一个是字符串 # 在整型65,90中,把随机出来的数字通过内置chr,转换为ascii中对应的大写的A-Z,(97-122小写) # 定义空字符串为了做字符串拼接 for i in range(n): # 循环范围传参 s1 = chr(random.randint(65,90)) # randint 大于等于65,小于等于90的整数,前闭后闭[65,90] # 然后通过chr 转成ascii码对应的字符 # chr(65-90) 就是A-Z # chr(97-122) 就是a-z # ord()函数主要用来返回对应字符的ascii码 # chr()主要用来表示ascii码对应的字符 # 可以用十进制,也可以用十六进制。 s2=str(random.randint(0,9)) # randint 大于等于0,小于等于9的整数,前闭后闭[0,9] # 转成str字符串 res += random.choice([s1,s2]) # 二选一,现在输出都为字符串 # random.choice模块是通过[]列表的索引取值 # 每次循环结束都把结果 添加到定义的res空字符串中‘‘字符串拼接 # 循环外界次数n次就循环n次,随机n次 return res # print(auth_code(9)) # res = 5U549I6H0... # ---- (Random Ex) ------------------------------------------------------------------------------------------------ # 1. # ascii: # Capital(A-Z) for range(65,90) # lower(a-z) for range(97,122) # Convert to ASCII code through Chr # Then check result‘s type. s = random.randint(65, 90) # 65|90|85|72 print(s,type(s)) # Enter --> s,int s = chr(s) # s --> chr --> ascii print(s,type(s)) # Enter --> s,str variable_name = random.choice([10,‘25‘]) print(variable_name) # Enter --> # case.1.Enter --> list[0,1] --> list[0] --> 10 # case.2.Enter --> list[0,1] --> list[1] --> ‘25‘ # res = random.choice([‘x‘,‘y‘,random.randint(1,9)]) # print(res) # Enter --> # case.1.Enter --> list[0,1,2] --> list[0] --> ‘x‘ # case.2.Enter --> list[0,1,2] --> list[1] --> ‘y‘ # case.3.Enter --> list[0,1,2] --> list[2] --> random.randint(1,9) --> 1|2...|9 # --------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------- ‘‘‘
原文地址:https://www.cnblogs.com/max404/p/10743694.html
时间: 2024-11-09 02:52:40