首页 > 分享 > 用Python制作桌宠,嘿咻~

用Python制作桌宠,嘿咻~

在这篇文章中,我将向大家介绍如何使用Python制作一个简单的桌面宠物。桌面宠物是一种可以在电脑桌面上运行的动画角色,它可以响应用户的点击和拖动,并在屏幕上移动。我将使用Tkinter库来实现这个项目。Tkinter是Python的标准GUI库,非常适合制作这种类型的应用程序。

准备工作

在开始之前,请确保你的计算机上已经安装了Python和Tkinter,以及pyautogui。如果还没有安装python,请前往Python官方网站下载并安装最新版本的Python。

1|$ pip install tk

2|$ pip install pyautogui

第一步:创建主窗口

首先,我需要创建一个主窗口,并设置它的大小和位置。还要设置窗口的背景颜色和其他属性。

import tkinter as tk

import pyautogui as pt

WIDTH, HEIGHT = pt.size()

taskbarHeight = 40

imgWidth, imgHeight = 500, 370

posX, posY = int(WIDTH / 2 - imgWidth / 2), 0

root = tk.Tk()

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

root.overrideredirect(1)

root.configure(bg='black')

root.attributes('-transparentcolor', 'black')

root.wm_attributes('-topmost', 1)

在这段代码中,我使用pyautogui库获取主屏幕的分辨率,并计算窗口的初始位置。然后,创建一个Tkinter窗口,并设置它的大小、位置、背景颜色和其他属性。

第二步:加载GIF图片

接下来,我需要加载桌面宠物的GIF图片。这些图片将用于显示桌面宠物的不同状态。我编写了一个函数来加载这些图片。

def load_images(file_path, frame_count):

images = []

for i in range(frame_count):

try:

img = tk.PhotoImage(file=file_path, format=f'gif -index {i}')

images.append(img)

except tk.TclError:

break

return images

idleRight = load_images("./photo/sleep.gif", 14)

idleLeft = load_images("./photo/flower.gif", 14)

runRight = load_images("./photo/play.gif", 16)

runLeft = load_images("./photo/run.gif", 18)

fall = load_images("./photo/cry.gif", 12)

在这段代码中,我定义了一个load_images函数,它接受GIF文件的路径和帧数作为参数,并返回一个包含所有帧的列表。然后使用这个函数来加载不同状态的GIF图片。

第三步:设置状态和动画

现在,我需要定义桌面宠物的不同状态,并编写一个函数来更新动画。

status = {

0: fall,

1: idleRight,

2: idleLeft,

3: runRight,

4: runLeft

}

status_num = 0

player = tk.Label(root, image=idleLeft[0], bg='black', bd=0)

player.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

def Anim(num, rate):

global player

if num < len(status[status_num]) - 1:

num += 1

else:

num = 0

player.config(image=status[status_num][num])

root.after(rate, lambda: Anim(num, rate))

Anim(0, 150)

在这段代码中,我定义了一个状态字典,其中每个键对应一个状态,每个值是一个包含该状态下所有帧的列表。还定义了一个Anim函数,它会根据当前状态循环播放动画帧。

第四步:实现状态变化和移动

接下来,我需要实现状态变化和桌面宠物的移动逻辑。

def changeStatus():

global status_num

status_num = random.randint(1, 4)

root.after(random.randint(1000, 5000), changeStatus)

def falling():

global status_num, posX, posY

if root.winfo_y() + imgHeight < HEIGHT - taskbarHeight:

status_num = 0

posY += 1

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

elif root.winfo_y() + imgHeight >= HEIGHT - taskbarHeight and status_num == 0:

status_num = 1

root.after(1, falling)

def moving():

global status_num, posX

if status_num == 3 and root.winfo_x() + imgWidth < WIDTH:

posX += 1

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

elif status_num == 3 and root.winfo_x() + imgWidth >= WIDTH:

status_num = 1

if status_num == 4 and root.winfo_x() > 0:

posX -= 1

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

elif status_num == 4 and root.winfo_x() <= 0:

status_num = 2

root.after(3, moving)

changeStatus()

falling()

moving()

在这段代码中,我定义了changeStatus、falling和moving函数。changeStatus函数每隔一段随机时间改变角色的状态。falling函数处理掉落状态。moving函数根据角色的状态移动窗口。

第五步:添加鼠标事件

最后,我需要添加鼠标点击和拖动事件,使用户可以与桌面宠物交互。

def on_click(event):

global status_num

status_num = random.randint(1, 4)

def on_drag(event):

global posX, posY

posX = event.x_root - imgWidth // 2

posY = event.y_root - imgHeight // 2

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

root.bind("<Button-1>", on_click)

root.bind("<B1-Motion>", on_drag)

在这段代码中,我定义了on_click和on_drag函数,并将它们绑定到鼠标事件。on_click函数在每次点击时随机改变角色的状态,on_drag函数使用户可以通过拖动鼠标移动窗口。

具体展示: 总结

以上就是我用Python制作一个简单的桌面宠物的完整过程。通过这个项目,你可以学习到如何使用Tkinter创建窗口、加载GIF图片、实现动画和处理鼠标事件。

完整代码如下:

import tkinter as tk

import pyautogui as pt

import random

WIDTH, HEIGHT = pt.size()

taskbarHeight = 40

imgWidth, imgHeight = 500, 370

posX, posY = int(WIDTH / 2 - imgWidth / 2), 0

root = tk.Tk()

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

root.overrideredirect(1)

root.configure(bg='black')

root.attributes('-transparentcolor', 'black')

root.wm_attributes('-topmost', 1)

def load_images(file_path, frame_count):

images = []

for i in range(frame_count):

try:

img = tk.PhotoImage(file=file_path, format=f'gif -index {i}')

images.append(img)

except tk.TclError:

break

return images

idleRight = load_images("./photo/sleep.gif", 14)

idleLeft = load_images("./photo/flower.gif", 14)

runRight = load_images("./photo/play.gif", 16)

runLeft = load_images("./photo/run.gif", 18)

fall = load_images("./photo/cry.gif", 12)

status = {

0: fall,

1: idleRight,

2: idleLeft,

3: runRight,

4: runLeft

}

status_num = 0

player = tk.Label(root, image=idleLeft[0], bg='black', bd=0)

player.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

def changeStatus():

global status_num

status_num = random.randint(1, 4)

root.after(random.randint(1000, 5000), changeStatus)

def falling():

global status_num, posX, posY

if root.winfo_y() + imgHeight < HEIGHT - taskbarHeight:

status_num = 0

posY += 1

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

elif root.winfo_y() + imgHeight >= HEIGHT - taskbarHeight and status_num == 0:

status_num = 1

root.after(1, falling)

def moving():

global status_num, posX

if status_num == 3 and root.winfo_x() + imgWidth < WIDTH:

posX += 1

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

elif status_num == 3 and root.winfo_x() + imgWidth >= WIDTH:

status_num = 1

if status_num == 4 and root.winfo_x() > 0:

posX -= 1

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

elif status_num == 4 and root.winfo_x() <= 0:

status_num = 2

root.after(3, moving)

def Anim(num, rate):

global player

if num < len(status[status_num]) - 1:

num += 1

else:

num = 0

player.config(image=status[status_num][num])

root.after(rate, lambda: Anim(num, rate))

def on_click(event):

global status_num

status_num = random.randint(1, 4)

def on_drag(event):

global posX, posY

posX = event.x_root - imgWidth // 2

posY = event.y_root - imgHeight // 2

root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")

root.bind("<Button-1>", on_click)

root.bind("<B1-Motion>", on_drag)

changeStatus()

falling()

moving()

Anim(0, 150)

root.mainloop()

(如果图像读入失败,可以试试改变帧数或用绝对引用地址)

希望你能从这篇教程中学到一些有用的知识,并能创建出你自己的专属桌面宠物。

感谢阅读,跪求打赏,创作不易,orz。

相关知识

怎么用Python制作一个可以聊天的皮卡丘版桌面宠物
桌宠吧
dnf雪人滑板车咻咻宠物外观 dnf雪人滑板车咻咻宠物站街特效
咻!咻!咻!咻!比赛就结束了!2019年狗狗飞球接力赛决赛
关于我们!嘿懂宠物
虚拟桌宠模拟器怎么聊天
虚拟桌宠模拟器 常见问题 Q&A
《嘿!酷格》——以宠物的独特视角感受温暖与爱
咪咻宠物赋能产业链,要做宠物活体新零售服务商
全本阅读化身为猫,成为第一宠物沟通师(咻咻的秋落木)最新章节

网址: 用Python制作桌宠,嘿咻~ https://m.mcbbbk.com/newsview241153.html

所属分类:萌宠日常
上一篇: 7.0宠物对战世界事件成就:送口
下一篇: wow 怎么捕捉宠物啊 ·怎么开