首页 > 分享 > 用Python写了一个好玩的桌面宠物游戏脚本,简单又好玩

用Python写了一个好玩的桌面宠物游戏脚本,简单又好玩

今天,我们来分享一个宠物桌面小程序,全程都是通过 PyQT 来制作的,对于 Python GUI 感兴趣的朋友,千万不要错过哦!

我们先来看看最终的效果,对于一个小小的娱乐项目来说,还是不错啦!

好了,废话不多说,我直接上干货,本项目使用 PYQT5 作为编码框架,如果你对于该框架不是特别熟悉的话,建议先去简单学习一下~

源码和素材图片在文末领取!

素材图片

项目源码展示

import sys

import os

import random

from PyQt5 import QtWidgets, QtGui, QtCore

class DeskPet(QtWidgets.QLabel):

def __init__(self):

super().__init__()

self.initUI()

self.childPets = []

self.isDragging = False

self.isMoving = False

self.change = False

def initUI(self):

self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)

self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

self.setGeometry(500, 500, 130, 130)

self.currentAction = self.startIdle

self.timer = QtCore.QTimer(self)

self.timer.timeout.connect(self.updateAnimation)

self.changeDirectionTimer = QtCore.QTimer(self)

self.changeDirectionTimer.timeout.connect(self.changeDirection)

self.startIdle()

self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

self.customContextMenuRequested.connect(self.showMenu)

self.setMouseTracking(True)

self.dragging = False

def loadImages(self, path):

return [QtGui.QPixmap(os.path.join(path, f)) for f in os.listdir(path) if f.endswith('.png')]

def startIdle(self):

self.setFixedSize(130, 130)

self.currentAction = self.startIdle

self.images = self.loadImages("resource/xianzhi")

self.currentImage = 0

self.timer.start(100)

self.moveSpeed = 0

self.movingDirection = 0

if self.changeDirectionTimer.isActive():

self.changeDirectionTimer.stop()

def startWalk(self):

self.setFixedSize(130, 130)

if not self.isDragging:

self.currentAction = self.startWalk

direction = random.choice(["zuo", "you"])

self.images = self.loadImages(f"resource/sanbu/{direction}")

self.currentImage = 0

self.movingDirection = -1 if direction == "zuo" else 1

self.moveSpeed = 10

self.timer.start(100)

self.changeDirectionTimer.start(3000)

def movePet(self):

screen = QtWidgets.QDesktopWidget().screenGeometry()

new_x = self.x() + self.movingDirection * self.moveSpeed

if new_x < 10:

new_x = 10

if self.currentAction == self.startWalk:

self.movingDirection *= -1

self.timer.stop()

self.images = []

if self.movingDirection == -1:

self.images = self.loadImages("resource/sanbu/zuo")

else:

self.images = self.loadImages("resource/sanbu/you")

self.currentImage = 0

self.timer.start(100)

elif new_x > screen.width() - self.width() - 10:

new_x = screen.width() - self.width() - 10

if self.currentAction == self.startWalk:

self.movingDirection *= -1

self.timer.stop()

self.images = []

if self.movingDirection == -1:

self.images = self.loadImages("resource/sanbu/zuo")

else:

self.images = self.loadImages("resource/sanbu/you")

self.currentImage = 0

self.timer.start(100)

self.deskpet_rect = self.geometry()

for child in self.childPets:

if isinstance(child, XiaobaiWindow):

self.xiaobai_rect = child.geometry()

if self.deskpet_rect.intersects(self.xiaobai_rect):

child.close()

self.startMeet()

self.move(new_x, self.y())

def startMeet(self):

self.setFixedSize(150, 150)

self.currentAction = self.startMeet

self.images = self.loadImages("resource/meet")

self.currentImage = 0

self.moveSpeed = 0

self.movingDirection = 0

self.timer.start(30)

def startLift(self):

self.setFixedSize(160, 160)

self.currentAction = self.startLift

self.images = self.loadImages("resource/linqi")

self.currentImage = 0

self.moveSpeed = 0

self.movingDirection = 0

self.timer.start(100)

def startFall(self):

self.setFixedSize(150, 150)

self.currentAction = self.startFall

self.images = self.loadImages("resource/xialuo")

self.currentImage = 0

self.movingDirection = 0

self.moveSpeed = 5

self.stopOtherActions()

self.timer.start(30)

def stopOtherActions(self):

self.timer.stop()

if self.currentAction == self.startWalk:

self.changeDirectionTimer.stop()

self.startIdle()

elif self.currentAction == self.startLift:

self.startIdle()

elif self.currentAction == self.startFall:

pass

else:

self.startIdle()

def updateAnimation(self):

self.setPixmap(self.images[self.currentImage])

self.currentImage = (self.currentImage + 1) % len(self.images)

if hasattr(self, 'movingDirection'):

if self.currentAction == self.startFall:

self.fallPet()

else:

self.movePet()

def fallPet(self):

self.setFixedSize(130, 130)

screen = QtWidgets.QDesktopWidget().screenGeometry()

new_y = self.y() + self.moveSpeed

if new_y > screen.height() - self.height() - 10:

new_y = screen.height() - self.height() - 10

self.timer.stop()

self.startIdle()

self.move(self.x(), new_y)

def showMenu(self, position):

menu = QtWidgets.QMenu()

if self.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))

def Snack(self):

self.setFixedSize(160, 130)

self.currentAction = self.sleep

self.images = self.loadImages("resource/snack")

self.currentImage = 0

self.timer.start(100)

self.moveSpeed = 0

self.movingDirection = 0

QtCore.QTimer.singleShot(len(self.images) * 100, self.sleep)

def transform(self):

self.setFixedSize(160, 130)

self.currentAction = self.transform

self.images = self.loadImages("resource/xiandanchaoren")

self.currentImage = 0

self.timer.start(100)

self.moveSpeed = 0

self.movingDirection = 0

def pipi(self):

self.setFixedSize(300, 130)

self.currentAction = self.pipi

self.images = self.loadImages("resource/pipi")

self.currentImage = 0

self.timer.start(25)

self.moveSpeed = 0

self.movingDirection = 0

def exercise(self):

self.setFixedSize(150, 180)

self.currentAction = self.exercise

self.images = self.loadImages("resource/yundong")

self.currentImage = 0

self.timer.start(125)

self.moveSpeed = 0

self.movingDirection = 0

def eating(self):

self.setFixedSize(160, 90)

self.currentAction = self.eating

self.images = self.loadImages("resource/eat")

self.currentImage = 0

self.timer.start(25)

self.moveSpeed = 0

self.movingDirection = 0

QtCore.QTimer.singleShot(len(self.images) * 30, self.startIdle)

def sleep(self):

self.setFixedSize(315, 500)

self.currentAction = self.sleep

self.images = self.loadImages("resource/sleep")

self.currentImage = 0

self.timer.start(155)

self.moveSpeed = 0

self.movingDirection = 0

def showWakeUpMenu(self):

self.setFixedSize(130, 130)

self.sleeping = True

menu = QtWidgets.QMenu()

menu.addAction("唤醒", self.wakeUp)

menu.exec_(self.mapToGlobal(self.pos()))

def WakeUp(self):

self.setFixedSize(180, 180)

self.sleeping = False

self.currentAction = self.WakeUp

self.images = self.loadImages("resource/waken")

self.currentImage = 0

self.timer.start(30)

QtCore.QTimer.singleShot(len(self.images) * 30, self.finishWakeUp)

def Ninjia(self):

self.setFixedSize(160, 150)

self.sleeping = False

self.currentAction = self.Ninjia

self.images = self.loadImages("resource/Ninjia")

self.currentImage = 0

self.timer.start(30)

QtCore.QTimer.singleShot(len(self.images) * 30, self.startIdle)

def Ninjia2(self):

new_pet = DeskPet()

self.childPets.append(new_pet)

self.setFixedSize(160, 150)

self.sleeping = False

self.currentAction = self.Ninjia2

self.images = self.loadImages("resource/Ninjia2")

self.currentImage = 0

self.timer.start(30)

QtCore.QTimer.singleShot(len(self.images) * 30, self.startIdle)

def finishWakeUp(self):

self.movingDirection = 0

self.wakeUpImagesLoaded = True

self.setFixedSize(180, 180)

self.timer.stop()

self.currentAction = self.startIdle

self.images = self.loadImages("resource/xianzhi")

self.currentImage = 0

self.timer.start(100)

def clonePet(self):

new_pet = DeskPet()

self.childPets.append(new_pet)

self.Ninjia()

new_pet.show()

new_pet.Ninjia2()

def starttalk(self):

starttalk = ChatApp()

starttalk.show()

self.childPets.append(starttalk)

def summonXiaobai(self):

xiaobai = XiaobaiWindow()

xiaobai.show()

self.childPets.append(xiaobai)

def closeEvent(self, event):

for child in self.childPets:

child.close()

super().closeEvent(event)

需要完整源码和素材图片的朋友们可以【点击这里】免费领取!

相关知识

【Python程序】用200行Python代码制作有趣的桌面宠物(源码可分享),大打工人解压放松程序,如何用Python制作一个桌面宠物!
用 Python 制作一个桌面宠物,好玩
用 Python 制作一个桌面宠物,好玩!
[DIY]用STM32实现桌面宠物功能#开源
用Python制作一个可以聊天的皮卡丘版桌面宠物
如何用python写一个桌面宠物
【Python程序】用Python写了一个好玩的桌面宠物游戏脚本;源码可分享;打工人必备程序;如何用Python制作一个桌宠
【python】python制作 连连看 游戏脚本(一)
桌面宠物猫好玩吗 桌面宠物猫玩法简介
【Python教程】教你用Python代码制作一个桌面宠物,专属桌宠,体验感升级1000%(附源码)

网址: 用Python写了一个好玩的桌面宠物游戏脚本,简单又好玩 https://m.mcbbbk.com/newsview1021220.html

所属分类:萌宠日常
上一篇: 宠物战争模拟器游戏
下一篇: 给狗狗洗澡啦!