首页 分享 python虚拟宠物

python虚拟宠物

来源:萌宠菠菠乐园 时间:2024-10-01 07:51

最新推荐文章于 2024-08-06 10:00:00 发布


点击(此处)折叠或打开

[18:15 t ~/PycharmProjects/talen]$ ll
-rw-rw-r--. 1 t t 2064 4月 22 18:15 critter_caretaker.py
-rw-rw-r--. 1 t t 17734 4月 22 16:55 critter.graphml

点击(此处)折叠或打开

#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
'''

class Critter(object):
    '''
    #一只需要细心呵护的虚拟宠物
    #critter caretaker
    #构造器,三个公共属性
    '''
    def __init__(self, name, hunger=0, boredom=0):
        self.name=name
        self.hunger=hunger
        self.boredom=boredom
    #单独处理时间流逝
    def __pass_time(self):
        self.hunger += 1
        self.boredom += 1
    #处理宠物性情
    @property
    def mood(self):
        unhappiness= self.boredom + self.hunger
        print("unhappniess num : %s" %unhappiness)
        if unhappiness 5:
            m = "happy"
        elif 5 = unhappiness = 10:
            m = "okay"
        elif 10 = unhappiness = 15:
            m = "bad"
        else:
            m = "mad"
        return m
    #处理
    def talk(self):
        print("I'm %s and I feel %s now!n"%(self.name,self.mood))
        self.__pass_time()
    def eat(self,food=4):
        print("Think you")
        self.hunger -= food
        if self.hunger 0 :
            self.hunger = 0
        #self.__pass_time()
    def play(self,fun=4):
        print("Wheee")
        self.boredom -= fun
        if self.boredom 0 :
            self.boredom = 0
        #self.__pass_time()

def main():
    #创建实例对象
    crit_name=raw_input( "Your pet name:" )
    print("Pet name :%s"%crit_name)
    crit = Critter(crit_name)
    choice=None
    while choice != 0:
        print
        ('''
        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        ''')
        choice=raw_input("请输入你的选择:")
        if choice == "0":
            print("Goodbye!")
            break
        elif choice == "1":
            #听宠物说话
            crit.talk()
        elif choice == "2":
            #给宠物吃饭
            crit.eat()
        elif choice == "3":
            #陪宠物玩
            crit.play()
        else:
            print("Sorry %s"%choice)
main()

点击(此处)折叠或打开

[18:20 t ~/PycharmProjects/talen]$ python critter_caretaker.py
Your pet name:talen
Pet name :talen

        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:1
unhappniess num : 0
I'm talen and I feel happy now!


        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:2
Think you

        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:1
unhappniess num : 3
I'm talen and I feel happy


        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:3
Wheee

        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:2
Think you

        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:3
Wheee

        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:2
Think you

        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:1
unhappniess num : 3
I'm talen and I feel happy now!


        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:1
unhappniess num : 5
I'm talen and I feel okay


        Critter caretaker
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter


        
请输入你的选择:0

[18:21 t ~/PycharmProjects/talen]$


参考:《python初学者指南》

文章知识点与官方知识档案匹配,可进一步学习相关知识

0

点赞

5

觉得还不错? 一键收藏

0

评论

01-213194

11-264483

08-06721

05-311365

12-09488

12-09705

05-07508

04-02374

公安备案号11010502030143 京ICP备19004658号 京网文〔2020〕1039-165号 经营性网站备案信息 北京互联网违法和不良信息举报中心 家长监护 网络110报警服务 中国互联网举报中心 Chrome商店下载 账号管理规范 版权与免责声明 版权申诉 出版物许可证 营业执照 ©1999-2024北京创新乐知网络技术有限公司

相关知识

python虚拟宠物猫
虚拟宠物2资源
python做桌面宠物
Python笔试题
Python期末作业
Python 使用 Pygame 库实现 Tom 猫游戏:初始化游戏、创建 Tom 猫、添加互动功能
Python小练习
输出宠物的叫声python
基于Python的猫狗宠物展示系统
python运行run在哪

网址: python虚拟宠物 https://www.mcbbbk.com/newsview299834.html

所属分类:萌宠日常
上一篇: 南京宠物猫舍猫叔
下一篇: 非非妈

推荐分享