1. 程式人生 > >面向對象text 01 蓋倫vs瑞文vs提莫

面向對象text 01 蓋倫vs瑞文vs提莫

@property dom sci lse 索引 ack 表示 int ron

‘‘‘

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 # --------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------- ‘‘‘

面向對象text 01 蓋倫vs瑞文vs提莫