How to make a game script in python

(*-*)浩
Release: 2019-07-04 10:44:18
Original
8839 people have browsed it

This article will use the 4399 mini-game "Pet Lianliankan Classic Edition 2" as a test case. By identifying small icons and simulating mouse clicks, the pairing can be quickly completed to complete the game script.

How to make a game script in pythonThe browser opens the game window (a single window). The screenshot of the main interface of the game requires two coordinates (upper left corner coordinates and lower right corner coordinates) to determine. The origin is generally the upper left corner of the screen, and the coordinates are uncertain. Students who click on the value can take a full-screen screenshot and use image editing software to view the coordinate values. (Recommended learning: Python video tutorial)

Get the window handle, here is the title of the browser title bar (right-click-view source code-title, plus the software name) For example: " Pet Lianliankan Classic 2, Pet Lianliankan Classic Edition 2 mini-games, 4399 mini-games www.4399.com - Google Chrome". Get the window handle and you're good to go.

Overall development idea: intercept the main game image---> split into small images---> compare each small image, compare the familiarity of the image, and store the number in the matrix---> pair matrix Perform connectable calculations ---> Simulate clicks.

Get the window handle and put the window on top

Python can use the win32gui module to call the Windows API to operate the window, and use the FindWindow() method to get the window handle ( handle), you need to pass in two parameters. The first one is the parent window handle (just fill in 0 here), and the second parameter is the name of the window (label title - Google Chrome). After getting the handle, set the window in front through SetForegroundWindows(). Here you can pass in the report of the game window. The code is as follows:

import win32gui

class GameAssist:

    def __init__(self, wdname):
        """初始化"""

        # 取得窗口句柄
        self.hwnd = win32gui.FindWindow(0, wdname)
        if not self.hwnd:
            print("窗口找不到,请确认窗口句柄名称:【%s】" % wdname )
            exit()
        # 窗口显示最前面
        win32gui.SetForegroundWindow(self.hwnd)


if __name__ == "__main__":
    # wdname 为连连看窗口的名称,必须写完整
    wdname = u'宠物连连看经典版2,宠物连连看经典版2小游戏,4399小游戏 www.4399.com - Google Chrome'

    demo = GameAssist(wdname)
    demo.start()
Copy after login

Intercept the game interface, split the icon, and compare the pictures

It takes some time to verify the program. If the captured picture is not good, it will affect subsequent operations, so the most important thing is to confirm the coordinate values ​​​​of the upper left corner and lower right corner of the game, as well as each The width and height of the small icon. As shown in the figure below, first intercept the entire game interface diagram, then divide the small icons, then compare each icon, and then replace the icon with a number and store it in the matrix (the number matrix here is inconsistent with the game diagram, the principle is the same).

How to make a game script in pythonAccording to the two coordinates of the upper left corner and lower right corner set by the initialization, use the ImageGrab.grab() method to take a screenshot, pass in a tuple, and then segment and cut the large image. Save them into small icons one by one in the images_list array.

def screenshot(self):
        """屏幕截图"""

        # 1、用grab函数截图,参数为左上角和右下角左标
        # image = ImageGrab.grab((417, 257, 885, 569))
        image = ImageGrab.grab(self.scree_left_and_right_point)

        # 2、分切小图
        # exit()
        image_list = {}
        offset = self.im_width  # 39

        # 8行12列
        for x in range(8):
            image_list[x] = {}
            for y in range(12):
                # print("show",x, y)
                # exit()
                top = x * offset
                left = y * offset
                right = (y + 1) * offset
                bottom = (x + 1) * offset

                # 用crop函数切割成小图标,参数为图标的左上角和右下角左边
                im = image.crop((left, top, right, bottom))
                # 将切割好的图标存入对应的位置
                image_list[x][y] = im

        return image_list
Copy after login

The small icon cut by the above code is converted into a numerical matrix. If the icon has been stored in image_type_list, the index will be returned. If it does not exist, it will be appended, and the current length will be the number of the newly added icon. , the code is as follows:

def image2num(self, image_list):
        """将图标矩阵转换成数字矩阵"""

        # 1、创建全零矩阵和空的一维数组
        arr = np.zeros((10, 14), dtype=np.int32)    # 以数字代替图片
        image_type_list = []

        # 2、识别出不同的图片,将图片矩阵转换成数字矩阵
        for i in range(len(image_list)):
            for j in range(len(image_list[0])):
                im = image_list[i][j]
                
                # 验证当前图标是否已存入
                index = self.getIndex(im, image_type_list)

                # 不存在image_type_list
                if index <p>The getIndex above is to compare the images and determine whether the icon has appeared (whether it already exists in the image_type_list, if it does not appear, it will be added). Here, the Hamming distance is used to determine the acquaintance of the two images. Degree, set the threshold to 10. When it is less than the threshold, it is considered to be the same picture. The specific code is as follows: </p><pre class="brush:php;toolbar:false"># 检查数组中是否有图标,如果有则返回索引下表
    def getIndex(self,im, im_list):
        for i in range(len(im_list)):
            if self.isMatch(im, im_list[i]):
                return i

        return -1

    # 汉明距离判断两个图标是否一样
    def isMatch(self, im1, im2):

        # 缩小图标,转成灰度
        image1 = im1.resize((20, 20), Image.ANTIALIAS).convert("L")
        image2 = im2.resize((20, 20), Image.ANTIALIAS).convert("L")

        # 将灰度图标转成01串,即系二进制数据
        pixels1 = list(image1.getdata())
        pixels2 = list(image2.getdata())

        avg1 = sum(pixels1) / len(pixels1)
        avg2 = sum(pixels2) / len(pixels2)
        hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pixels1))
        hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pixels2))

        # 统计两个01串不同数字的个数
        match = sum(map(operator.ne, hash1, hash2))

        # 阀值设为10
        return match <p>Program core-icon connection algorithm (path search)</p><p>Here is only the algorithm code Carry out a simple analysis. If you don’t understand the program well, you can leave a message and follow up with graphic analysis. </p><p>Through the above development process, a matrix like the following is basically obtained. Just compare two values ​​with the same number to find connectable paths. If found, simulate a click operation. Here is a brief introduction to the game rules: 8 rows by 12 columns of game icon area. The 0 in the periphery actually means that it can be passed when looking for a path. For example, the coordinates (1, 1) can be connected to (1, 10), (7, 1) Connect with (7,2). </p><pre class="brush:php;toolbar:false">arr = [
    [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
    [ 0,  1,  2,  3,  4,  5,  4,  6,  7,  2,  1,  1,  8,  0],
    [ 0,  9,  3,  3, 10,  4,  7, 11,  7,  2,  3,  1,  6,  0],
    [ 0,  6,  7,  4, 11,  5,  8,  1,  6,  5,  4,  2,  8,  0],
    [ 0,  6,  2,  9,  6,  8,  9,  7, 12, 11,  3, 11, 11,  0],
    [ 0,  5,  9,  8,  9,  2,  6, 11, 11,  3,  9,  2, 12,  0],
    [ 0, 12,  5, 12,  5, 10,  5,  6,  5,  7, 12,  4,  3,  0],
    [ 0,  1,  8, 10, 12,  9, 10,  4,  3,  7,  2,  1, 10,  0],
    [ 0,  1,  4, 10,  8, 12, 10, 10,  9, 12,  8,  7, 11,  0],
    [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]
]
Copy after login

The idea of ​​​​the method: To find a path, first find a set of coordinates that can be directly connected horizontally and vertically. For example, the set of coordinates p1 (1,1) has [ (0,1), ( 1,0)], and the connectable set of another coordinate p2 (1,10) is [(0,10)], and then compare the connectable coordinate sets of p1 and p2. If there are also connectable coordinates in the set, It means that p1 and p2 can be connected. Obviously, (0,1) and (0,10) are on the same line and can be connected. This means that there is a connectable path between p1 and p2. The code is as follows:

Briefly analyze the code implementation process: pass in the two coordinate values ​​that need to be compared in isReachable(), then obtain the coordinate sets that can be connected to the two points in the horizontal and vertical directions (isRowConnect(), isColConnect()), and finally Traverse the collection and compare whether there is a linkable one. If it exists, it means that the two incoming coordinates can be connected.

# 是否为同行或同列且可连
    def isReachable(self, x1, y1, x2, y2):
        # 1、先判断值是否相同
        if self.im2num_arr[x1][y1] != self.im2num_arr[x2][y2]:
            return False

        # 1、分别获取两个坐标同行或同列可连的坐标数组
        list1 = self.getDirectConnectList(x1, y1)
        list2 = self.getDirectConnectList(x2, y2)
        # print(x1, y1, list1)
        # print(x2, y2, list2)

        # exit()

        # 2、比较坐标数组中是否可连
        for x1, y1 in list1:
            for x2, y2 in list2:
                if self.isDirectConnect(x1, y1, x2, y2):
                    return True
        return False

    # 获取同行或同列可连的坐标数组
    def getDirectConnectList(self, x, y):

        plist = []
        for px in range(0, 10):
            for py in range(0, 14):
                # 获取同行或同列且为0的坐标
                if self.im2num_arr[px][py] == 0 and self.isDirectConnect(x, y, px, py):
                    plist.append([px, py])

        return plist

    # 是否为同行或同列且可连
    def isDirectConnect(self, x1, y1, x2, y2):
        # 1、位置完全相同
        if x1 == x2 and y1 == y2:
            return False

        # 2、行列都不同的
        if x1 != x2 and y1 != y2:
            return False

        # 3、同行
        if x1 == x2 and self.isRowConnect(x1, y1, y2):
            return True

        # 4、同列
        if y1 == y2 and self.isColConnect(y1, x1, x2):
            return True

        return False

    # 判断同行是否可连
    def isRowConnect(self, x, y1, y2):
        minY = min(y1, y2)
        maxY = max(y1, y2)

        # 相邻直接可连
        if maxY - minY == 1:
            return True

        # 判断两个坐标之间是否全为0
        for y0 in range(minY + 1, maxY):
            if self.im2num_arr[x][y0] != 0:
                return False
        return True

    # 判断同列是否可连
    def isColConnect(self, y, x1, x2):
        minX = min(x1, x2)
        maxX = max(x1, x2)

        # 相邻直接可连
        if maxX - minX == 1:
            return True

        # 判断两个坐标之间是否全为0
        for x0 in range(minX + 1, maxX):
            if self.im2num_arr[x0][y] != 0:
                return False
        return True
Copy after login

Learning such a game auxiliary script is also very helpful for individuals to cultivate their interest in programming. It can be regarded as a good way to spend time after work. I will study and learn more in these directions in the future.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to make a game script in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!