码迷,mamicode.com
首页 > 编程语言 > 详细

python,面向对象编程的第1个小应用程序,游戏人生

时间:2019-12-07 10:52:38      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:+=   对象   color   center   面向对象编程   __init__   ==   编程   turn   

应用python面向对象的基本功能,实现下面的"游戏人生"小程序

class person:
    def __init__(self,name,gender,age,arg):
        self.name = name
        self.gender = gender
        self.age = age
        self.arg = arg
        print("Initiate status is:")
        self.show_status()
    def grass_fight(self):
        self.arg -= 200
        self.show_status()
    def self_exercize(self):
        self.arg += 100
        self.show_status()
    def multi_person(self):
        self.arg -= 500
        self.show_status()
    def show_status(self):
        print(self.name,self.gender,self.age,The current fight-value is:+str(self.arg))

def processing(choice):
    if choice == 1:
        cjj.grass_fight()
    elif choice == 2:
        cjj.self_exercize()
    elif choice == 3:
        cjj.multi_person()
    elif choice == 4:
        exit(Game Over)
    else:
        print(please re-input your choice)
    return

cjj = person(cjj,female,18,1000)
print(‘‘.center(50,-))
while True:
    print(1. grass fighting)
    print(2. self-exercizing)
    print(3. multi-people)
    print(4. Exit)
    choice = input(Input your choice)
    processing(choice)

运行结果如下:

Initiate status is:
cjj female 18 The current fight-value is:1000
--------------------------------------------------
1. grass fighting
2. self-exercizing
3. multi-people
4. Exit
Input your choice1
cjj female 18 The current fight-value is:800

用户输入选择后,显示当前战斗力,直到用户输入4,游戏结束,显示"Game Over"

python,面向对象编程的第1个小应用程序,游戏人生

标签:+=   对象   color   center   面向对象编程   __init__   ==   编程   turn   

原文地址:https://www.cnblogs.com/iceberg710815/p/12000492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!