0%

不务正业系列:桌宠开发Ⅱ(偶尔更新)

桌宠开发Ⅱ

距离上一个版本的桌宠已经1个月了,最近心血来潮,想回顾一下

这两天写这个东西有点上瘾,通过改 BUG,我对 PyQt5 算是熟悉一些了

DesktopPets_v2.0

这次的桌宠迎来大升级,右键菜单功能补全,大大提高了互动性

共添加了3个功能:

  • 天气预报
  • 爬虫
  • 图片盒子

PS:“爬虫”爬取到的图片会直接被“图片盒子”读取

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import os
import sys
import random
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import time

from Attached.CallWeatherWin import Mainweather
from Attached.crawler import Crawler
from Attached.Blankbox import MainBOX

'''配置信息'''
class Config():
ROOT_DIR = os.path.join(os.path.split(os.path.abspath(__file__))[0], 'resources')
print(ROOT_DIR)
ACTION_DISTRIBUTION = [
['X','X','X','X','5','19','4','18','4','19','4','18','4','19','X','X','X','X','5','6','7','8','9','10'], # 吃撇_0
['1','1a','1b','1c','1d','1','1a','1b','1c','1d'], # 眨眼_1
['1','2','3','2','3','2','3','2','3','2','3','2','3','2','3','2','3''2','3','2','3''2','3','2','3','1','1a','1b'], # 行走_2
['6','6','7','7','8','8','5','5','9','9','10','10','7','7','6','6'], # 拖动_3
['11','11a','11b','11c','11d','11e','11f','11g','11f','11g','11f','11g','11f','11g','11f','11g','11e','11d','11c','11b','11a','11'], # 打哈切_4
['12', '13', '14'], # 爬_5
['19','5','19','4','18','4','19','4','18','4','19','4','18','4','19','4','18','4','5','1','X','X','X','1','1a','1b','1c'], # 触地_6
['20', '21'], # 睡觉_7
['22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a'], # 跳跃_8
['23','23a','23','23a','23b','24','25','26','27','28','29','34','35','36','37','34','35','36','37'], # 举手_9
['15','16','17','26','27','28','29','15','34','17','26','27','28','1','1a','1b'], # 攻击_10
['30','30','30','30','30','30a','30b','30b','30b','30c','30c','30a','30b','30b','30b','30c','30c','31','32','33'], # 打喷嚏_11
['9','19','4','18','x','1b','1c','1d'], # 撞墙_12
]
PET_ACTIONS_MAP = {'pet_1': ACTION_DISTRIBUTION}
for i in range(0): PET_ACTIONS_MAP.update({'pet_%s' % i+1: ACTION_DISTRIBUTION})

'''桌面宠物'''
class DesktopPet(QWidget):
tool_name = '桌面宠物'
stat = [1,2,4,8,9,10,11]
def __init__(self, parent=None, **kwargs):
super(DesktopPet, self).__init__(parent)
self.cfg = Config()
for key, value in kwargs.items():
if hasattr(self.cfg, key): setattr(self.cfg, key, value)
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.quitPet)
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(236, 260)
self.randomPosition()
self.is_running_action = False
self.action_images = []
self.action_pointer = 0
self.action_max_len = 0
self.x = self.pos().x()
self.y = self.pos().y()
self.heading = 0
self.touchdown_key = 0
self.jumpping_key = 0
self.fallingBody()

'''初始化计时器'''
def InitTimer(self,Act,start) -> None:
self.timer = QTimer()
self.timer.timeout.connect(Act)
self.timer.start(start)

'''随机做一个动作'''
def randomAct(self):
if not self.is_running_action:
self.is_running_action = True
self.key = random.randint(0,len(DesktopPet.stat)-1)
self.action = DesktopPet.stat[self.key]
print("action is:"+str(self.action))
self.action_images = self.pet_images[self.action]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.heading = random.randint(0, 1)
if self.action == 2:
if self.heading == 0:
print("now is Right")
elif self.heading == 1:
print("now is Left")
elif self.action == 8:
self.jumpping_key = 1
self.InitTimer(self.randomAct,40)

if self.touchdown_key == 1:
self.action_images = self.pet_images[6]
self.action_max_len = len(self.action_images)
self.touchdown()
elif self.jumpping_key == 1:
self.action_images = self.pet_images[8]
self.action_max_len = len(self.action_images)
self.jumpping()
else:
if self.action == 2:
self.moving()
else:
self.runFrame()

'''完成动作的每一帧'''
def runFrame(self):
if self.action_pointer == self.action_max_len:
if self.action != 3:
time.sleep(0.5)
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.setImage(self.action_images[self.action_pointer].mirrored(self.heading, False))
self.action_pointer += 1

'''动作-移动'''
def moving(self):
if self.action_pointer == self.action_max_len:
time.sleep(0.8)
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
else:
screenRect = QApplication.desktop().screenGeometry()
self.x = self.pos().x()
self.x += int(32 * (0.5 - self.heading))
if (self.heading == 1):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
self.x = self.pos().x()
self.x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.heading = 0
else:
self.move(self.x, self.pos().y())
self.setImage(self.action_images[self.action_pointer].mirrored(0, False))
self.action_pointer += 1
elif (self.heading == 0):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
self.x = self.pos().x()
self.x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.heading = 1
else:
self.move(self.x, self.pos().y())
self.setImage(self.action_images[self.action_pointer].mirrored(1, False))
self.action_pointer += 1

'''动作-撞墙'''
def hitwall(self):
if self.action_pointer == self.action_max_len:
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.move(self.x, self.pos().y())
self.setImage(self.action_images[self.action_pointer].mirrored(self.heading, False))
self.action_pointer += 1

'''动作-坠落'''
def fall(self):
y = self.pos().y()
x = self.pos().x()
y += int(16)
screenRect = QApplication.desktop().screenGeometry()
if (y >= screenRect.height() - self.size().height()):
print('now is kiss the ground')
self.action_pointer = 0
self.touchdown_key = 1
self.InitTimer(self.randomAct,80)
else:
self.move(x,y)
self.setImage(self.pet_images[6][0].mirrored(self.heading, False))

'''动作-触地'''
def touchdown(self):
self.x = self.pos().x()
self.y = self.pos().y()
self.x -= 2
self.y -= 2
if self.action_pointer == self.action_max_len:
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.touchdown_key = 0
else:
self.move(self.x, self.y)
self.setImage(self.action_images[self.action_pointer].mirrored(self.heading, False))
self.action_pointer += 1

'''动作-吃瘪'''
def uncomfortable(self):
if not self.is_running_action:
self.is_running_action = True
self.action = 0
print("action is:" + str(self.action))
self.action_images = self.pet_images[self.action]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.heading = random.randint(0, 1)
else:
self.runFrame()

'''动作-拖动'''
def dragging(self):
if not self.is_running_action:
self.is_running_action = True
self.action = 3
print("action is:" + str(self.action))
self.action_images = self.pet_images[self.action]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.heading = random.randint(0, 1)
else:
self.runFrame()

'''动作-跳跃'''
def jumpping(self):
if self.action_pointer == 0:
self.z = 80
if self.action_pointer == self.action_max_len:
self.fallingBody()
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.jumpping_key = 0
self.z = 80
else:
screenRect = QApplication.desktop().screenGeometry()
self.x = self.pos().x()
self.y = self.pos().y()
self.x += int(48 * (0.5 - self.heading))
self.y -= self.z
self.z -= 5
if (self.heading == 1):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
self.x = self.pos().x()
self.x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.fallingBody()
self.heading = 0
else:
self.move(self.x, self.y)
self.setImage(self.action_images[self.action_pointer].mirrored(0, False))
self.action_pointer += 1
elif (self.heading == 0):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
x = self.pos().x()
x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.fallingBody()
self.heading = 1
else:
self.move(self.x, self.y)
self.setImage(self.action_images[self.action_pointer].mirrored(0, False))
self.action_pointer += 1

'''设置当前显示的图片'''
def setImage(self, image):
self.image.setPixmap(QPixmap.fromImage(image))

'''随机导入一个桌面宠物的所有图片'''
def randomLoadPetImages(self):
cfg = self.cfg
pet_name = random.choice(list(cfg.PET_ACTIONS_MAP.keys()))
actions = cfg.PET_ACTIONS_MAP[pet_name]
pet_images = []
self.flag = 0
for action in actions:
patch = [self.loadImage(os.path.join(cfg.ROOT_DIR, pet_name, 'shime'+item+'.png')) for item in action]
pet_images.append(patch)
iconpath = os.path.join(cfg.ROOT_DIR, pet_name, 'shimeX.png')
return pet_images, iconpath

'''鼠标左右键功能'''
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.InitTimer(self.dragging,80)
self.is_follow_mouse = True
self.mouse_drag_pos = event.globalPos() - self.pos()
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
elif event.button() == Qt.RightButton:
self.InitTimer(self.uncomfortable,80)
self.menu = QMenu(self)
self.weather = self.menu.addAction("天气预报")
self.crawler = self.menu.addAction("启动爬虫")
self.imagebox = self.menu.addAction("图片盒子")
self.quitA = self.menu.addAction("退出附属程序")
self.quit = self.menu.addAction("退出桌宠")
self.choice = self.menu.exec_(self.mapToGlobal(event.pos()))

if (self.choice == self.weather):
print("weather")
self.weatherGet()
if (self.choice == self.crawler):
print("crawler")
self.crawlerStart()
if (self.choice == self.imagebox):
print("imagebox")
self.imageBox()
if (self.choice == self.quitA):
print("quitA")
self.quitAttached()
if (self.choice == self.quit):
print("quit")
self.quitPet()

self.randomAct()

'''鼠标移动, 则宠物也移动'''
def mouseMoveEvent(self, event):
if Qt.LeftButton and self.is_follow_mouse:
self.move(event.globalPos() - self.mouse_drag_pos)
event.accept()

'''鼠标释放时, 取消绑定'''
def mouseReleaseEvent(self, event):
self.is_follow_mouse = False
self.setCursor(QCursor(Qt.ArrowCursor))
self.fallingBody()

"""宠物自由落体"""
def fallingBody(self):
x = self.pos().x()
y = self.pos().y()
print("x=%x : y=%x" %(x,y))
self.InitTimer(self.fall,25)

'''导入图像'''
def loadImage(self, imagepath):
image = QImage()
image.load(imagepath)
return image

'''随机到一个屏幕上的某个位置'''
def randomPosition(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(int(width), int(height))

'''天气预报'''
def weatherGet(self):
wea.show()
self.fallingBody()

'''启动爬虫'''
def crawlerStart(self):
pets.close()
cra.Start()
pets.show()
self.fallingBody()

'''图片盒子'''
def imageBox(self):
box.show()
self.fallingBody()

'''退出附属程序'''
def quitAttached(self):
wea.close()
print("Everything is done!!!")

'''退出桌宠'''
def quitPet(self):
self.close()
sys.exit()

if __name__=="__main__":
app = QApplication(sys.argv)
pets = DesktopPet()
wea = Mainweather()
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap("D:\PythonProject\images\YHellow.png")))
wea.setPalette(palette)
cra = Crawler()
box = MainBOX()
pets.show()
sys.exit(app.exec_())

更新日志:

  • version:v2.0
  • date:2022.5.14
  • type:
    • Features:
      • 新添右键功能:天气预报
      • 新添右键功能:爬虫
      • 新添右键功能:图片盒子
      • 新添右键功能:关闭所有附属程序
    • Changed:NULL
    • Removed:NULL
  • desc:
    • 想做的差不多都做了,下次试试连接网易云的 API

DesktopPets_v2.1

新加入“网易云API”选项,其本质是一个爬虫,可以在 UI 界面选择歌曲

PS:由于本人不会并发,所以在“网易云爬虫”爬歌的时候,桌宠和 UI 界面都会卡顿

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import os
import sys
import random
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import requests
import time

from Attached.CallWeatherWin import Mainweather
from Attached.crawler import Crawler
from Attached.Blankbox import MainBOX
from Attached.Netease import Netease

'''配置信息'''
class Config():
ROOT_DIR = os.path.join(os.path.split(os.path.abspath(__file__))[0], 'resources')
print(ROOT_DIR)
ACTION_DISTRIBUTION = [
['X','X','X','X','5','19','4','18','4','19','4','18','4','19','X','X','X','X','5','6','7','8','9','10'], # 吃撇_0
['1','1a','1b','1c','1d','1','1a','1b','1c','1d'], # 眨眼_1
['1','2','3','2','3','2','3','2','3','2','3','2','3','2','3','2','3''2','3','2','3''2','3','2','3','1','1a','1b'], # 行走_2
['6','6','7','7','8','8','5','5','9','9','10','10','7','7','6','6'], # 拖动_3
['11','11a','11b','11c','11d','11e','11f','11g','11f','11g','11f','11g','11f','11g','11f','11g','11e','11d','11c','11b','11a','11'], # 打哈切_4
['12', '13', '14'], # 爬_5
['19','5','19','4','18','4','19','4','18','4','19','4','18','4','19','4','18','4','5','1','X','X','X','1','1a','1b','1c'], # 触地_6
['20', '21'], # 睡觉_7
['22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a','22','22a'], # 跳跃_8
['23','23a','23','23a','23b','24','25','26','27','28','29','34','35','36','37','34','35','36','37'], # 举手_9
['15','16','17','26','27','28','29','15','34','17','26','27','28','1','1a','1b'], # 攻击_10
['30','30','30','30','30','30a','30b','30b','30b','30c','30c','30a','30b','30b','30b','30c','30c','31','32','33'], # 打喷嚏_11
['9','19','4','18','x','1b','1c','1d'], # 撞墙_12
]
PET_ACTIONS_MAP = {'pet_1': ACTION_DISTRIBUTION}
for i in range(0): PET_ACTIONS_MAP.update({'pet_%s' % i+1: ACTION_DISTRIBUTION})

'''桌面宠物'''
class DesktopPet(QWidget):
tool_name = '桌面宠物'
stat = [1,2,4,8,9,10,11]
def __init__(self, parent=None, **kwargs):
super(DesktopPet, self).__init__(parent)
self.cfg = Config()
for key, value in kwargs.items():
if hasattr(self.cfg, key): setattr(self.cfg, key, value)
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.quitPet)
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(236, 260)
self.randomPosition()
self.is_running_action = False
self.action_images = []
self.action_pointer = 0
self.action_max_len = 0
self.x = self.pos().x()
self.y = self.pos().y()
self.heading = 0
self.touchdown_key = 0
self.jumpping_key = 0
self.fallingBody()

'''初始化计时器'''
def InitTimer(self,Act,start) -> None:
self.timer = QTimer()
self.timer.timeout.connect(Act)
self.timer.start(start)

'''随机做一个动作'''
def randomAct(self):
if not self.is_running_action:
self.is_running_action = True
self.key = random.randint(0,len(DesktopPet.stat)-1)
self.action = DesktopPet.stat[self.key]
print("action is:"+str(self.action))
self.action_images = self.pet_images[self.action]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.heading = random.randint(0, 1)
if self.action == 2:
if self.heading == 0:
print("now is Right")
elif self.heading == 1:
print("now is Left")
elif self.action == 8:
self.jumpping_key = 1
self.InitTimer(self.randomAct,40)

if self.touchdown_key == 1:
self.action_images = self.pet_images[6]
self.action_max_len = len(self.action_images)
self.touchdown()
elif self.jumpping_key == 1:
self.action_images = self.pet_images[8]
self.action_max_len = len(self.action_images)
self.jumpping()
else:
if self.action == 2:
self.moving()
else:
self.runFrame()

'''完成动作的每一帧'''
def runFrame(self):
if self.action_pointer == self.action_max_len:
if self.action != 3:
time.sleep(0.5)
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.setImage(self.action_images[self.action_pointer].mirrored(self.heading, False))
self.action_pointer += 1

'''动作-移动'''
def moving(self):
if self.action_pointer == self.action_max_len:
time.sleep(0.8)
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
else:
screenRect = QApplication.desktop().screenGeometry()
self.x = self.pos().x()
self.x += int(32 * (0.5 - self.heading))
if (self.heading == 1):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
self.x = self.pos().x()
self.x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.heading = 0
else:
self.move(self.x, self.pos().y())
self.setImage(self.action_images[self.action_pointer].mirrored(0, False))
self.action_pointer += 1
elif (self.heading == 0):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
self.x = self.pos().x()
self.x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.heading = 1
else:
self.move(self.x, self.pos().y())
self.setImage(self.action_images[self.action_pointer].mirrored(1, False))
self.action_pointer += 1

'''动作-撞墙'''
def hitwall(self):
if self.action_pointer == self.action_max_len:
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.move(self.x, self.pos().y())
self.setImage(self.action_images[self.action_pointer].mirrored(self.heading, False))
self.action_pointer += 1

'''动作-坠落'''
def fall(self):
y = self.pos().y()
x = self.pos().x()
y += int(16)
screenRect = QApplication.desktop().screenGeometry()
if (y >= screenRect.height() - self.size().height()):
print('now is kiss the ground')
self.action_pointer = 0
self.touchdown_key = 1
self.InitTimer(self.randomAct,80)
else:
self.move(x,y)
self.setImage(self.pet_images[6][0].mirrored(self.heading, False))

'''动作-触地'''
def touchdown(self):
self.x = self.pos().x()
self.y = self.pos().y()
self.x -= 2
self.y -= 2
if self.action_pointer == self.action_max_len:
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.touchdown_key = 0
else:
self.move(self.x, self.y)
self.setImage(self.action_images[self.action_pointer].mirrored(self.heading, False))
self.action_pointer += 1

'''动作-吃瘪'''
def uncomfortable(self):
if not self.is_running_action:
self.is_running_action = True
self.action = 0
print("action is:" + str(self.action))
self.action_images = self.pet_images[self.action]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.heading = random.randint(0, 1)
else:
self.runFrame()

'''动作-拖动'''
def dragging(self):
if not self.is_running_action:
self.is_running_action = True
self.action = 3
print("action is:" + str(self.action))
self.action_images = self.pet_images[self.action]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.heading = random.randint(0, 1)
else:
self.runFrame()

'''动作-跳跃'''
def jumpping(self):
if self.action_pointer == 0:
self.z = 80
if self.action_pointer == self.action_max_len:
self.fallingBody()
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.jumpping_key = 0
self.z = 80
else:
screenRect = QApplication.desktop().screenGeometry()
self.x = self.pos().x()
self.y = self.pos().y()
self.x += int(48 * (0.5 - self.heading))
self.y -= self.z
self.z -= 5
if (self.heading == 1):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
self.x = self.pos().x()
self.x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.fallingBody()
self.heading = 0
else:
self.move(self.x, self.y)
self.setImage(self.action_images[self.action_pointer].mirrored(0, False))
self.action_pointer += 1
elif (self.heading == 0):
if ((self.x <= 0 and self.heading == 1) or (
self.x >= screenRect.width() - self.size().width() and self.heading == 0)):
print('now is hit wall')
x = self.pos().x()
x += int(32)
self.action_images = self.pet_images[12]
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.hitwall()
self.fallingBody()
self.heading = 1
else:
self.move(self.x, self.y)
self.setImage(self.action_images[self.action_pointer].mirrored(0, False))
self.action_pointer += 1

'''设置当前显示的图片'''
def setImage(self, image):
self.image.setPixmap(QPixmap.fromImage(image))

'''随机导入一个桌面宠物的所有图片'''
def randomLoadPetImages(self):
cfg = self.cfg
pet_name = random.choice(list(cfg.PET_ACTIONS_MAP.keys()))
actions = cfg.PET_ACTIONS_MAP[pet_name]
pet_images = []
self.flag = 0
for action in actions:
patch = [self.loadImage(os.path.join(cfg.ROOT_DIR, pet_name, 'shime'+item+'.png')) for item in action]
pet_images.append(patch)
iconpath = os.path.join(cfg.ROOT_DIR, pet_name, 'shimeX.png')
return pet_images, iconpath

'''鼠标左右键功能'''
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.InitTimer(self.dragging,80)
self.is_follow_mouse = True
self.mouse_drag_pos = event.globalPos() - self.pos()
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
elif event.button() == Qt.RightButton:
self.InitTimer(self.uncomfortable,80)
self.menu = QMenu(self)
self.weather = self.menu.addAction("天气预报")
self.crawler = self.menu.addAction("启动爬虫")
self.music = self.menu.addAction("网易云API")
self.imagebox = self.menu.addAction("图片盒子")
self.quitA = self.menu.addAction("退出附属程序")
self.quit = self.menu.addAction("退出桌宠")
self.choice = self.menu.exec_(self.mapToGlobal(event.pos()))

if (self.choice == self.weather):
print("weather")
self.weatherGet()
if (self.choice == self.crawler):
print("crawler")
self.crawlerStart()
if (self.choice == self.music):
print("music")
self.Netease()
if (self.choice == self.imagebox):
print("imagebox")
self.imageBox()
if (self.choice == self.quitA):
print("quitA")
self.quitAttached()
if (self.choice == self.quit):
print("quit")
self.quitPet()

self.randomAct()

'''鼠标移动, 则宠物也移动'''
def mouseMoveEvent(self, event):
if Qt.LeftButton and self.is_follow_mouse:
self.move(event.globalPos() - self.mouse_drag_pos)
event.accept()

'''鼠标释放时, 取消绑定'''
def mouseReleaseEvent(self, event):
self.is_follow_mouse = False
self.setCursor(QCursor(Qt.ArrowCursor))
self.fallingBody()

"""宠物自由落体"""
def fallingBody(self):
x = self.pos().x()
y = self.pos().y()
print("x=%x : y=%x" %(x,y))
self.InitTimer(self.fall,25)

'''导入图像'''
def loadImage(self, imagepath):
image = QImage()
image.load(imagepath)
return image

'''随机到一个屏幕上的某个位置'''
def randomPosition(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(int(width), int(height))

'''天气预报'''
def weatherGet(self):
wea.show()
self.fallingBody()

'''启动爬虫'''
def crawlerStart(self):
pets.close()
cra.Start()
pets.show()
self.fallingBody()

def Netease(self):
mus.show()
self.fallingBody()

'''图片盒子'''
def imageBox(self):
box.show()
self.fallingBody()

'''退出附属程序'''
def quitAttached(self):
wea.close()
box.close()
mus.close()
print("Everything is done!!!")

'''退出桌宠'''
def quitPet(self):
self.close()
sys.exit()

if __name__=="__main__":
app = QApplication(sys.argv)
pets = DesktopPet()
wea = Mainweather()
mus = Netease()
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap("D:\PythonProject\images\YHellow.png")))
wea.setPalette(palette)
mus.setPalette(palette)
cra = Crawler()
box = MainBOX()
pets.show()
sys.exit(app.exec_())

更新日志:

  • version:v2.1
  • date:2022.5.20
  • type:
    • Features:
      • 新添右键功能:网易云API
    • Changed:NULL
    • Removed:NULL
  • desc:
    • 以我的技术,感觉已经到极限了,可能会有好长一段时间都不会更新了