
Lv.1
小园园可爱捏~
在 Python喵喵的进化【面向对象】【求助大神把他改成tkinter版】 中回复
不用谢
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()2024-01-26T09:44:46 点赞:0