Students who have played computer games must be familiar with plug-ins , But have you ever thought about how to make a plug-in when you use it ?
I opened it. 4399 Small game network , Click to open an unknown game , Well, , Sushi maker , There is material on one side , When the guests come, say what they want , Just serve it to him according to the menu ~
First of all, declare , The concept of game plug-in here , It's different from the plug-ins in those large online games , Can't automatically play strange , Can't drink medicine, can't avoid GM…… What's the use of this plug-in ? Good question. , useless , In addition to wasting your time , Improve your programming skills , Add a little bit bit by bit to do plug-in foundation , useless , If you are aiming to make an amazing plug-in that will immediately become supernatural if you don't open it , I'm afraid I'm going to disappoint you , Please make a detour as soon as possible . My goal is simple , Just play this little game automatically .
Need to install autopy and PIL as well as pywin32 package .
autopy It is an automatic operation python library , Can simulate some mouse 、 Keyboard events , You can also access the screen , I was going to use it win32api To simulate the input event , I found it easy to use , The most powerful thing is that it is cross platform , Please search for installation .
PIL That's famous ,Python Image processing No.1, The following will explain what to do with it .
pywin32 It's not necessary , But for convenience ( The mouse is moving by itself , How to end it ), It is recommended to install , oh , I was in win Made on the platform , The plug-in is probably only windows Users need it ?
Screenshots and image processing tools
Screen capture is to obtain game images for analysis of game tips , In fact, there is no special tool to directly Print Screen Paste it into the image processing tool . I use it PicPick, Quite easy to use , And individual users are free . Image processing is to obtain all kinds of information , We'll use it to get the order image and save it , For plug-in analysis and judgment . I use it PhotoShop… Don't tell me Adobe, Actually PicPick The image editor in is also enough , As long as you can check the image coordinates and clip pictures , It's just that I'm used to PS 了 ~
Watch this game , Yes 8 Growing vegetables , Every dish has a fixed practice , Once the customer sits down , There will be a picture above your head , Look at the picture and you'll know what he wants , Click the raw material area on the left , And then click on …… I don't know what it's called , Something like a bamboo slip , The food is finished , Then drag the prepared food to the customer .
The position of the picture on the customer's head is fixed , There are only four positions in total , We can analyze it one by one , The position of raw materials is also fixed , The practice of each dish is even more clear , In this way, we can judge , The program can help us make delicious dishes one by one and serve them , So the money rolled in :)
This command will quickly move the mouse to the specified screen coordinates , You know what screen coordinates are , The upper left corner is (0,0), Then increase right and down , therefore 1024×768 The coordinates in the lower right corner of the screen are …… You guessed it , yes (1023,767).
But some unfortunate , If you actually use this command , And then use autopy.mouse.get_pos() Get the current coordinates , Find it is not (100,100) On , But smaller , For example, my machine is (97,99), It's about resolution . This mobile is for users and windows in mouse_event function , If not clear api Of , It's good to know this , This coordinate is not very accurate . As curious as I am , You can read it autopy Source code , I found that there was a problem with his algorithm for calculating absolute coordinates :
point.x *= 0xFFFF / GetSystemMetrics(SM_CXSCREEN);
Here, divide first and then multiply , Those who have learned some calculation methods should know about integer operation , It should be multiplied and then divided , Otherwise, there will be relatively large errors , If he writes :
point.x = point.x * 0xffff / GetSystemMetrics(SM_CXSCREEN);
It will be much more accurate , Although it will be a little slower in theory , But I'm too lazy to change the code and recompile , A few pixels away , It has little effect on us ~ Let's learn a lesson .
This is simpler , But remember that the operations here are very, very fast , Maybe the game hasn't reacted yet , You're done , So it failed …… So when necessary , please sleep For a moment .
We didn't use the keyboard this time , So I won't say .
How do you do it? ? Just analyze the image on the customer's head , Come on , Start with getting images ~
Open your favorite image editor , Start measuring ~ We need to know where the image is on the screen , You can measure it with a ruler , Originally, direct measurement is also possible , But here I use the position in the upper left corner of the screen ( That is to say 1) As a reference position , So once the picture changes , We only need to modify the coordinates of one point , Otherwise, it is not a happy thing to write every point again .
Look at the image on the leftmost customer avatar , We need two points to determine this range , They are the upper left corner and the lower right corner of the image , That is to say 2 Sum point 3,. There are also three customer positions in the back , Simply add an increment ,for Circulation is born for this !
alike , The location of our raw materials ,“ Bamboo mat ” Location, etc , Can be obtained in this way . Pay attention to the relative position of the upper left corner of the game screen . As for the method of capturing pictures ,PIL Of ImageGrab It's easy to use ,autopy You can also take pictures , Why not , I will talk about .
A rather difficult problem in our plug-in has appeared , How to know which dish is the image we get ? To human eyes …… Even dog eyes , It's all quite easy The problem of ,“ It's easy to see ”! Right , This is where people are superior to machines , We do simple things , The computer can't distinguish clearly .
autopy Image limitations
If you've seen it autopy Of api, You'll find that it has a bitmap package , There are find_bitmap Method , It is to look for small images of samples in a large image . Smart you can think of , We can cut off the whole game , Then prepare small images of all the dishes and use this method to find out which dish is called .
exactly , At the beginning, I also had the impulse to do so , But I gave up immediately …… This method finds images , Speed aside , It has a condition that “ Exactly match ”, There is a pixel on the image RGB It's not worth it 1, It can't find out .
We know flash It's vector drawing , It displays a dot matrix image on the screen after zooming , There are great variables here , Theoretically, the results of the same input and the same algorithm must be consistent , But because of the drawing background , There will always be a little gap , It is this gap that makes this wonderful function unusable ……
ok , It's also a good thing that it can't be used , Otherwise, how can I introduce our excellent image analysis algorithm ?
I'm sure you've used Google Of “ Search by map ” function , without , You are out of date , Go and try ! When you enter a picture , It will show you images similar to this picture , So when you find a favorite picture and want to make wallpaper, you think it's too small , You can basically use this method to find the right ~
We will use a similar principle to judge the user's order , Of course, our algorithm cannot be compared with Google So complicated , Zhihu has a very good article describing this problem , You can have a look if you are interested , I give the implementation directly :
Because this is a method of a class , So there's a self Parameters , Ignore it . there img Should pass in a Image object , You can make the result after reading the image file , It can also be the result of screen capture . And the scaled size (18,13) I made it according to the actual situation , Because the image of the dishes on the customer's Avatar is basically this ratio . It turns out that this proportion is still very important , Because our dishes are a little similar , If the ratio is not appropriate, it will be distorted after compression , It is easy to misjudge ( I've suffered before ).
Get a picture “ The fingerprint ” after , We can compare it with standard picture fingerprints , How to compare , You should use “ Hamming distance ”, That is, the number of different characters in the corresponding positions of two strings . The implementation is also simple ……
def hamming_dist(self, hash1, hash2):
return sum(itertools.imap(operator.ne, hash1, hash2))
Okay , We can use the prepared standard image , Then read the calculation feature code in advance and store it , Then compare the screenshots with them , The one with the smallest distance is the corresponding dish , The code is as follows :
Here's one 50 Initial distance , If the captured image is larger than... Compared with any menu 50, Explain what ?
It shows that the image at that position is not a dish , That means the customer hasn't taken that seat yet , Or we minimize the game ( Here comes the boss ), It's important to do this , So that it doesn't find the closest but completely irrelevant dish to deal with .
The question is simple , We just need to record the ingredients of the menu , Then click the corresponding position to , I wrote it as a class to call :
This is the least technical class in this plug-in :) Please forgive me for not writing notes and doc, Because it's simple , I believe you know .
Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !
Python All directions are Python Sort out the common technical points , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .
If a worker wants to do a good job, he must sharpen his tools first . Study Python Common development software is here , It saves you a lot of time .
When we were watching videos to learn , You can't just move your eyes and brain without hands , A more scientific way to learn is to use them after understanding , At this time, the hand training program is very suitable .
Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .
We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .
Guarantee 100% free
】