找回密码
 会员注册
查看: 19|回复: 0

Python制作的桌面宠物-python实战-python源码-python项目练习

[复制链接]

3

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2024-9-6 22:04:18 | 显示全部楼层 |阅读模式
Python制作的桌面宠物随机从64种宠物里生成,从一款是你喜欢的,赶紧来认养吧#完整源代码可在下方图片免费获取运行程序前请先安装PyQt5。程序入口DesktopPet.py。程序运行截图:DesktopPet.py'''Function:实现一款桌面宠物'''importosimportcfgimportsysimportrandomfromPyQt5.QtGuiimport*fromPyQt5.QtCoreimport*fromPyQt5.QtWidgetsimport*fromPyQt5importQtWidgets,QtGui'''桌面宠物'''classDesktopPet(QWidget):def__init__(self,parent=None,**kwargs):super(DesktopPet,self).__init__(parent)#初始化self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.SubWindow)self.setAutoFillBackground(False)self.setAttribute(Qt.WA_TranslucentBackground,True)self.repaint()#随机导入一个宠物self.pet_images,iconpath=self.randomLoadPetImages()#设置退出选项quit_action=QAction('退出',self,triggered=self.quit)quit_action.setIcon(QIcon(iconpath))self.tray_icon_menu=QMenu(self)self.tray_icon_menu.addAction(quit_action)self.tray_icon=QSystemTrayIcon(self)self.tray_icon.setIcon(QIcon(iconpath))self.tray_icon.setContextMenu(self.tray_icon_menu)self.tray_icon.show()#当前显示的图片self.image=QLabel(self)self.setImage(self.pet_images[0][0])#是否跟随鼠标self.is_follow_mouse=False#宠物拖拽时避免鼠标直接跳到左上角self.mouse_drag_pos=self.pos()#显示self.resize(128,128)self.randomPosition()self.show()#宠物动画动作执行所需的一些变量self.is_running_action=Falseself.action_images=[]self.action_pointer=0self.action_max_len=0#每隔一段时间做个动作self.timer=QTimer()self.timer.timeout.connect(self.randomAct)self.timer.start(500)'''随机做一个动作'''defrandomAct(self):ifnotself.is_running_action:self.is_running_action=Trueself.action_images=random.choice(self.pet_images)self.action_max_len=len(self.action_images)self.action_pointer=0self.runFrame()'''完成动作的每一帧'''defrunFrame(self):ifself.action_pointer==self.action_max_len:self.is_running_action=Falseself.action_pointer=0self.action_max_len=0self.setImage(self.action_images[self.action_pointer])self.action_pointer+=1'''设置当前显示的图片'''defsetImage(self,image):self.image.setPixmap(QPixmap.fromImage(image))'''随机导入一个桌面宠物的所有图片'''defrandomLoadPetImages(self):pet_name=random.choice(list(cfg.PET_ACTIONS_MAP.keys()))actions=cfg.PET_ACTIONS_MAP[pet_name]pet_images=[]foractioninactions:pet_images.append([self.loadImage(os.path.join(cfg.ROOT_DIR,pet_name,'shime'+item+'.png'))foriteminaction])iconpath=os.path.join(cfg.ROOT_DIR,pet_name,'shime1.png')returnpet_images,iconpath'''鼠标左键按下时,宠物将和鼠标位置绑定'''defmousePressEvent(self,event):ifevent.button()==Qt.LeftButton:self.is_follow_mouse=Trueself.mouse_drag_pos=event.globalPos()-self.pos()event.accept()self.setCursor(QCursor(Qt.OpenHandCursor))'''鼠标移动,则宠物也移动'''defmouseMoveEvent(self,event):ifQt.LeftButtonandself.is_follow_mouse:self.move(event.globalPos()-self.mouse_drag_pos)event.accept()'''鼠标释放时,取消绑定'''defmouseReleaseEvent(self,event):self.is_follow_mouse=Falseself.setCursor(QCursor(Qt.ArrowCursor))'''导入图像'''defloadImage(self,imagepath):image=QImage()image.load(imagepath)returnimage'''随机到一个屏幕上的某个位置'''defrandomPosition(self):screen_geo=QDesktopWidget().screenGeometry()pet_geo=self.geometry()width=(screen_geo.width()-pet_geo.width())*random.random()height=(screen_geo.height()-pet_geo.height())*random.random()self.move(width,height)'''退出程序'''defquit(self):self.close()sys.exit()'''run'''if__name__=='__main__':app=QApplication(sys.argv)pet=DesktopPet()sys.exit(app.exec_())123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122配置文件cfg.py'''配置文件'''ROOT_DIR='resources'ACTION_DISTRIBUTION=[['1','2','3'],['4','5','6','7','8','9','10','11'],['12','13','14'],['15','16','17'],['18','19'],['20','21'],['22'],['23','24','25'],['26','27','28','29'],['30','31','32','33'],['34','35','36','37'],['38','39','40','41'],['42','43','44','45','46']]PET_ACTIONS_MAP={'pet_1':ACTION_DISTRIBUTION}foriinrange(2,65)ET_ACTIONS_MAP.update({'pet_%s'%i:ACTION_DISTRIBUTION})1234567891011121314151617181920完整源代码请下方获取👇↓↓↓
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-10 18:12 , Processed in 1.923214 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表