split into small pictures ---> compare each small picture, compare the familiarity of the pictures, and store the number in the matrix --- > Perform connectable calculations on matrices ---> Simulate clicks."/> split into small pictures ---> compare each small picture, compare the familiarity of the pictures, and store the number in the matrix --- > Perform connectable calculations on matrices ---> Simulate clicks.">
search
HomeBackend DevelopmentPython TutorialHow to make a game script in python

How to make a game script in python

Jul 04, 2019 am 10:44 AM
python

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()

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

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]
]

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

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!

Statement
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
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.