Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to make a desktop pet with a pickup card that you can chat with?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Python to make a chat pickup mound version of the desktop pet, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Development tools

Python version: 3.6.4

Related modules:

PyQt5 module

And some modules that come with Python.

Brief introduction of principle

Since you want to write a desktop pet, the first thing to do is to find the picture material of the pet. Here we use pet picture material from the APP of shimiji, such as Pikachu:

I have downloaded pictures of more than 60 kinds of pets for you to choose from:

It's all packaged and provided in the relevant files, so I won't share the crawler code here (I picked it, and I kept it as long as I didn't think it was particularly ugly), so as not to put unnecessary pressure on other people's servers.

Next, we can start designing our desktop pets. In view of the fact that desktop widgets written in python on the Internet are basically based on tkinter, in order to highlight the difference of official accounts, here we use PyQt5 to implement our desktop pets.

Step realization

First, let's initialize a desktop pet window component:

Class DesktopPet (QWidget): def _ init__ (self, parent=None, * * kwargs): super (DesktopPet, self). _ _ init__ (parent) self.show ()

It works like this:

Next, let's set the properties of the window to make it more suitable as a pet window:

# initialize self.setWindowFlags (Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow) self.setAutoFillBackground (False) self.setAttribute (Qt.WA_TranslucentBackground, True) self.repaint ()

And randomly import a pet image to see how it works:

# randomly import a pet self.pet_images, iconpath = self.randomLoadPetImages () # the currently displayed image self.image = QLabel (self) self.setImage (self.pet_images [0] [0])

The function code for randomly importing all images of a pet is as follows:

Def randomLoadPetImages (self): pet_name = random.choice (list (cfg.PET_ACTIONS_MAP.keys () actions = cfg.PET_ACTIONS_ map [pet _ name] pet_images = [] for action in actions: pet_images.append ([self.loadImage (os.path.join (pet, pet_name)) 'shime'+item+'.png')) for item in action]) iconpath = os.path.join (cfg.ROOT_DIR, pet_name,' shime1.png') return pet_images, iconpath

Of course, we also want the pet's location on the desktop to be random each time, which will be more interesting:

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 (width, height)

Now, when you run our program, it looks like this:

It seems pretty good ~ and so on, there seems to be a problem, after resetting the window properties, how does this thing quit? Does it seem strange to add a symbol like x in the upper right corner of the pet?

Don't worry, we can add a tray icon to our desktop pet to enable the exit function of the desktop pet program:

# set exit option quit_action = QAction ('exit', self, triggered=self.quit) 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 ()

The effect goes like this:

OK, this seems to be a model ~ but it still doesn't seem to be quite right. Every time the pet is generated on the desktop, the position is random, but we can't adjust the pet's position, which is obviously unreasonable. As a desktop pet, you must not interfere with the owner's work position! Why don't we write down the functions when the mouse is pressed, moved, and released, so that we can drag it with the mouse:

When the left mouse button is pressed, the pet will be bound to the mouse position 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)), the pet will also move the def mouseMoveEvent (self) Event): if Qt.LeftButton and self.is_follow_mouse: self.move (event.globalPos ()-self.mouse_drag_pos) event.accept ()''unbind' 'def mouseReleaseEvent (self, event): self.is_follow_mouse = False self.setCursor (QCursor (Qt.ArrowCursor)) when the mouse is released

The effect is as follows:

Haha, it's getting more and more decent ~ finally, as a lively pet, you can't be so rigid and motionless, can you? Should you at least learn to make an expression to amuse the host? OK, let's first set a timer:

# do an action every once in a while self.timer = QTimer () self.timer.timeout.connect (self.randomAct) self.timer.start

The timer switches the pictures of the selected pets from time to time to achieve the animation effect of the pet's facial expressions. (the basic content that the video is made up of frame-by-frame pictures does not need me to popularize science.) Of course, here we must classify the pictures (the pictures that do the same action belong to the same category) to ensure the consistency of the pet's facial expressions. Specifically, the code implementation is as follows:

Def randomAct (self): if not self.is_running_action: self.is_running_action = True self.action_images = random.choice (self.pet_images) self.action_max_len = len (self.action_images) self.action_pointer = 0 self.runFrame ()''complete every frame of the action' 'def runFrame (self): If self.action_pointer = = self.action_max_len: self.is_running_action = False self.action_pointer = 0 self.action_max_len = 0 self.setImage (self.action_ images [self.action _ pointer]) self.action_pointer + = 1

OK, it's done.

Thank you for reading this article carefully. I hope the article "how to use Python to make a pickup card version desktop pet that you can chat with" will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report