桌面宠物1-2(python实现)

2025年4月22日 999+浏览

宠物饿了:每隔一段时间,宠物会显示“饿了”的状态,并提示用户喂食。
全屏暴走:当宠物心情不好时,它会在整个屏幕上快速移动。
爬行模式:宠物有时会切换到爬行模式,缓慢移动。
发表心情:宠物会随机发表一些心情文字,比如“开心”、“无聊”等。


import tkinter as tk
from PIL import Image, ImageTk
import random

class DesktopPet:
    def __init__(self, root):
        # 初始化窗口
        self.root = root
        self.root.overrideredirect(True)  # 去掉窗口边框
        self.root.attributes("-topmost", True)  # 窗口置顶
        self.root.wm_attributes("-transparentcolor", "white")  # 设置透明颜色

        # 加载宠物图像
        self.pet_images = {
            "normal": ImageTk.PhotoImage(Image.open("pet_normal.png").convert("RGBA")),
            "happy": ImageTk.PhotoImage(Image.open("pet_happy.png").convert("RGBA")),
            "hungry": ImageTk.PhotoImage(Image.open("pet_hungry.png").convert("RGBA")),
            "angry": ImageTk.PhotoImage(Image.open("pet_angry.png").convert("RGBA"))
        }
        self.current_image = "normal"

        # 创建画布并放置宠物
        self.canvas = tk.Canvas(root, width=100, height=100, highlightthickness=0)
        self.canvas.pack()
        self.pet = self.canvas.create_image(50, 50, image=self.pet_images[self.current_image])

        # 绑定事件
        self.canvas.bind("<Button-1>", self.on_click)  # 鼠标点击事件

        # 宠物状态
        self.hunger_timer = 0  # 饥饿计时器
        self.mood = "normal"  # 当前心情
        self.is_rampage = False  # 是否暴走
        self.is_crawling = False  # 是否爬行

        # 开始动画
        self.move_pet()
        self.update_status()

    def on_click(self, event):
        """处理鼠标点击事件"""
        if self.mood == "hungry":
            self.feed_pet()  # 喂食
        else:
            self.change_mood("happy")  # 切换到开心状态

    def feed_pet(self):
        """喂食宠物"""
        self.change_mood("normal")
        self.hunger_timer = 0  # 重置饥饿计时器

    def change_mood(self, mood):
        """切换宠物心情"""
        self.mood = mood
        if mood == "angry":
            self.is_rampage = True
        elif mood == "normal":
            self.is_crawling = False
            self.is_rampage = False
        self.update_image()

    def update_image(self):
        """更新宠物图片"""
        if self.mood == "hungry":
            self.current_image = "hungry"
        elif self.mood == "angry":
            self.current_image = "angry"
        elif self.mood == "happy":
            self.current_image = "happy"
        else:
            self.current_image = "normal"
        self.canvas.itemconfig(self.pet, image=self.pet_images[self.current_image])

    def move_pet(self):
        """随机移动宠物"""
        if self.is_rampage:
            x = random.randint(-50, 50)
            y = random.randint(-50, 50)
        elif self.is_crawling:
            x = random.randint(-5, 5)
            y = random.randint(-5, 5)
        else:
            x = random.randint(-10, 10)
            y = random.randint(-10, 10)

        self.canvas.move(self.pet, x, y)

        # 获取当前窗口位置
        current_x, current_y = self.root.winfo_x(), self.root.winfo_y()
        new_x = current_x + x
        new_y = current_y + y

        # 更新窗口位置
        self.root.geometry(f"+{new_x}+{new_y}")

        # 循环调用,形成动画效果
        self.root.after(100, self.move_pet)

    def update_status(self):
        """更新宠物状态"""
        self.hunger_timer += 1
        if self.hunger_timer > 20:  # 每隔20秒宠物会饿
            self.change_mood("hungry")

        # 随机切换心情
        if random.random() < 0.05:  # 5%的概率切换心情
            moods = ["normal", "happy", "angry"]
            self.change_mood(random.choice(moods))

        # 如果暴走,10秒后恢复正常
        if self.is_rampage and random.random() < 0.1:
            self.change_mood("normal")

        # 如果爬行,5秒后恢复正常
        if self.is_crawling and random.random() < 0.2:
            self.change_mood("normal")

        self.root.after(1000, self.update_status)

if __name__ == "__main__":
    # 创建主窗口
    root = tk.Tk()
    pet = DesktopPet(root)
    root.mainloop()