|
制作一个桌面宠物(DesktopPet)在Python中通常涉及多个步骤,包括创建宠物的图形界面、添加动画效果、处理用户交互等。下面是一个简化的步骤指南,帮助你开始使用Python制作桌面宠物:选择图形库:Tkinter(Python自带的图形库,简单但功能有限)。Pygame(适用于游戏和多媒体应用,功能强大)。PyQt 或 ySide(跨平台的图形用户界面工具包,用于创建复杂的桌面应用)。第三方库如Kivy(多平台Python库,用于开发多触摸应用)或wxPython(另一个跨平台的GUI工具包)。设计宠物形象:你可以使用图像编辑软件(如AdobePhotoshop、GIMP等)来创建宠物的图片或动画。宠物可以有不同的状态,比如睡觉、跑动、吃东西等,每种状态对应不同的图片或动画。编写代码:初始化图形窗口,并加载宠物的初始状态图片。编写代码来处理宠物的动画,比如定期更换图片来模拟宠物的动作。添加用户交互功能,比如用户可以点击或拖动宠物来移动它,或者与宠物进行简单的互动。添加逻辑:根据用户的操作或时间的变化,更新宠物的状态和行为。可以添加一些随机性,使宠物的行为看起来更自然。测试与调试:在不同的操作系统和配置上测试你的桌面宠物,确保它能在各种环境下正常工作。调试并修复任何发现的问题。打包与发布:使用如PyInstaller、cx_Freeze等工具将你的应用打包成一个可执行文件。发布你的桌面宠物,可以分享到网上供其他人下载和使用。运行结果:Python代码示例:(代码不完整,完整代码请看文末图片)我基本上快发完了,你们先自己试一下能不能写完它ps:图片自己可以找,不一定要我的importsysimportosimportrandomfromPyQt5importQtWidgets,QtGui,QtCoreclassDeskPet(QtWidgets.QLabel):def__init__(self):super().__init__()self.initUI()self.childPets=[]self.isDragging=Falseself.isMoving=Falseself.change=FalsedefinitUI(self):self.setWindowFlags(QtCore.Qt.FramelessWindowHint|QtCore.Qt.WindowStaysOnTopHint)self.setAttribute(QtCore.Qt.WA_TranslucentBackground)self.setGeometry(500,500,130,130)self.currentAction=self.startIdleself.timer=QtCore.QTimer(self)self.timer.timeout.connect(self.updateAnimation)self.changeDirectionTimer=QtCore.QTimer(self)#添加定时器self.changeDirectionTimer.timeout.connect(self.changeDirection)#定时器触发时调用changeDirection方法self.startIdle()self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)self.customContextMenuRequested.connect(self.showMenu)self.setMouseTracking(True)self.dragging=FalsedefloadImages(self,path):return[QtGui.QPixmap(os.path.join(path,f))forfinos.listdir(path)iff.endswith('.png')]defstartIdle(self):self.setFixedSize(130,130)self.currentAction=self.startIdleself.images=self.loadImages("Deskpet/resource/xianzhi")self.currentImage=0self.timer.start(100)self.moveSpeed=0self.movingDirection=0ifself.changeDirectionTimer.isActive():self.changeDirectionTimer.stop()#停止方向改变的定时器defstartWalk(self):self.setFixedSize(130,130)ifnotself.isDragging:self.currentAction=self.startWalkdirection=random.choice(["zuo","you"])self.images=self.loadImages(f"Deskpet/resource/sanbu/{direction}")self.currentImage=0self.movingDirection=-1ifdirection=="zuo"else1self.moveSpeed=10self.timer.start(100)self.changeDirectionTimer.start(3000)#启动定时器defmovePet(self):screen=QtWidgets.QDesktopWidget().screenGeometry()new_x=self.x()+self.movingDirection*self.moveSpeedifnew_xscreen.width()-self.width()-10:new_x=screen.width()-self.width()-10ifself.currentAction==self.startWalk:self.movingDirection*=-1#停止加载原先的图片self.timer.stop()self.images=[]#清空当前图片列表#根据移动方向加载对应的图片ifself.movingDirection==-1:#向左移动self.images=self.loadImages("Deskpet/resource/sanbu/zuo")else:#向右移动self.images=self.loadImages("Deskpet/resource/sanbu/you")self.currentImage=0self.timer.start(100)self.deskpet_rect=self.geometry()forchildinself.childPets:ifisinstance(child,XiaobaiWindow):self.xiaobai_rect=child.geometry()ifself.deskpet_rect.intersects(self.xiaobai_rect):child.close()self.startMeet()self.move(new_x,self.y())defstartMeet(self):self.setFixedSize(150,150)self.currentAction=self.startMeetself.images=self.loadImages("Deskpet/resource/meet")self.currentImage=0self.moveSpeed=0self.movingDirection=0self.timer.start(30)defstartLift(self):self.setFixedSize(160,160)self.currentAction=self.startLiftself.images=self.loadImages("Deskpet/resource/linqi")self.currentImage=0self.moveSpeed=0self.movingDirection=0self.timer.start(100)defstartFall(self):self.setFixedSize(150,150)self.currentAction=self.startFallself.images=self.loadImages("Deskpet/resource/xialuo")self.currentImage=0self.movingDirection=0self.moveSpeed=5self.stopOtherActions()self.timer.start(30)defstopOtherActions(self):self.timer.stop()ifself.currentAction==self.startWalk:self.changeDirectionTimer.stop()#停止方向判定定时器self.startIdle()elifself.currentAction==self.startLift:self.startIdle()elifself.currentAction==self.startFall:passelse:self.startIdle()defupdateAnimation(self):self.setPixmap(self.images[self.currentImage])self.currentImage=(self.currentImage+1)%len(self.images)ifhasattr(self,'movingDirection'):ifself.currentAction==self.startFall:self.fallPet()else:self.movePet()deffallPet(self):self.setFixedSize(130,130)screen=QtWidgets.QDesktopWidget().screenGeometry()new_y=self.y()+self.moveSpeedifnew_y>screen.height()-self.height()-10:new_y=screen.height()-self.height()-10self.timer.stop()self.startIdle()self.move(self.x(),new_y)defshowMenu(self,position):menu=QtWidgets.QMenu()ifself.currentAction==self.sleep:menu.addAction("偷吃宵夜",self.Snack)menu.addAction("唤醒",self.WakeUp)menu.addSeparator()menu.addAction("隐藏",self.minimizeWindow)menu.addAction("退出",self.close)else:menu.addAction("散步",self.startWalk)menu.addAction("下落",self.startFall)menu.addAction("运动",self.exercise)menu.addAction("吃饭",self.eating)menu.addAction("睡觉",self.sleep)menu.addAction("屁屁舞",self.pipi)menu.addAction("分身术",self.clonePet)menu.addAction("动感光波!",self.transform)menu.addAction("呼唤小白",self.summonXiaobai)menu.addAction("测试",self.startMeet)child_menu=menu.addMenu("小彩蛋")child_menu.addAction("开发者的Q/A",self.starttalk)child_menu.addAction("小游戏",self.transform)menu.addSeparator()menu.addAction("停止",self.startIdle)menu.addAction("隐藏",self.minimizeWindow)menu.addAction("退出",self.close)menu.exec_(self.mapToGlobal(position))defSnack(self):self.setFixedSize(160,130)self.currentAction=self.sleepself.images=self.loadImages("Deskpet/resource/snack")self.currentImage=0self.timer.start(100)self.moveSpeed=0self.movingDirection=0QtCore.QTimer.singleShot(len(self.images)*100,self.sleep)deftransform(self):self.setFixedSize(160,130)self.currentAction=self.transformself.images=self.loadImages("Deskpet/resource/xiandanchaoren")self.currentImage=0self.timer.start(100)self.moveSpeed=0self.movingDirection=0defpipi(self):self.setFixedSize(300,130)self.currentAction=self.pipiself.images=self.loadImages("Deskpet/resource/pipi")self.currentImage=0self.timer.start(25)self.moveSpeed=0self.movingDirection=0defexercise(self):self.setFixedSize(150,180)self.currentAction=self.exerciseself.images=self.loadImages("Deskpet/resource/yundong")self.currentImage=0self.timer.start(125)self.moveSpeed=0self.movingDirection=0defeating(self):self.setFixedSize(160,90)self.currentAction=self.eatingself.images=self.loadImages("Deskpet/resource/eat")self.currentImage=0self.timer.start(25)self.moveSpeed=0self.movingDirection=0QtCore.QTimer.singleShot(len(self.images)*30,self.startIdle)defsleep(self):self.setFixedSize(315,500)self.currentAction=self.sleepself.images=self.loadImages("Deskpet/resource/sleep")self.currentImage=0self.timer.start(155)self.moveSpeed=0self.movingDirection=0defshowWakeUpMenu(self):self.setFixedSize(130,130)self.sleeping=Truemenu=QtWidgets.QMenu()menu.addAction("唤醒",self.wakeUp)menu.exec_(self.mapToGlobal(self.pos()))defWakeUp(self):self.setFixedSize(180,180)self.sleeping=Falseself.currentAction=self.WakeUpself.images=self.loadImages("Deskpet/resource/waken")self.currentImage=0self.timer.start(30)#延时,等待所有图片加载完成QtCore.QTimer.singleShot(len(self.images)*30,self.finishWakeUp)defNinjia(self):self.setFixedSize(160,150)self.sleeping=Falseself.currentAction=self.Ninjiaself.images=self.loadImages("Deskpet/resource/Ninjia")self.currentImage=0self.timer.start(30)#延时,等待所有图片加载完成QtCore.QTimer.singleShot(len(self.images)*30,self.startIdle)defNinjia2(self):new_pet=DeskPet()self.childPets.append(new_pet)self.setFixedSize(160,150)self.sleeping=Falseself.currentAction=self.Ninjia2self.images=self.loadImages("Deskpet/resource/Ninjia2")self.currentImage=0self.timer.start(30)#延时,等待所有图片加载完成QtCore.QTimer.singleShot(len(self.images)*30,self.startIdle)deffinishWakeUp(self):self.movingDirection=0self.wakeUpImagesLoaded=Trueself.setFixedSize(180,180)self.timer.stop()self.currentAction=self.startIdleself.images=self.loadImages("Deskpet/resource/xianzhi")self.currentImage=0self.timer.start(100)defclonePet(self):new_pet=DeskPet()self.childPets.append(new_pet)self.Ninjia()new_pet.show()new_pet.Ninjia2()defstarttalk(self):starttalk=ChatApp()starttalk.show()self.childPets.append(starttalk)defsummonXiaobai(self):xiaobai=XiaobaiWindow()xiaobai.show()self.childPets.append(xiaobai)defcloseEvent(self,event):forchildinself.childPets:child.close()#关闭所有子窗口super().closeEvent(event)defminimizeWindow(self):self.showMinimized()defmousePressEvent(self,event):ifevent.button()==QtCore.Qt.LeftButton:self.dragging=Trueself.isDragging=Trueself.drag_position=event.globalPos()-self.pos()self.prevAction=self.currentActionself.startLift()event.accept()defmouseMoveEvent(self,event):ifQtCore.Qt.LeftButtonandself.dragging:self.move(event.globalPos()-self.drag_position)event.accept()defmouseReleaseEvent(self,event):ifevent.button()==QtCore.Qt.LeftButton:self.dragging=Falseself.isDragging=False#根据需要重新启动changeDirectionTimerifself.currentAction==self.startWalk:self.changeDirectionTimer.start()self.prevAction()#或者self.startIdle(),根据之前的动作恢复状态event.accept()defchangeDirection(self):ifself.currentAction==self.startFallorself.currentAction==self.eatingorself.currentAction==self.transformorself.currentAction==self.sleeporself.currentAction==self.pipiorself.currentAction==self.exerciseorself.currentAction==self.WakeUporself.currentAction==self.startIdleorself.startMeet:return#如果正在执行下落动作,不改变方向ifrandom.random()
|
|