电脑桌面太单调,用Python写一个桌面小宠物应用。
下面是一个使用Python创建的简单桌面小宠物应用。这个小宠物会在桌面上游荡,可以响应鼠标点击,并且有简单的动画效果。
import tkinter as tk
import random
import time
from PIL import Image, ImageTk
import os
import sysclass DesktopPet:def __init__(self, root):self.root = rootself.root.overrideredirect(True) # 移除窗口边框self.root.wm_attributes("-topmost", True) # 窗口置顶self.root.wm_attributes("-transparentcolor", "white") # 设置白色为透明色# 设置窗口大小和初始位置self.width = 100self.height = 100screen_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 = 0self.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 ImageDrawdraw = 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 ImageDrawdraw = 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_xnew_y = self.y + self.direction_y# 边界检测if new_x <= 0 or new_x >= screen_width - self.width:self.direction_x *= -1new_x = max(0, min(new_x, screen_width - self.width))if new_y <= 0 or new_y >= screen_height - self.height:self.direction_y *= -1new_y = max(0, min(new_y, screen_height - self.height))self.x = new_xself.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) % 4self.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 = Trueself.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 = Falsedef 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 // 2self.y = self.root.winfo_y() + event.y - self.height // 2self.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库动态生成的,因此可以直接运行而无需额外的资源文件。