引言:是否还记得曾经风靡全国的QQ宠物呢,那个又调皮又可爱的QQ企鹅,偶尔还会生病撒娇,需要培养的虚拟宠物,你是否还记得它的样子呢。它是腾讯公司推出的第一款QQ宠物系列游戏,也是运营了很久的系列宠物游戏。该游戏无级别限制,贯穿宠物成长全过程,包括打工、学习、旅游、游戏、结婚、生蛋等。但是该游戏已于2018年9月14日下架,全服关闭,我们为此感到可惜,但是今天我们就将走上他们曾经的制作之路实现我们的桌面宠物,手把手从零制作。而今天我们就将借助python实现自己的宠物,会说话,并且还可以控制电脑等智能功能,首先让我们看一下我们的效果。
class newWindow(QWidget):
def __init__(self, parent=None):
super(newWindow,
self).__init_
_(parent)
self.i =
1
self.mypix()
self.move(
1750,
50)
self.timer = QTimer()
self.timer.setInterval(
500)
self.timer.timeout.connect(
self.timeChanged)
self.timer.start()
self.setWindowFlags(Qt.FramelessWindowHint)
# 去除界面边框
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# 背景透明
self.setMouseTracking(False)
# 设置鼠标移动跟踪是否有效
self.initUI()
def mypix1(self):
self.update()
if
self.i ==
6
:
#散步
time.sleep(
5)
elif
self.i ==
11
:
#甩膀子
time.sleep(
1)
elif
self.i ==
16
:
#唱歌
time.sleep(
1)
elif
self.i ==
21
:
#拖东西
time.sleep(
1)
elif
self.i ==
26
:
#坐在地上无聊
time.sleep(
1)
elif
self.i ==
31
:
#坐在地上沮丧
time.sleep(
1)
elif
self.i ==
36
:
#爬起来
time.sleep(
1)
elif
self.i ==
43
:
#准备变红
time.sleep(
1)
elif
self.i ==
48
:
#变红
time.sleep(
1)
elif
self.i ==
54
:
#红色状态
time.sleep(
1)
elif
self.i ==
56
:
#返回
self.i =
1
time.sleep(
2)
self.mypic = {
1:
'.\img\shime1.png',
2:
'.\img\shime1.png',
3:
'.\img\shime1.png',
4:
'.\img\shime1.png',
5:
'.\img\shime1.png',
6:
'.\img\shime2.png',
7:
'.\img\shime3.png',
8:
'.\img\shime2.png',
9:
'.\img\shime3.png' ,
10:
'.\img\shime3.png',
11:
'.\img\shime5.png',
12:
'.\img\shime6.png',
13:
'.\img\shime5.png',
14:
'.\img\shime6.png',
15:
'.\img\shime6.png',
16:
'.\img\shime47.png',
17:
'.\img\shime48.png',
18:
'.\img\shime49.png',
19:
'.\img\shime50.png',
20:
'.\img\shime50.png',
21:
'.\img\shime38.png',
22:
'.\img\shime39.png',
23:
'.\img\shime40.png',
24:
'.\img\shime41.png',
25:
'.\img\shime30.png',
26:
'.\img\shime31.png',
27:
'.\img\shime32.png',
28:
'.\img\shime33.png',
29:
'.\img\shime33.png',
30:
'.\img\shime16.png',
31:
'.\img\shime15.png',
32:
'.\img\shime16.png' ,
33:
'.\img\shime17.png',
34:
'.\img\shime18.png',
35:
'.\img\shime19.png',
36:
'.\img\shime19.png',
37:
'.\img\shime19.png',
38:
'.\img\shime20.png',
39:
'.\img\shime20.png',
40:
'.\img\shime21.png' ,
41:
'.\img\shime22.png',
42:
'.\img\shime26.png',
43:
'.\img\shime27.png',
44:
'.\img\shime28.png',
45:
'.\img\shime29.png',
46:
'.\img\shime30.png',
47:
'.\img\shime47.png',
48:
'.\img\shime46.png',
49:
'.\img\shime45.png',
50:
'.\img\shime44.png',
51:
'.\img\shime43.png',
52:
'.\img\shime42.png',
53:
'.\img\shime42.png',
54:
'.\img\shime43.png',
55:
'.\img\shime42.png',
56:
'.\img\shime43.png'}
self.pix = QPixmap(
self.mypic[
self.i],
'0', Qt.AvoidDither
| Qt.ThresholdAlphaDither | Qt.ThresholdDither)
self.resize(
self.pix.size())
self.setMask(
self.pix.mask())
self.dragPosition = None
def mousePressEvent(self, event):
QtCore.Qt.NoButton -
0 - 没有按下鼠标键
QtCore.Qt.LeftButton -
1 -按下鼠标左键
QtCore.Qt.RightButton -
2 -按下鼠标右键
QtCore.Qt.Mion 或 QtCore.Qt.MiddleButton -
4 -按下鼠标中键
nn = event.buttons()
def mouseReleaseEvent(self, event):
print(
'鼠标键放开了')
# 显示不规则图片
def mypix(self):
self.update()
self.pix=QPixmap(
'.\img\shime1.png',
'0',Qt.AvoidDither
|Qt.ThresholdAlphaDither|Qt.ThresholdDither)
self.resize(
self.pix.size())
self.setMask(
self.pix.mask())
def mouseMoveEvent(self, event):
# 鼠标键移动时调用
ret =
self.hasMouseTracking()
#print('鼠标移动了:%s' % ret)
x = event.x()
# 返回鼠标相对于窗口的x轴坐标
y = event.y()
# 返回鼠标相对于窗口的y轴坐标
# print('鼠标x坐标:%s ,鼠标y坐标:%s' % (x, y))
xy = event.pos()
s =
self.mapToGlobal(xy)
# 将窗口坐标转换成屏幕坐标.属于QWidget类的方法;参数类型QPoint
#print('鼠标x坐标:%s ,鼠标y坐标:%s' % (s.x(), s.y()))
self.move(s.x()-
75, s.y()-
100)
self.update()
self.pix = QPixmap(
'.\img\shime4.png',
'0', Qt.AvoidDither
| Qt.ThresholdAlphaDither | Qt.ThresholdDither)
self.resize(
self.pix.size())
self.setMask(
self.pix.mask())
xy1 = event.globalPos()
# 返回鼠标相对于屏幕的坐标。PyQt5.QtCore.QPoint(1096, 37)【用xy1.x() xy1.y()提取值】
s1 =
self.mapFromGlobal(xy1)
# 将屏幕坐标转换成窗口坐标.属于QWidget类的方法;参数类型QPoint
# mapToParent(QPoint) - 将窗口坐标转换成父窗口坐标。如果没有父窗口,则相当于mapToGlobal (QPoint)
# mapFromParent(QPoint) - 将父窗口坐标转换成窗口坐标。如果没有父窗口,则相当于mapFromGlobal(QPoint)
# mapTo (QWidget, QPoint) - 将窗口坐标转换成 QWidget父窗口坐标
px = event.globalX()
# 返回相对于屏幕的x坐标
py = event.globalY()
# 返回相对于屏幕的y坐标
s = event.windowPos()
# 相对于窗口的坐标(保留一位小数),PyQt5.QtCore.QPointF(481.0, 1.0)【用s.x() s.y()提取值】
p = event.screenPos()
# 相对于屏幕的坐标(保留一位小数).PyQt5.QtCore.QPointF(476.0, 49.0)【用p.x() p.y()提取值】
t = event.timestamp()
def contextMenuEvent(self, e):
"""右键菜单"""
cmenu = QMenu(self)
act1 = cmenu.addAction(
"语音聊天")
act2 = cmenu.addAction(
"语音控制")
act3 = cmenu.addAction(
"WiFi连接")
act4 = cmenu.addAction(
"关机")
act5 = cmenu.addAction(
"退出")
act6 = cmenu.addAction(
"待续")
action = cmenu.exec_(self.mapToGlobal(e.pos()))
if action == act5:
qApp.quit()
elif action == act1:
os.system(
"python F:/代码/python/语音识别/语音聊天机器人/user.py")
elif action == act2:
os.startfile(
r"F:\代码\python\语音识别\语音智能控制\recognize.exe")
elif action == act3:
os.startfile(
r'C:\Users\asus\Desktop\功能\断网连接.vbs')
elif action == act4:
os.system(
"shutdown -p")
elif action == act6:
self.parent_window.show()
if __name__ ==
'__main__':
app = QApplication(sys.argv)
demo = newWindow()
demo.show()
sys.
exit(app.exec())
热 文 推 荐