Teach you to use Python to create game plug

Source: Internet
Author: User
Tags range

Played a computer game of the students to plug is certainly not unfamiliar, but you use the plug-in when did you think how to do a plug-in it? (Of course, with the plug-in is not so moral ha, hehe), then we will look at how to use Python to create a plug-in ....

I opened 4399 Games Network, point opened an unknown game, well, sushi division, there is material on one side, guests come to say their requirements, you follow the menu to give him a good ~ why so difficult? 8 Kinds of menu can not remember clearly, point to point on the wrong, the mouse is not so muscle strain what the injury ah ...

First of all to declare that the concept of the game plug-in here, and those large online games in the plug-in can be different, can not automatically play strange, can not drink medicine can not avoid GM ... So what's the use of this plug-in? Ask good, useless, besides can waste you a little time, improve the programming technology, add a little bit of dot dot to do outside the base, no use, if you are to make a surprise heaven and earth is not open to the gods of the plug-in as a goal to come, I am afraid to let you down, please take a detour early. My goal is very simple, is automatically play this game just.

Preparation of tools

You need to install Autopy and PIL as well as pywin32 packages. Autopy is a python library of automated operations, can simulate some mouse, keyboard events, but also access to the screen, I would like to use WIN32API to simulate the input event, found that this use is relatively simple, the most powerful is that it is cross-platform, please search for installation; and PiL that's famous. , Python image processing No.1, below will explain what to do with it; Pywin32 is not necessary, but for the convenience (the mouse is moving itself, how to end it), or recommend installation, oh yes, I was on the win platform to do, Plug-in probably only Windows users need it?

Screenshots and image processing tools

Screenshot is to get the game image for analysis game hints, in fact, no special tools directly print screen paste into the image processing tool can also. I use the Picpick, quite good, and personal users are free, and image processing is to obtain a variety of information, we want to use it to get a la carte image to save, for the analysis of the external evaluation. I'm using Photoshop ... Do not tell Adobe, in fact, the Picpick image editor is enough, as long as you can view the image coordinates and clip pictures are very hungry, but I am used to PS ~

Editor

I don't have to say that, write code need an editor Ah! I use VIM, if you are willing to use WordPad can also ...

Principle Analysis

Plug the history of what I do not want to say, interested in Google or the mother (note: Non-technical problems can be Baidu).

Watch this game, there are 8 of vegetables, every dish has a fixed approach, once the customer sat down, the top of the head will have a picture, see the picture to know what he wants to order, click the left raw material area, and then click ... Do not know what to call, like a bamboo slips the same thing, the dish is finished, and then the good food dragged to the customer before.

The location of the picture on the customer's head is fixed, in total, there are only four locations, we can analyze one by one, and the location of raw materials is also fixed, the practice of each dish is clear, so we can judge, the procedure can be very good for us to make a part of the food and serve, so the money rolling to:

Autopy Introduction

GitHub has a very good introductory article, although it is English but very simple, but I still pick a few this time with the instructions to show that I am very hard-working.

Move mouse

1 Import autopy

2 autopy.mouse.move (100, 100) # Move mouse

3 Autopy.mouse.smooth_move (400, 400) # Smooth moving the mouse (the one above is instantaneous)

This command will allow the mouse to quickly move to the specified screen coordinates, you know what is the screen coordinates, the upper left corner is (0,0), and then to the right downward increments, so 1024x768 screen in the lower right corner coordinates ... You guessed it, yes (1023,767).

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Unfortunately, if you actually use this command, and then use Autopy.mouse.get_pos () to get the current coordinates, you find that it is not on (100,100), but smaller, such as My machine is (97,99), and resolution. This move is the user and Windows Mouse_event function, if not clear API, know this thing is good, this is not very accurate coordinates. As I am very curious, you can read the source code of Autopy, I found that he calculated the absolute coordinate algorithm has problems:

Point.x *= 0xffff/getsystemmetrics (Sm_cxscreen);

Here to do division and multiplication, learned a little calculation method should know for the integer operation, should be multiplied by the addition, otherwise it will produce a relatively large error, if he wrote:

Point.x = Point.x * 0xffff/getsystemmetrics (Sm_cxscreen);

will be more accurate, although the theory will be a little bit slower, but I also do not bother to change the code recompile, a few pixels, here on our influence is not ~ I have to learn.

Click the mouse

1 #引入autopy模块

2 # * * *

3 Import Autopy

4 Autopy.mouse.click () # click

5 Autopy.mouse.toggle (True) # Press left button

6 Autopy.mouse.toggle (False) # Loosen left button

This is relatively simple, but remember that the operation here is very very fast, it is possible that the game has not responded to it, you completed, and then failed ... So sleep a little while you need it.

Keyboard operation

We didn't use the keyboard this time, so I won't say it.

How do you do it? To analyze the image on the customer's head, come on, start by getting the image.

Open your favorite image editor and start measuring it ~ we need to know where the image is on the screen, can use the ruler quantity, originally the direct quantity also can, but I use here the picture the upper left corner position (namely point 1) as the reference position, so once the picture has the change, we only need to modify one point coordinates to be good , otherwise it is not a happy thing to rewrite every point.

Look at the top left of the customer image above, we need two points to determine this range, respectively, is the image of the upper left corner and the lower right corner, that is, point 2 and point 3. There are three more customers in the back of the position, simply add an increment on the good, for the loop is for this and life!

Similarly, the position of our raw material, the position of "mats" and so on, can be obtained by this method. Note that the relative position in the upper-left corner of the game screen is obtained. As for the method of grasping the graph, the imagegrab of PIL is very easy to use, autopy can also catch a picture, why not, I will say below.

Analyzing images

We have a very difficult problem in this plug-in appeared, how to know the image we obtained is which dish? To the human eye ... Even the dog's eyes, this is a fairly easy question, "See To Know!" Yes, this is the place where people are smarter than machines, we do things that are simple, but the computer is silly to know.

Autopy Image Limitations

If you've seen the Autopy API, you'll find that it has a bitmap package with a Find_bitmap method that looks for small images in a large image. Smart you can think of, we can intercept the entire game screen, and then prepare all the small image of the dishes with this method to find out what the dish was called. Yes, I had the urge to do it at first, but I gave it up right away ... This method to find the image, the speed does not say, it has a condition is "exact match", the image has a pixel RGB value of 1, it can not find out. We know that Flash is a vector drawing, it has a dot matrix image displayed on the screen is scaled, where the variables are very large, the theory of the same input the same algorithm results must be consistent, but because of the relationship between the drawing background, there will always be a little gap, That's the difference that makes this wonderful function unusable ...

Well, it's good not to use it, otherwise how can I draw our clever image analysis algorithm?

The principle of similarity image search

I believe you must have used Google's "according to map search" function, if not, you are outdated, go to try! When you enter a picture, it will be similar to this image is presented to you, so when you find a favorite figure want to do wallpaper and feel too small, basically can use this method to find the right ~

We will use the same principle to judge the user's order, of course, our algorithm is not as complex as Google, know that there is a very good article describes the problem, interested can see, I directly give the implementation:

1 def get_hash (Self, img):

2 #使用PIL模块缩放图片, * * *

3 image = Img.resize ((a), Image.antialias). Convert ("L")

4 pixels = list (Image.getdata ())

5 avg = SUM (pixels)/len (pixels)

6 return "" ". Join (Map (Lambda P:" 1 "if p > Avg Else" 0 ", pixels))

Because this is a method of the class, there is a self argument that ignores it. The IMG here should pass in an Image object that allows you to read the result of the image file, or the result of the screenshot. And the size of the scale (18,13) is based on my actual situation, because the customer's head on the dishes on the image is basically this ratio. It turns out that this ratio is very important, because our dishes are a bit similar, if the proportion is not suitable for the distortion after compression, easy to misjudge (I had to suffer before).

Get a picture of the "fingerprint", we can compare with the standard picture fingerprint, how to compare it, should use "Hamming distance", that is, two strings corresponding to the number of different characters in the position. The implementation is also very simple ...

def hamming_dist (self, HASH1, HASH2):

return sum (Itertools.imap (operator.ne, HASH1, HASH2))

Well, we can use the standard images we have prepared, then store them in advance, then we'll take a screenshot and compare them, and the smallest is the corresponding dish, the code reads as follows:

def order (self, I):
       l, t = self.left + i * self.step, self.top
       r, B = L + self.width, T + self.height
       HASH2 = Self.get_hash (Imagegrab.grab (L, T, R, b)))
       (MI, dist) = None, for
       I, hash1 in Enumerate (self.maps):
           if has H1 is None:
               continue
           this_dist = Self.hamming_dist (Hash1, HASH2)
           if this_dist < dist:
               mi = i
               Dist = this_dist return
       mi

Here is a 50 initial distance, if the intercepting image is greater than 50 compared to any menu, what does it mean? It means that the image at that location is not a dish, that is, the customer has not yet taken the position, or we have minimized the game (the Boss is here), so it is important to avoid it at random to find one of the most similar but completely not the side of the dish to deal with.

Automatic cooking

The problem is simple, we just need to put the menu material on the record, and then click on the appropriate location, I wrote it as a class to invoke:

Class Menu:
   def __init__ (self):
       self.stuff_pos = []
       self.recipes = [None] * 8
       self.init_stuff ()
       Self.init_recipe ()
   def init_stuff (self):
       for I in range (9):
           self.stuff_pos.append ((L + 102 + (i% 3) *, T + 303 + (I/3) *)
   def init_recipe (self):
       self.recipes[0] = (1, 2)
       self.recipes[1] = (0, 1, 2)
       sel F.RECIPES[2] = (5, 1, 2)
       self.recipes[3] = (3, 0, 1, 2)
       self.recipes[4] = (4, 1, 2)
       self.recipes[5] = (7, 1 , 2)
       self.recipes[6] = (6, 1, 2)
       self.recipes[7] = (8, 1, 2)
   def click (Self, i):
       autopy.mouse.move (SE Lf.stuff_pos[i][0] + self.stuff_pos[i][1] +
       Autopy.mouse.click ()
   def make (self, i): for
       x in self . Recipes[i]:
           self.click (x)
       autopy.mouse.move (L + 315, T + 363)
       Autopy.mouse.click ()

This is not the most technical content of a class: Please forgive me I did not write comments and Doc, because it is very simple, I believe you understand.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.