猫史档案馆


Python喵喵的进化【面向对象】【求助大神把他改成tkinter版】

用户:蒟蒻OIer1048576蒟蒻OIer1048576查看:0 回复:1 评论:0 创建时间:2020-04-10T13:38:15


import time,threading,random


class Cat:
    def __init__(self,Type,name):
        self.Type = Type
        self.name = name
        self.hungryValue = 100
        self.happyValue = 100
        self.IQ = 50

    def cry(self):
        print('喵~呜~(伤心)')

    def happy(self):
        print('喵~(快乐)')

    def hungry(self):
        print('喵~咕噜噜~(饿了)')

    def study(self):
        print('喵~')
        self.IQ+=5

    def eat(self,food):
        self.hungryValue += food*3


class SingCat(Cat):
    def __init__(self, Type, name):
        self.Type = Type
        self.name = name
        self.hungryValue = 100
        self.happyValue = 100
        self.IQ = 100

    def sing(self):
        print('啦啦啦')
        evaluate = input('我唱的好不好啊?')
        if evaluate == '好':
            self.happyValue = min(100,happyValue+10)
            self.happy()
        elif evaluate == '不好':
            print('喵~我可能还需要学习')
            self.happyValue-=15
            self.IQ+=10


class CodeMao(SingCat):
    def __init__(self, Type, name):
        self.Type = Type
        self.name = name
        self.hungryValue = 100
        self.happyValue = 100
        self.IQ = 200

    def code(self,time):
        for i in range(int(time)):
            print('打代码ing')
            time.sleep(1)
        self.happyValue = min(100, happyValue+10)
        self.happy()
        self.IQ += 10


class AICodeMao(CodeMao):
    def __init__(self, Type, name):
        self.Type = Type
        self.name = name
        self.hungryValue = 100
        self.happyValue = 100
        self.IQ = 400

    def SuperStudy(self,times):
        for i in range(int(times)):
            IQ+=0.01
            time.sleep(0.005)


class Person:
    def __init__(self):
        self.food=0
        self.Cat=Cat('可爱','喵喵')
    def work(self):
        print('今天,你赚了¥200,买了10袋喵粮')
        self.food+=10

def see(person):
    while person.Cat.happyValue >= 0 and person.Cat.hungryValue >= 0:
        print('喵喵的饥饿值=', person.Cat.hungryValue)
        print('喵喵的开心度=', person.Cat.happyValue)
        print('喵喵的IQ=', person.Cat.IQ)
        time.sleep(5)

def catset(person):
    while person.Cat.happyValue >= 0 and person.Cat.hungryValue >= 0:
        time.sleep(3)
        person.Cat.happyValue -= 1
        if person.Cat.happyValue <= 20:
            person.Cat.cry()
            if person.Cat.happyValue <0:
                print('我这么伤心,你不来安慰我么?')
        person.Cat.hungryValue -= 2
        if person.Cat.hungryValue <= 20:
            person.Cat.hungry()
            if person.Cat.hungryValue < 0:
                print('我这么饿!')

def main(person):
    cls=1
    while person.Cat.happyValue >= 0 and person.Cat.hungryValue >= 0:
        a=input('请输入行为(1——玩耍)(2——学习)(3——喂食)(4——工作):')
        if a == '1':
            if cls>=1:
                person.Cat.happy()
                person.Cat.happyValue += 5
            if cls>=2:
                person.Cat.sing()
                person.Cat.happyValue += 5
            if cls>=3:
                person.Cat.code(random.randint(1,5))
        elif a == '2':
            if cls == 4:
                person.Cat.SuperStudy(random.randint(1, 5)*200)
            else:
                person.Cat.study()
        elif a == '3':
            person.Cat.eat(person.food)
        elif a == '4':
            time.sleep(3)
            person.work()
        if person.Cat.IQ >=400:
            print('恭喜你,喵喵已升级为AI编程猫')
            person.Cat = AICodeMao('可爱', '喵喵')
        elif person.Cat.IQ >= 200:
            print('恭喜你,喵喵已升级为编程猫')
            person.Cat = CodeMao('可爱', '喵喵')
        elif person.Cat.IQ >= 100:
            print('恭喜你,喵喵已升级为唱歌喵')
            person.Cat = SingCat('可爱', '喵喵')
        else:
            person.Cat = Cat('可爱', '喵喵')
        time.sleep(1.5)

p=Person()
a1=threading.Thread(target=see, args=(p,))
a2=threading.Thread(target=catset, args=(p,))
a3 = threading.Thread(target=main, args=(p,))
a1.start()
a2.start()
a3.start()


回复

上一页1 页 / 共 1下一页
littleyuanlittleyuan

不用谢
import tkinter as tk
from tkinter import messagebox
import time, threading, random

class CatUI:
    def __init__(self, master, cat):
        self.master = master
        self.cat = cat
        self.cat_label = tk.Label(master, text=f"猫咪名称:{cat.name}\n饥饿值:{cat.hungryValue}\n开心度:{cat.happyValue}\n智商:{cat.IQ}")
        self.cat_label.pack()

        self.update_ui_timer = None
        self.start_update_ui()

        self.frame_controls = tk.Frame(master)
        self.btn_play = tk.Button(self.frame_controls, text="玩耍", command=self.play)
        self.btn_study = tk.Button(self.frame_controls, text="学习", command=self.study)
        self.btn_feed = tk.Button(self.frame_controls, text="喂食", command=self.feed)
        self.btn_work = tk.Button(self.frame_controls, text="工作", command=self.work)
        self.btn_play.pack(side=tk.LEFT)
        self.btn_study.pack(side=tk.LEFT)
        self.btn_feed.pack(side=tk.LEFT)
        self.btn_work.pack(side=tk.LEFT)
        self.frame_controls.pack()

    def start_update_ui(self):
        def update_ui():
            self.cat_label.config(text=f"猫咪名称:{self.cat.name}\n饥饿值:{self.cat.hungryValue}\n开心度:{self.cat.happyValue}\n智商:{self.cat.IQ}")
            self.update_ui_timer = self.master.after(5000, update_ui)

        update_ui()

    def play(self):
        if isinstance(self.cat, SingCat):
            self.cat.sing()
        elif isinstance(self.cat, CodeMao) or isinstance(self.cat, AICodeMao):
            self.cat.code(random.randint(1, 5))
        self.cat.happy()
        self.update_cat_status()

    def study(self):
        if isinstance(self.cat, AICodeMao):
            self.cat.SuperStudy(random.randint(1, 5) * 200)
        else:
            self.cat.study()
        self.update_cat_status()

    def feed(self):
        self.cat.eat(self.person.food)
        self.update_cat_status()

    def work(self):
        self.person.work()
        self.update_cat_status()

    def update_cat_status(self):
        if self.cat.IQ >= 400:
            self.cat = AICodeMao('可爱', '喵喵')
            messagebox.showinfo("提示", "恭喜你,喵喵已升级为AI编程猫")
        elif self.cat.IQ >= 200:
            self.cat = CodeMao('可爱', '喵喵')
            messagebox.showinfo("提示", "恭喜你,喵喵已升级为编程猫")
        elif self.cat.IQ >= 100:
            self.cat = SingCat('可爱', '喵喵')
            messagebox.showinfo("提示", "恭喜你,喵喵已升级为唱歌喵")
        else:
            self.cat = Cat('可爱', '喵喵')

        self.cat_label.config(text=f"猫咪名称:{self.cat.name}\n饥饿值:{self.cat.hungryValue}\n开心度:{self.cat.happyValue}\n智商:{self.cat.IQ}")

class Person:
    def __init__(self):
        self.food = 0
        self.cat = Cat('可爱', '喵喵')

    def work(self):
        self.food += 10
        messagebox.showinfo("提示", "今天,你赚了¥200,买了10袋喵粮")

def see(person, cat_label):
    while True:
        cat_label.config(text=f"猫咪名称:{person.cat.name}\n饥饿值:{person.cat.hungryValue}\n开心度:{person.cat.happyValue}\n智商:{person.cat.IQ}")
        if person.cat.happyValue <= 0 or person.cat.hungryValue <= 0:
            messagebox.showerror("警告", "喵喵状态异常,请及时关注!")
            break
        time.sleep(5)

def catset(person):
    while True:
        time.sleep(3)
        person.cat.happyValue -= 1
        if person.cat.happyValue <= 20:
            person.cat.cry()
            if person.cat.happyValue < 0:
                messagebox.showerror("警告", "我这么伤心,你不来安慰我么?")
        person.cat.hungryValue -= 2
        if person.cat.hungryValue <= 20:
            person.cat.hungry()
            if person.cat.hungryValue < 0:
                messagebox.showerror("警告", "我这么饿!")

root = tk.Tk()
root.title("猫咪养成模拟器")

p = Person()
cat_ui = CatUI(root, p.cat)

threading.Thread(target=see, args=(p, cat_ui.cat_label)).start()
threading.Thread(target=catset, args=(p,)).start()

root.mainloop()

点赞0


评论