下面是一个使用Python创建的简单桌面小宠物应用。这个小宠物会在桌面上游荡,可以响应鼠标点击,并且有简单的动画效果。
import tkinter as tk
import random
import time
from PIL import Image, ImageTk
import os
import sys
class DesktopPet:
def __init__(self, root):
self.root = root
self.root.overrideredirect(True) # 移除窗口边框
self.root.wm_attributes("-topmost", True) # 窗口置顶
self.root.wm_attributes("-transparentcolor", "white") # 设置白色为透明色
# 设置窗口大小和初始位置
self.width = 100
self.height = 100
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
self.x = random.randint(0, screen_width - self.width)
self.y = random.randint(0, screen_height - self.height)
# 设置窗口位置
self.root.geometry(f"{self.width}x{self.height}+{self.x}+{self.y}")
# 创建画布
self.canvas = tk.Canvas(self.root, width=self.width, height=self.height,
highlightthickness=0, bg='white')
self.canvas.pack()
# 加载宠物图像
self.load_images()
self.current_image_index = 0
self.pet_image = self.canvas.create_image(50, 50, image=self.images[0])
# 初始化变量
self.direction_x = random.choice([-3, -2, -1, 1, 2, 3])
self.direction_y = random.choice([-3, -2, -1, 1, 2, 3])
self.last_move_time = time.time()
self.move_delay = 0.05 # 移动延迟(秒)
self.last_animation_time = time.time()
self.animation_delay = 0.2 # 动画延迟(秒)
self.is_sleeping = False
# 绑定事件
self.root.bind("<Button-1>", self.on_click) # 鼠标左键点击
self.root.bind("<Button-3>", self.on_right_click) # 鼠标右键点击
self.root.bind("<B1-Motion>", self.on_drag) # 拖动宠物
# 开始移动
self.move()
def load_images(self):
# 创建宠物图像
self.images = []
# 正常状态图像
for i in range(4):
img = Image.new('RGBA', (100, 100), (255, 255, 255, 0))
self.create_pet_image(img, i)
self.images.append(ImageTk.PhotoImage(img))
# 睡觉状态图像
img_sleep = Image.new('RGBA', (100, 100), (255, 255, 255, 0))
self.create_sleeping_pet(img_sleep)
self.sleep_image = ImageTk.PhotoImage(img_sleep)
def create_pet_image(self, img, frame):
# 绘制宠物身体
from PIL import ImageDraw
draw = ImageDraw.Draw(img)
# 身体(圆形)
body_color = (255, 200, 200) # 浅粉色
draw.ellipse([20, 20, 80, 80], fill=body_color)
# 耳朵
draw.ellipse([15, 15, 35, 35], fill=body_color)
draw.ellipse([65, 15, 85, 35], fill=body_color)
# 眼睛(根据帧数变化位置,产生眨眼效果)
eye_color = (0, 0, 0)
if frame == 3: # 眨眼帧
draw.ellipse([35, 45, 45, 50], fill=eye_color)
draw.ellipse([55, 45, 65, 50], fill=eye_color)
else:
draw.ellipse([35, 40, 45, 50], fill=eye_color)
draw.ellipse([55, 40, 65, 50], fill=eye_color)
# 嘴巴(微笑)
draw.arc([35, 50, 65, 70], start=0, end=180, fill=eye_color, width=2)
# 腮红
blush_color = (255, 150, 150)
draw.ellipse([25, 55, 35, 65], fill=blush_color)
draw.ellipse([65, 55, 75, 65], fill=blush_color)
def create_sleeping_pet(self, img):
# 绘制睡觉状态的宠物
from PIL import ImageDraw
draw = ImageDraw.Draw(img)
# 身体(圆形)
body_color = (255, 200, 200) # 浅粉色
draw.ellipse([20, 20, 80, 80], fill=body_color)
# 耳朵
draw.ellipse([15, 15, 35, 35], fill=body_color)
draw.ellipse([65, 15, 85, 35], fill=body_color)
# 闭着的眼睛
draw.line([35, 45, 45, 45], fill=(0, 0, 0), width=2)
draw.line([55, 45, 65, 45], fill=(0, 0, 0), width=2)
# Zzz... 睡眠符号
draw.text((70, 30), "Zzz", fill=(100, 100, 255))
def move(self):
current_time = time.time()
# 移动宠物
if current_time - self.last_move_time > self.move_delay:
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
# 更新位置
new_x = self.x + self.direction_x
new_y = self.y + self.direction_y
# 边界检测
if new_x <= 0 or new_x >= screen_width - self.width:
self.direction_x *= -1
new_x = max(0, min(new_x, screen_width - self.width))
if new_y <= 0 or new_y >= screen_height - self.height:
self.direction_y *= -1
new_y = max(0, min(new_y, screen_height - self.height))
self.x = new_x
self.y = new_y
# 更新窗口位置
self.root.geometry(f"+{int(self.x)}+{int(self.y)}")
self.last_move_time = current_time
# 更新动画
if current_time - self.last_animation_time > self.animation_delay:
if not self.is_sleeping:
self.current_image_index = (self.current_image_index + 1) % 4
self.canvas.itemconfig(self.pet_image, image=self.images[self.current_image_index])
self.last_animation_time = current_time
# 随机改变方向
if random.random() < 0.01: # 1%的几率
self.direction_x = random.choice([-3, -2, -1, 1, 2, 3])
self.direction_y = random.choice([-3, -2, -1, 1, 2, 3])
# 随机进入睡眠状态
if not self.is_sleeping and random.random() < 0.002: # 0.2%的几率
self.is_sleeping = True
self.canvas.itemconfig(self.pet_image, image=self.sleep_image)
self.root.after(5000, self.wake_up) # 5秒后醒来
# 继续移动
self.root.after(10, self.move)
def wake_up(self):
self.is_sleeping = False
def on_click(self, event):
# 当点击宠物时,改变方向
self.direction_x = random.choice([-3, -2, -1, 1, 2, 3])
self.direction_y = random.choice([-3, -2, -1, 1, 2, 3])
# 显示点击效果
self.canvas.create_oval(event.x-10, event.y-10, event.x+10, event.y+10,
outline="red", width=2, tags="click")
self.root.after(200, lambda: self.canvas.delete("click"))
def on_right_click(self, event):
# 右键点击退出
self.root.destroy()
def on_drag(self, event):
# 拖动宠物
self.x = self.root.winfo_x() + event.x - self.width // 2
self.y = self.root.winfo_y() + event.y - self.height // 2
self.root.geometry(f"+{int(self.x)}+{int(self.y)}")
def main():
root = tk.Tk()
root.config(bg='white') # 设置背景色为白色(透明色)
pet = DesktopPet(root)
root.mainloop()
if __name__ == "__main__":
main()
如何运行这个程序
确保你已安装Python(建议Python 3.6或更高版本)
安装必要的依赖库:
pip install pillow
将上面的代码保存为desktop_pet.py
运行程序:
python desktop_pet.py
这个程序不需要任何外部图像文件,所有图形都是使用Pillow库动态生成的,因此可以直接运行而无需额外的资源文件。