python教程

python开发一个桌面僵尸宠物代码

我的站长站 2021-06-14 人阅读

python开发一个桌面行走的僵尸宠物代码,可切换僵尸皮肤,效果如下:

python开发一个桌面僵尸宠物代码
Python代码截图

python代码如下

# *_* coding : UTF-8 *_*
# author  :  Leemamas
# 开发时间  :  2021/5/28  0:48
 
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
 
class TablePet(QWidget):
    def __init__(self):
        super(TablePet, self).__init__()
 
 
        self.is_follow_mouse = False
        self.mouse_drag_pos = self.pos()
        # 每隔一段时间做个动作
        self.timer = QTimer()
        self.timer.timeout.connect(self.randomAct)
        self.timer.start(100)
 
        ##僵尸形态
        self.sharp=1
        ##皇帝的新衣
        self.clothes = ''
        self.clothes_key = 0
        self.wardrobe(self.sharp)
 
        self.initUi()
        self.tray()
 
 
    ##衣柜
    def wardrobe(self,key):
 
        default = 'sourceZombieZombie_'
        battle = 'sourceConeheadZombieConeheadZombie_'
        if key == 0:
            self.clothes=default
            ##TODO 根据文件数自动获取
            self.clothes_key=21
        else:
            self.clothes=battle
            self.clothes_key=20
 
 
    ##变身
    def transformation(self):
        if self.sharp==0:
            self.wardrobe(1)
            self.sharp=1
        else:
            self.wardrobe(0)
            self.sharp=0
 
    def randomAct(self):
        # 读取图片不同的地址,实现动画效果
        if self.key<self.clothes_key:
            self.key+=1
        else:
            self.key=0
 
        self.pic_url = self.clothes + str(self.key) + '.png'
        self.pm = QPixmap(self.pic_url)
        if not self.is_follow_mouse:
            # 实现行进效果
            if self.w>0:
                self.w-=2
            else:
                self.w=1400
            self.move(self.w, self.h)
        self.lbl.setPixmap(self.pm)
 
 
 
    def initUi(self):
        screen = QDesktopWidget().screenGeometry()
        self.w=1400
        self.h=800
        self.setGeometry(self.w,self.h,300,300)
        # self.setWindowTitle('mypet')
        self.lbl = QLabel(self)
        self.key=0
        self.pic_url=self.clothes+str(self.key)+'.png'
        self.pm = QPixmap(self.pic_url)
        self.lbl.setPixmap(self.pm)
 
        # 背景透明等效果
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
        self.setAutoFillBackground(False)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.show()
        # self.repaint()
 
 
    #系统托盘
    def tray(self):
        tp=QSystemTrayIcon(self)
        tp.setIcon(QIcon('sourceZombieZombie_0.png'))
        ation_quit= QAction('QUIT', self, triggered=self.quit)
        transformation= QAction('transformation', self, triggered=self.transformation)
        tpMenu=QMenu(self)
        tpMenu.addAction(ation_quit)
        tpMenu.addAction(transformation)
        tp.setContextMenu(tpMenu)
        tp.show()
 
    #鼠标事件
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.is_follow_mouse = True
            self.mouse_drag_pos = event.globalPos() - self.pos()
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))
 
    def mouseMoveEvent(self, event):
        if Qt.LeftButton and self.is_follow_mouse:
            self.move(event.globalPos() - self.mouse_drag_pos)
            xy=self.pos()
            self.w,self.h=xy.x(),xy.y()
            event.accept()
 
    def mouseReleaseEvent(self, event):
        self.is_follow_mouse = False
        self.setCursor(QCursor(Qt.ArrowCursor))
 
    def quit(self):
        self.close()
        sys.exit()
 
 
if __name__ == '__main__':
    app=QApplication(sys.argv)
    myPet=TablePet()
    sys.exit(app.exec_())

僵尸图片素材下载地址:https://11px.lanzoui.com/iGDvSq6jesd

相关推荐
  • Python代码
  • Python源码
  • iqiyi视频解析Python代码

    最新吾爱大佬分享的一段iqiyi视频解析Python代码,转载分享给大家参考。代码说明m3u8下载部分:pip install m3u8download-hecoter使用需要nodejs项目链接:https://github.com/hecoter/videoParse/tree/main/iqiyipython代码import requestsimport reimpo...

    python教程 105 1年前
  • python开发一个桌面僵尸宠物代码

    python开发一个桌面行走的僵尸宠物代码,可切换僵尸皮肤,效果如下:python代码截图python代码如下# *_* coding : UTF-8 *_*# author : Leemamas# 开发时间 : 2021/5/28 0:48 import sysfrom PyQt5.QtGui import *from PyQt5.QtCore import *from PyQt...

    python教程 216 2年前
  • 获取免费的https代理Python代码

    前言大家用Python爬网页时候,爬快了被封IP,爬慢了,等的着急,这时候就需要https代理来切换IP了。分享一段获取免费的https代理Python代码,可以快速获取网络上免费的https代理。Python代码from multiprocessing.dummy import Lockimport reimport requestsi...

    python教程 92 2年前
  • Json压缩和格式化工具,附Python源码
    Json压缩和格式化工具,附Python源码

    软件介绍一款Json压缩和格式化工具,可以在线Json压缩和格式化。基于Python库开发,附上Python源码,GUI没有美化,巨丑。软件截图Python源码import jsonimport tkinter as tkdef json_compress(json_str...

    开发软件 34 10个月前
  • python打飞机小游戏源码+成品打包

    python源码用的pygame库,自带的random和os。程序运行需要的图片,声音和字体下载链接: https://pan.baidu.com/s/1KItG2usXOM_xcxcdHIixaw 提取码: qmweimport pygameimport randomimport os FPS = 60WIDTH = 500HEIGHT = 600 BLACK = (0, 0, 0)WHITE =...

    python教程 39 10个月前
  • 原创力文库Python爬虫下载源码

    # !/usr/bin/python# -*- coding: UTF-8 -*-import reimport jsonimport osimport shutilimport sysimport timeimport requestsimport img2pdffrom PIL import Image from alive_progress import alive_barfrom requests.exceptions import SSLErro...

    python教程 53 1年前
最新更新