基于面向对象的模拟人生游戏类

【Python之旅】第四篇(四):基于面向对象的模拟人生游戏类

python 面向对象编程 class  模拟人生游戏

摘要:  需要用面向对象的思想编写一个模拟人生的小游戏,其实就目前学的知识来说,最多只能写个故事叙述类的小游戏,因此这里只是给出该游戏中有关class的代码,代码是Alex老师写的,对进一步加深Python面向对象的特性有很大帮助!     游戏的情节梗概如下: John and Liz 是高中同学时的恋...

 需要用面向对象的思想编写一个模拟人生的小游戏,其实就目前学的知识来说,最多只能写个故事叙述类的小游戏,因此这里只是给出该游戏中有关class的代码,代码是Alex老师写的,对进一步加深Python面向对象的特性有很大帮助!

    游戏的情节梗概如下:

John and Liz 是高中同学时的恋人,后来Liz考上了北京城市学院,Jhon没有,为了跟女朋友在一起,他来到了北京打工(一家网吧当网管),挣钱为Liz交学费,后来LIZ毕业后工作了,遇到了公司的高富帅peter,然后两人就苟且在了一起,JHON发现后非常伤心,发誓要把LIZ夺回来,然后他发粪学习,增加自身能力,参加自考,学习老男孩PYTHON,若干年后,当上了某大型互联网公司的IT总监,月薪5万,北京买了车和房,偶然又见到了LIZ,此时她已被高富PETER甩了,LIZ提出再回到JHON身边时,JHONE优雅的说。。。

    从情节中可以看出,用目前所学的知识,做交互式的话,可能意义不太大,但定义好角色的类之后,其实也是可以折腾一下的,由于时间的关系,就不去折腾了,因为这个过程中需要的其实更多的是有关游戏中的Ideas,下面就给出Alex老师写的类代码,以及我自己加的一点点的故事叙述型的游戏情节吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class person:
    assets = 0
    school_name = None
    Interview = ['360''Baidu''Ali''Tengxun']
    attraction = 0
    skills = []
    love_status = None
    lover = None
    job = None
    company = None
 
    def __init__(self, name, sex, role):
        self.name = name
        self.sex = sex
        self.role = role
        print '33[32;1m-33[0m'*60
        if self.role == 'rich':
            self.assets += 10000000
            self.attraction += 80
            print '33[32;1mMy name is %s, I am a %s guy, I have %s money! It is good to be rich..33[0m'
            %(self.name, self.role, self.assets)
        elif self.role == 'poor':
            self.assets += 5000
            self.attraction += 40
            print '033[31;1mMy name is %s, I am a %s guy, I hvae %s money! I hate 
to be poor, but...life is fucking hard..033[0m' % 
            (self.name, self.role, self.assets)
        elif self.role == 'beauty':
            self.assets += 5000
            self.attraction += 90
            print '033[32;1mMy name is %s, I am a %s girl, I do not have much money, 
but I am very beautiful,that makes me feel good and confident, but I do 
not want to be poor forever.033[0m' % (self.name,self.role)
 
 
    def talk(self, msg, tone = 'normal'):
        if tone == 'normal':
            print '33[32;1m%s: %s33[0m' % (self.name, msg)
        elif tone == 'angry':
            print '33[31;1m%s: %s33[0m' % (self.name, msg)
 
    def assets_balance(self, amount, action):
        if action == 'earn':
            self.assets += amount
            print '33[32;1m%s just made %sRMB! Current assets is %s 33[0m' 
            (self.name, amount, self.assets)
        elif action == 'cost':
            self.assets -= amount
            print '33[32;1m%s just cost %sRMB! Current assets is %s 33[0m' 
            (self.name, amount, self.assets)
 
p1 = person('John''male''poor')
p1.talk('Hello, my guys!')
p1.assets_balance(300'earn')
 
p2 = person('Liz''female''beauty')
p2.talk('Hi, my dear!')
p2.assets_balance(1500,'earn')
 
p3 = person('Peter''male''rich')
p3.talk('Hi guys')
p3.assets_balance(3000'cost')
 
def section(part):
    print '33[31;1m*33[0m'*30 + part + '33[31;1m*33[0m'*30
 
section('Part 1: A love story')
 
p1.lover = p2
p1.love_status = 'Not_single'
p1.talk('I hvae a girlfriend, her name is %s.I love she very much.' % p1.lover.name)
 
p2.lover = p1
p1.love_status = 'Not_single'
p2.talk('I have a boy friend, his name is %s.Thout he is poor, he loves me.' % p2.lover.name)
 
section('Part 2: college entrance examination')
 
p1.talk('Oh, my god! I can not go to a college.''angry')
p2.talk('I can go to a college to change my life.')
 
section("Part 3: Liz's difficulity")
 
p2.talk('What should I do?I do not have money to go to the college.')
p1.talk('Do not worry!Though I can not go to the college with you, I still can earn money to support you.')
p2.talk('%s, so thank you for you.I love you!' % p2.lover.name)
 
section("Part 4: work and college life")
 
p1.talk('In order to support %s, I must work hard at the net bar.' % p1.lover.name)
p2.talk('I will study hard to enter a good company when I graduate.')

    重点应该是放在类的代码上,这些代码可以加深初学者对Python类的使用的理解。

原文地址:https://www.cnblogs.com/weiman3389/p/6044957.html