Title: Simulated Life
Requirements: 1, at least two characters
2, the process of playing, there is a conflict
3, according to different interactions, produce different behavior.
4, must use the object-oriented grammar and thought
1. Thinking of solving problems
Create a class that gives roles different methods and uses object-oriented thinking to implement
2. Flowchart
3. Source Code Implementation
#!/usr/bin/env python#-*-coding:utf-8-*-# version:python3.5.0# at 2016/3/3 9:38import sysimport timeclass person (objec T): Assets = 0 # Initialize asset to $0 lover = none # Love Object love_status = none # Love status Role_font_color = {' rich ': 32, ' Poor ': Beauty ': 35} # set different roles talk font color Def __init__ (self, name, age, sex, role): Self.name = name SE Lf.age = Age Self.sex = Sex Self.role = role if Self.role = = ' rich ': self.assets + = 10000 xx elif Self.role = = ' Poor ': self.assets + = elif Self.role = = ' Beauty ': Self.asse TS + = + def talk (self, msg, tone= ' normal '): ' normal conversation, font color for:p Aram msg::p Aram Tone: : return: ' if tone = = ' angry ': for S in Msg:sys.stdout.write (' \033[31;1m%s\ 033[0m '% s) sys.stdout.flush () Time.sleep (0.1) sys.stdout.write (' \ n ') Els E:for s in MSG:sys.stdout.write (' \033[%d;1m%s\033[0m '% (self.role_font_color[self.role ], s)) Sys.stdout.flush () Time.sleep (0.1) sys.stdout.write (' \ n ') def All_asset S (self, Money, action): "The individual owns the value of the asset, calculates the amount of money spent with the spend:p Aram:money money:p aram:action earn or spend "If action = = ' earn ': self.assets + = money print (' \033[32;1m%s earns $%s!) now has%s meta \033[0m '% (self.name, Money, self.assets)) elif action = = ' cost ': self.assets-= Money Print (' \033[31;1m%s spent%s dollars! Now also remaining%s \033[0m '% (self.name, Money, Self.assets)) def introduce ( P1, R1, B1): "Initialize personas, introduce yourself." :p Aram P1: A poor character:p Aram R1: A wealthy character:p Aram B1: A beauty role: Return:none "for I in Range (1,101): Print (' Game loading: |%-25s | \033[31;1m%3d%%\033[0m '% (' > ' * (I//4), i), end= ' \ R ') # in the string =%, to be used with double-percent. Time.sleep (0.05) print (' * ' * *) print (' \033[33;1m Welcome to The Sims Live game \033[0m ') print (' * ' * 50 Print (' Introduction to Game characters: ') P1.talk (' [%s]: Hi, my name is Xiaoming, this year%d, a poor boy. I hate poverty and hope to change my life by working hard! '% (P1.name, p1.age)) R1.talk (' [%s]: Hi, my name is Xiao Fu, this year%d, a rich second-generation boy. I have a car has a room, like to bubble beautiful girl, money let me do whatever I want! '% (R1.name, r1.age)) B1.talk (' [%s]: Hi, my name is Xiao Li, this year%d years old, a beautiful girl. Although I have not much money, but I am very beautiful. I don't want to go to poverty forever! '% (B1.name, b1.age)) def show_induction (): "Introduction to Employees: return: ' Print ('-' * ') print (' Chapter One: Entry company '. Center ' print ('-' * ') print (' Xiao Ming and Xiao Li are college lovers, and join the same company after graduation. "They both worked hard to make more money," he said. "\ nthe two years have passed, how much money have they made?" ') p1.all_assets (40000, ' earn ') b1.all_assets (35000, ' earn ') Time.sleep (1) def show_feelings (): ' feelings arise trust crisis : Return: ' Print ('-' * ') print (' Chapter II: Emotional Crisis ') print ('-' * ') b1.talk (' [%s]: Xiao Ming, we've been in love for so long, we don't have a car. How does life live without a room? '% b1.name) p1.talk (' [%s]: Xiao Li, we struggle together, life will certainly be better! '% p1.name) b1.talk(' [%s]: No, now I want to end this life. Little rich to me very good, he has a car has a room, can give me a lot, so I want him together! '% b1.name, ' angry ') r1.talk (' [%s]: Yes, I am true Echolie, you are so poor, do not dream! '% r1.name) p1.talk (' [%s]: Xiao Li, we are very happy together! Xiao Fu is not such a good person on the surface. '% p1.name) b1.talk (' [%s]: That's not the life I want, let's break up! '% b1.name, ' angry ') p1.talk (' [%s]: Give me 3 days to think about it, and I'll make a decision again. '% p1.name) b1.talk (' [%s]: All right, give you 3 days! '% b1.name) time.sleep (1) def break_up (): ' Poor character and beauty role choose to break up, the final outcome. : Return: ' Print ('-' * ') print (' Chapter III: Breakup Ending ') print ('-' * ') p1.talk (' [%s]: Xiao Li, you've decided to break up, okay, then It! '% p1.name) p1.talk (' [%s]: Someday you'll regret it, wait and see '% p1.name, ' angry ') b1.talk (' [%s]: Little rich, let's go! '% b1.name, ' angry ') print (' After the heartbreak of Xiaoming, he eventually became an IT director by attending the Python training. He also bought a car and a house. ') p1.all_assets (5000000, ' earn ') print (' One day, Xiaoming drove by the street, saw two people quarrel, blocking the road ahead. He got off to look at the situation and surprised him! ') P1.talk (' [%s]: You guys, don't block the road! Let me drive past '% p1.name, ' angry ') b1.talk (' [%s]: Xiao Ming, I am Xiao Li, he is little rich. '% B1.name, ' Print (' em at this time, little rich because of the heart, spent all the money. Xiao Li also became poor because of the small rich. Because of the money, they have broken up and quarreled. ') P1.talk (' [%s]: Your karma, if you know this, why shouldAt the beginning of it! '% p1.name) b1.talk (' [%s]: xiaoming, I want to be with you again, okay? '% B1.name, ' B1.talk (' [%s]: Don't dream, you have been with the money because of the small rich, now is back. " Finish the car and leave! '% b1.name, ' angry ') print (' The end little Li is no longer beautiful, turned back to the poor girl. And the small rich also because of excessive play, spend all the money, become poor wretch! ') print (' \033[31;1m play \033[0m ') def together (): "Role poor and beauty selected together, final result: return: ' Print ('-' * ') print (' Chapter Three: the perfect ending ') print ('-' * ') print (' Xiao Ming and Xiao Li choose together, never separate! ') P1.talk (' [%s]: Xiao Li, you finally figured out, we are born a pair! '% p1.name) b1.talk (' [%s]: Well, even if a small rich car has a room, but he goes all day to soak up many girls, such people are not worth together! " '% b1.name) b1.talk (' [%s]: We will make a lot of money through our own efforts! " '% b1.name) r1.talk (' [%s]: Xiao Li, I am not the person you said, those girls are the initiative to ask me, I also have no resistance! " '% r1.name) b1.talk (' [%s]: Xiaoming, let's resign! I don't want to see little rich! '% b1.name, ' angry ') p1.talk (' [%s]: OK, we work together, we have a harvest! '% p1.name ' print (' xiaoming and Xiao Li through their own efforts, eventually also buy a car to buy a house p. And little rich because all day girls, have spent all the money, into the poor cock silk! ') print (' \033[31;1m play \033[0m ') def choose (): "The paradox part of the couple, can choose to ' break up ' or ' together ': return: ' Print ('-' * *) print (' The agreed 3 days time is up, you and I re-choose: \033[31;1m1 Break up 2 together33[0m ') While true:choose1 = input (' xiaoming, select 1 or 2: '). Strip () if choose1 = = ": Continue Choose2 = input (' Xiao Li, choose 1 or 2: '). Strip () if Choose2 = = ': Continue if choose1 in [' 1 ', ' 2 '] and Choose2 in [' 1 ', ' 2 ']: if choose1 = = ' 2 ' and choose2 = = ' 2 ': # Both sides decide to be together together () Break elif Choose1 = = ' 1 ' or choose2 = = ' 1 ': # either party chooses to split break_up () breaks Else:print (' You chose the wrong, please re-select! ' If __name__ = = ' __main__ ': p1 = person (' xiaoming ', ' + ', ' Male ', ' poor ') r1 = person (' Little rich ', ' Male ', ' rich ') B1 = Per Son (' Xiao Li ', ' Female ', ' Beauty ') introduce (P1, R1, B1) # Role Self Introduction show_induction () # onboarding Company Section show_feelings () # Emotional Crisis Part choose ()
Python's mock workplace Life game