zoukankan      html  css  js  c++  java
  • Learn Python the hard way, ex42 物以类聚

    依然少打很多剧情,并修改了很多,还好,能运行

     1 #!urs/bin/python
     2 #coding:utf-8
     3 
     4 from sys import exit
     5 from random import randint
     6 
     7 class Game(object):
     8 
     9     def __init__(self,start):
    10         self.quips = [
    11         "you died.",
    12         "such a luser.",]
    13         self.start = start
    14 
    15     def play(self):
    16         next = self.start
    17 
    18         while True:
    19             print "
    ------------"
    20             room = getattr(self,next)
    21             next = room()
    22 
    23     def death(self):
    24         print self.quips[randint(0,len(self.quips)-1)]
    25         exit(1)
    26 
    27     def central_corridor(self):
    28         print "in #25 planet"
    29         print "we must find weapon"
    30 
    31         action = raw_input(">>")
    32 
    33         if action == "shoot":
    34             print "you died .because shoot"
    35             return 'death'
    36         elif action == 'dodge':
    37             print "you head and eats U"
    38             return 'death'
    39         elif action == "joke":
    40             print "lucky!"
    41             return 'laser'
    42         else:
    43             print "DOES"
    44             return 'central_corridor'
    45 
    46     def laser(self):
    47         print "you get bomb"
    48         code = "%d" % (randint(1,9))
    49         guess= raw_input("enter key:>>")
    50         guesses = 0
    51 
    52         while guess != code and guesses <10:
    53             print "BZZZZZZZZZZZZEDDDDDD"
    54             guesses +=1
    55             guess = raw_input("[enter key:]>>")
    56 
    57         if guess == code:
    58             print "you guess right"
    59             return 'the_bridge'
    60         else:
    61             print "ship go away and you die"
    62             return 'death'
    63 
    64     def the_bridge(self):
    65         print "which you choise :"
    66 
    67         action = raw_input(">>")
    68 
    69         if action == "throw bomb":
    70             print "it bomb lost"
    71             return 'death'
    72 
    73         elif action == "get bomb":
    74             print "get it .bomb"
    75             return 'escape_pod'
    76         else:
    77             print "go back"
    78             return 'the_bridge'
    79 
    80     def escape_pod(self):
    81         print "do U take?"
    82 
    83         good_pod=randint(1,3)
    84         guess = raw_input('[pod]>>')
    85 
    86         if int(guess) != good_pod:
    87             print "into jam jelly"
    88             return 'death'
    89 
    90         else:
    91             print "time ,you won!"
    92             exit(0)
    93 a_game = Game("central_corridor")
    94 a_game.play()

    Output:

     1 ------------
     2 in #25 planet
     3 we must find weapon
     4 >>joke
     5 lucky!
     6 
     7 ------------
     8 you get bomb
     9 enter key:>>3
    10 BZZZZZZZZZZZZEDDDDDD
    11 [enter key:]>>5
    12 BZZZZZZZZZZZZEDDDDDD
    13 [enter key:]>>7
    14 BZZZZZZZZZZZZEDDDDDD
    15 [enter key:]>>9
    16 BZZZZZZZZZZZZEDDDDDD
    17 [enter key:]>>2
    18 BZZZZZZZZZZZZEDDDDDD
    19 [enter key:]>>4
    20 BZZZZZZZZZZZZEDDDDDD
    21 [enter key:]>>6
    22 BZZZZZZZZZZZZEDDDDDD
    23 [enter key:]>>8
    24 you guess right
    25 
    26 ------------
    27 which you choise :
    28 >>get bomb
    29 get it .bomb
    30 
    31 ------------
    32 do U take?
    33 [pod]>>2
    34 time ,you won!
    35 
    36 ***Repl Closed***
  • 相关阅读:
    golang生成树状菜单
    golang自定义某种类型时的打印输出
    【转】搭建自己的邮件服务器
    【转】【VSCode】golang的调试配置launch.json
    【转】Nvidia GeForce MX250 Lower-End Dedicated Graphics
    【转】Alertmanager高可用
    【转】Prometheus 和 Alertmanager实战配置
    YAML格式的语法
    golang写一个占用大内存的程序
    [转]TDengine常用命令及SQL
  • 原文地址:https://www.cnblogs.com/sub2020/p/7877894.html
Copyright © 2011-2022 走看看