Learn Python The Hard Way (python 2.7) ex45.py 你来制作一个游戏

# -*- coding:utf-8 -*-

# 植物大战僵尸 英雄列表   
#植物方:
 #    英雄名                  属性           原型               专属超级能量                     超级能量效果                           
#5绿影侠Green Shadow       猛长 聪明      豌豆射手        精准轰击 Precision Blast         攻击中线,造成 5 点伤害。
#4玫瑰 Rose                智慧 光能      玫瑰            变羊术Goatify                    把力量最高的一只僵尸变为1/1的山羊。
#3土豆仔Spudow             爆花 守卫      土豆地雷      扔土豆Tater Toss                 召唤一株“烫大头”(对自己线上的一只僵尸造成6点伤害)
#2坚果骑士Wall-Knight      守卫 光能      坚果            坚不可摧 Uncrackable              你的英雄在本回合无法受到伤害。抽一张牌。
#1暗夜菇Night Cap          爆花 聪明      大喷菇        膨胀蘑菇膨胀蘑菇Toadstool Takedown 召唤一株毒蘑菇。在它线上对一只僵尸造成2点伤害



#僵尸方:
#     英雄名                     属性         原型             专属超级能量                     超级能量效果                           
#5超尸Super Brainz            有脑 狡猾    普通僵尸     移动攻击(carried away)     效果:指定一个僵尸移动位置,并且进行一次额外攻击。
#4摔跤狂The Smash            健壮猛兽    巨人         震地猛击Slammin'Smackdown  消灭一株4攻以下的植物。
#3不死女巫Immorticia        有脑 猛兽    蝙蝠女巫     魔宠Witch's Familiar       召唤一只僵尸蝙蝠(2/1,两栖,这张牌对植物造成伤害时,抽一张牌)
#2霹雳舞王Electric Boogaloo 猛兽 疯狂   迪斯科僵尸   活力四射Stayin' Alive      对一株植物造成 3 点伤害。为你的英雄治疗 3 点血量。
#1无穷小子Impfinity            狡猾 疯狂    小鬼         三重威胁Triple Threat      在任意两行生成2攻击1生命的分身小鬼克隆体。能力:两栖。


from random import randint
from sys import exit

class Sence(object):
    def __init__(self):
        pass
    #    self.location = location     ,location, zombie
    #    self.zombie = zombie     
        
    def Greeting(self,location,zombie):
        #self.location = location
        #self.zombie = zombie
        #location = 'discotheque'
        #zombie = 'Electric Boogaloo'
        
        print "Welcome to %s! You all will be eatten by %s and burried here! Haw-haw!" % (location, zombie)
        print "What's the exclusive energy of the %s?" % zombie
        print "A: 'Super Brainz', B: 'Stayin' Alive', C: 'Witch's Familiar', D: 'Slammin'Smackdown' and E: 'carried away'."      
        zombie_energy = raw_input("-->")
        print "Which hero will you choose to destroy the %s?" % zombie
        print "No.1 Night Cap, No.2 Wall-Knight, No.3 Spudow, N0.4 Rose and No.5 Green Shadow."
        hero_no = raw_input("-->")       
        print "Which super power will you choost to match your hero?"
        print "#1 'Toadstool Takedown', #2 'Uncrackable', #3 'Tater Toss', #4 'Goatify', #5 'Precision Blast'."
        plant_power = raw_input("-->")
        
        #print "\n"      #怎么会输出两行回车???待解答
        return zombie_energy, hero_no, plant_power    #此行作用是返回变量值,可在其它Sence类中使用(否则需在Scene类中定义,且与此处值无关)
        

class Engine(object):     
    def __init__(self,the_room):
        self.the_room = the_room
        
    def play(self):
    
        #print "Welcome to %s! You all will be eatten by %s and burried here! Haw-haw!" % (location, zombie)
        print "You are the plant family: Night Cap, Wall-Knight, Spudow, Rose and Green Shadow."
        print "Your super power are 'Toadstool Takedown', 'Uncrackable', 'Tater Toss', 'Goatify', 'Precision Blast'."
        print "Your opponents are the zombie family: Impfinity, Electric Boogaloo, Immorticia, The Smash and Super Brainz."
        print "They have the exclusive energy of 'Super Brainz', 'Stayin' Alive', 'Witch's Familiar', 'Slammin'Smackdown' and 'carried away'."                    
        print "Your plant family will have a war with the zombie family. Now you are under the castle wall of zombie.\n"
        
        while True:
            current_room = the_map.this_scene(self.the_room)          #返回当前场景 类名
            self.the_room = current_room.enter()                      #返回下一场景 名
            #next_room = self.this_scene(current_room)
            
 

 
class Cemetery(Sence):
    #location =  'cemetery'
    #zombie = 'Impfinity'
    #Location1.Greeting()      NameError: name 'Location1' is not defined   不能在类名称后引用函数/方法,只能在实例(变量)后引用
    #aa = Sence('cemetery','Impfinity')
    #aa.Greeting()
    
    def enter(self):   
        password = '%s%s%s'  % (randint(1,9), randint(1,9), randint(1,9))       #变量为3位字符串组成的数字,而非整数
        ## password = '%s%s%s'  % randint(1,9)*3    ----> TypeError: not enough arguments for format string
        #如果写成整形变量  password = randint(1,9)+10*randint(1,9)+100*randint(1,9) 则必须输入整数,
                #     否则会报错--->ValueError: invalid literal for int() with base 10: ''     
        print password        #为了验证程序,print出口令
        
        i = 0
        while i<10:
            cc = raw_input("please input the password to enter the zombie realm!-->")     #字符串变量        
           #如果password直接写成整形变量,则对应的 cc = int(raw_input(……))    此时必须输入整数,输入空字符串(>>   )时会报错
            if cc == password:               #将字符串转变为整形变量
                print "Oh! You are so smart!\n"
                break                                
            i = i+1

        if i == 10 and cc != password:
            print "Oh! You have chosen the wrong password! You die!\n"       #\n 不要写成/n       
            exit(1)                  #此处写exit()或 exit(1) 有何区别??-----------返回值不同,如何查看??待学习
        
        #zombie_energy = ''       #line48 Engine类的3个变量已传入此处,否则这3个变量值都为'',if语句永远无法正确执行
        #hero_no = ''
        #plant_power = ''     
        
        aa = Sence()      
        zombie_energy, hero_no, plant_power = aa.Greeting('Cemetery','Impfinity')    #此行很重要,否则3个变量不会被传递Scene()中输入的值
        ###aa.Greeting('cemetery','Impfinity')           NameError: global name 'zombie_energy' is not defined
                
        if zombie_energy == 'A' and hero_no == 'No.1' and plant_power == '#1':
        #if zombie_energy == '' and hero_no == '' and plant_power == '':       
            print "The Impfinity moves rapidly then uses 'Triple Threat' and turns into 3 bodies to attack the Night Cap, "
            print "but the Night Cap summons 'Toadstool Takedown' and become a expansion rapidly to against and defect it."
            print "The Impfinity converts back one body then drop dead and become a skull on the Cemetery!\n"
            print "^-^  "*10,  "\n \n"
            return 'discotheque'
        else:
            return 'death'
 

 
class Discotheque(Sence):
    #def __init__(self):
        #Sence.__init__(self)
    #location =  'discotheque'
    #zombie = 'Electric Boogaloo'
   #Location1.Greeting()      NameError: name 'Location1' is not defined   不能在类名称后引用函数/方法,只能在实例(变量)后引用
    def enter(self):   
            
        #aa = Sence('discotheque','Electric Boogaloo')
        aa = Sence()
        #location = 'discotheque'
        #zombie = 'Electric Boogaloo'
        zombie_energy, hero_no, plant_power = aa.Greeting('Discotheque','Electric Boogaloo')
        
        if zombie_energy == 'B' and hero_no == 'No.2' and plant_power == '#2':        
            print "The Electric Boogaloo becomes crazy with earsplitting disco music and uses 'Stayin' Alive' to attack the Wall-Knight, "
            print "but the Wall-Knight exerts 'Uncrackable' and get a golden shinging body armor to against and defect it."
            print "The Electric Boogaloo dances slower and slower then drops dead and become a pile of bone ash on the Discotheque!\n"
            print "^-^  "*10,  "\n \n"
            return 'bat_cave'
        else:
            return 'death'
        
        
        
        
#pp = Discotheque()  
#pp.enter()
     
   
class BatCave(Sence):
    def enter(self):  

        aa = Sence()      
        zombie_energy, hero_no, plant_power = aa.Greeting('Bat-cave','Immorticia')
        
        if zombie_energy == 'C' and hero_no == 'No.3' and plant_power == '#3':
            print "The Immorticia bawls and squals then uses 'Witch's Familiar' to summon a zombie bats to attack the Spudow, "
            print "but the Spudow exerts 'Tater Toss' and throw out a large formidable landmines to against and defect it."
            print "The Immorticia let out an ear-piercing scream then drop dead and become a huge black bat on the Bat-cave!\n"   
            print "^-^  "*10,  "\n \n"             
            return 'rifter'
        else:
            return 'death'
            

            
   
class Rifter(Sence):
    def enter(self):    
            
        aa = Sence()      
        zombie_energy, hero_no, plant_power = aa.Greeting('Rifter','The Smash')
        
        if zombie_energy == 'D' and hero_no == 'No.4' and plant_power == '#4':
            print "The Smash appears with tremendous steps and uses 'Slammin'Smackdown' to attack the Rose, "
            print "but the Rose summon 'Goatify' and emits boundless sunshine to against and defect it."
            print "The Smash stops step and becomes a hyaline ice goat as the same size as it before on the Rifter!\n"  
            print "^-^  "*10,  "\n \n"          
            return 'ruins'
        else:
            return 'death'    
    
class Ruins(Sence):
    def enter(self):   

        aa = Sence()      
        zombie_energy, hero_no, plant_power = aa.Greeting('Ruins','Super Brainz')
        
        if zombie_energy == 'E' and hero_no == 'No.5' and plant_power == '#5':
            #continue           ###SyntaxError: 'continue' not properly in loop  只能用在循环里面
            print "The Super Brainz uses 'carried away' to combine a stream of zombies and itself to a huge Skeleton to attack the Green Shadow, "
            print "but the Green Shadow exerts 'Precision Blast' and fire repeating magic pea bullets to against and defect it."
            print "The huge Skeleton cannot move and summon more zombies again, it become a puff of dark smoke then fade from the Ruins!\n"
            print "^-^  "*10,  "\n \n"             
        else:
            return 'death'    
        
        keyword = randint(1,9)                                                #整形变量
        print keyword                           #为了验证程序,print出口令keyword
        cc = int(raw_input("please input the password that to escape the zombie realm!-->") )   ##必须输入整数
        if cc == keyword:                   
            print "Oh! I believe you are smart enough!\n"
            return 'win'
        else:            
            return 'death'   
            
            
class Death(Sence):
    def enter(self):
        print "What a pity! Your hero plant have not enough powerful to defeat the zombie family! You die!\n"
        print "~"*20
        exit(1)
    
class Win(Sence):
    def enter(self):
        print "Wow! Your hero plant are so powerful that you win the game! You are the SUPER HERO!\n"
        print "----------************----------\n"
        exit(1)    

class Map(object):
    dict = {'cemetery': Cemetery(),
            'discotheque': Discotheque(),
            'bat_cave': BatCave(),    
            'rifter': Rifter(),
            'ruins': Ruins(),
            'death': Death(),
            'win': Win()
            }
        
    #def Opening_scene(self):    
        #return Map().next_scene(self.start_scene)
        
    def this_scene(self,scene_name):           #
        #dd = Current_scene.enter()
        return Map.dict.get(scene_name)        #返回传入字符串对应的场景 类名
 
the_map = Map()
the_engine = Engine('cemetery')
the_engine.play()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值