1. 程式人生 > >面向物件程式設計-回合制遊戲

面向物件程式設計-回合制遊戲

面向物件-->類和物件的關係

import random

class God_mountain:  # 類名  神天兵
    def __init__(self,role,hp,spell,mp,skill,skill1,skill2):
        self.role = role
        self.hp = hp
        self.spell = spell
        self.mp = mp
        self.skill = skill
        self.skill1 = skill1
        self.skill2 = skill2

    
def spell_attacks1(self, inferno): # 法師攻擊 技能1 if self.hp > 0: crit = random.randint(50,self.spell) # spell 法術攻擊 100 n = random.randint(0,1) if n == 0: inferno.hp -= self.spell print('%s使出了%s攻擊了%s'%(self.role,self.skill,inferno.role))
else: inferno.hp -= (self.spell + crit) print('%s使出了%s攻擊了%s引發了法術暴擊' % (self.role, self.skill, inferno.role)) def spell_attacks2(self, inferno): # 技能2 if self.hp > 0: crit = random.randint(50,self.spell) n = random.randint(0,1)
if n == 0: inferno.hp -= self.spell print('%s使出了%s攻擊了%s'%(self.role,self.skill1,inferno.role)) else: inferno.hp -= (self.spell + crit) print('%s使出了%s攻擊了%s引發了法術暴擊' % (self.role, self.skill1, inferno.role)) def spell_attacks3(self, inferno): # 技能3 if self.hp > 0: crit = random.randint(50,self.spell) n = random.randint(0,1) if n == 0: inferno.hp -= self.spell print('%s使出了%s攻擊了%s'%(self.role,self.skill2,inferno.role)) else: inferno.hp -= (self.spell + crit) print('%s使出了%s攻擊了%s引發了法術暴擊' % (self.role, self.skill2, inferno.role)) class Mix_monty: # 類名 混天魔 def __init__(self, role,hp,attack,mp,skill): self.role = role self.hp = hp self.attack = attack self.mp = mp self.skill = skill def physical_attacks(self,Wizards): # Wizards 仙族 if self.hp > 0: crit = random.randint(50, self.attack) n = random.randint(0,1) if n == 0: Wizards.hp -= self.attack print('%s仰天長嘯攻擊了%s'%(self.role,Wizards.role)) else: Wizards.hp -= (self.attack + crit) print('%s仰天長嘯使出了%s攻擊了%s引發了雷霆暴擊'%(self.role,self.skill,Wizards.role)) deity = God_mountain('神天兵',1000,100,2000,'袖裡乾坤','天誅地滅','九龍冰封') #仙族 神天兵的資訊 demon = Mix_monty('混天魔',1000,100,1000,'乾坤一棍') # 魔族 混天魔的資訊 import time #引用倒計時 print('\r準備決鬥',end='') time.sleep(0.5) print('\r3',end='') time.sleep(0.5) print('\r2',end='') time.sleep(0.5) print('\r1',end='') time.sleep(0.5) print('\r開始',end='') time.sleep(0.5) print('\r ',end='') def func(): demon.physical_attacks(deity) if deity.hp < 0: deity.hp = 0 print('神天兵剩餘HP', deity.hp) print('----------------------------') time.sleep(0.5) if random.randint(0,1): func() while 1: if deity.hp > 0 and demon.hp > 0: random.choice([deity.spell_attacks1, deity.spell_attacks2, deity.spell_attacks3])(demon) if demon.hp < 0: demon.hp = 0 print('混天魔剩餘HP',demon.hp) print('----------------------------') time.sleep(0.5) func() if demon.hp <= 0: demon.hp = 0 print('\n%s陣亡!仰天大吼一聲,生而為民,死亦何憾!'%(demon.role)) break if deity.hp <= 0: deity.hp = 0 print('\n%s陣亡,發出神靈嘆息,來生再戰'%(deity.role)) break