首页 > 分享 > 【python】python制作 连连看 游戏脚本(一)

【python】python制作 连连看 游戏脚本(一)

【python】python制作 连连看 游戏脚本(一)_sunriver2000的博客-CSDN博客

【python】python制作 连连看 游戏脚本(二)_sunriver2000的博客-CSDN博客

【python】python制作 连连看 游戏脚本(三)_sunriver2000的博客-CSDN博客

【python】python制作 连连看 游戏脚本(四)_sunriver2000的博客-CSDN博客

在游戏中学习编程,即享受了游戏的乐趣,有提升了编程能力,可谓一举两得。

本文以【宠物连连看经典版2】为例,讲述使用python制作简单的游戏脚本。

游戏网址:宠物连连看经典版2

代码下载:GitHub - sunriver2000/LinkGameAss

环境

系统:win10 x64

Python版本:V3.6.6

主要模块:win32gui(识别窗口、窗口置顶等操作)、pillow(屏幕截图)、numpy(创建矩阵)、operator(比较值)、pymouse(模拟鼠标点击)。

安装参考:

【Python】安装pyuserinput_sunriver2000的博客-CSDN博客

【Python】安装pillow与numpy_sunriver2000的博客-CSDN博客

1、建立数学模型

【宠物连连看经典版2】游戏目标:需要消除的是8*12的图标矩阵,如下图所示。 

 实际8*12图标矩阵外围有一圈空白通道,所以实际矩阵规格是10*14,如下图。

空白通道用0表示,图标有12种分别用1至12数字表示,上图可以抽象为如下矩阵。

[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

[ 0 1 2 3 4 4 5 5 3 1 1 6 7 0]

[ 0 2 8 9 3 5 1 4 2 8 10 8 5 0]

[ 0 11 5 2 9 12 10 7 8 9 12 3 11 0]

[ 0 11 6 6 4 7 11 6 10 11 12 1 3 0]

[ 0 10 2 11 10 7 12 11 3 9 12 12 10 0]

[ 0 12 8 7 2 6 8 1 10 7 6 5 8 0]

[ 0 1 9 9 9 4 4 6 7 11 4 1 10 0]

[ 0 5 3 5 6 4 12 7 2 9 8 2 3 0]

[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

A图标通过两次90度拐弯到达B图标,则AB图标消除。消除后,矩阵内原来位置补零,变成通道。

综上,脚本主要涉及图像识别、图标消除等两种算法。

2、初始化

2.1、获取游戏窗口标题

打开【连连看】游戏网页后,使用EnumChildWindows()函数枚举所有窗口并保存为文本文件。浏览文件,确定窗口标题为【宠物连连看经典版2小游戏,在线玩,4399小游戏 - 360极速浏览器 13.5】。

import win32gui, win32ui, win32con, win32api

f = open('winlist.txt', 'w', encoding='utf-8')

hd = win32gui.GetDesktopWindow()

hwndChildList = []

win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwndChildList)

for hwnd in hwndChildList:

print("句柄:",hwnd,"标题:",win32gui.GetWindowText(hwnd))

f.write("句柄:" + str(hwnd) + " 标题:" + win32gui.GetWindowText(hwnd) + 'n')

f.close()

以上代码可以独立执行。 

2.2、前置游戏窗口

前置游戏窗口的代码放在GameAssist类初始化中,类声明对象后,自动前置游戏窗口。

class GameAssist:

def __init__(self, wdname):

self.hwnd = win32gui.FindWindow(None, wdname)

if not self.hwnd:

print("can't find window, name is : 【%s】" % wdname)

exit()

print("find window, name is : 【%s】" % wdname)

win32gui.SetForegroundWindow(self.hwnd)

'

在主函数中,调用方式如下。

if __name__ == "__main__":

wdname = u'宠物连连看经典版2小游戏,在线玩,4399小游戏 - 360极速浏览器 13.5'

demo = GameAssist(wdname)

3、图像识别算法

3.1、确定参数

第一个参数是图标矩阵的坐标,包括:左上角和右下角两个点位。

左上角( 360,258)

右下角(1253,853)

获取方法:抓屏,使用画图进行编辑。这两个坐标必须精确,会影响到后续图像识别。

第二个参数是图标的高度(高度与宽度相等)。

width = (1253 - 360)/ 12 = 74.41

def __init__(self, wdname):

self.hwnd = win32gui.FindWindow(None, wdname)

if not self.hwnd:

print("can't find window, name is : 【%s】" % wdname)

exit()

print("find window, name is : 【%s】" % wdname)

win32gui.SetForegroundWindow(self.hwnd)

self.im2num_arr = []

self.screen_left_and_right_point = (360, 258, 1253, 853)

self.im_width = 74.41

self.mouse = PyMouse()

'

3.2、截取图标矩阵

def screenshot(self):

image = ImageGrab.grab(self.screen_left_and_right_point)

save_im(image, 'image')

return image_list

'

 使用ImageGrab.grab()截取屏幕后,可以保存为图片进行调试。

def save_im(image, name):

screen_path = os.path.join(os.path.dirname(__file__), 'screen')

if not os.path.exists(screen_path):

os.makedirs(screen_path)

image_name = os.path.join(screen_path, name)

t = time.strftime('%Y%m%d_%H%M%S', time.localtime())

image.save('%s_%s.png' % (image_name, t))

'

矩阵外围的长方形线宽保留1个像素,效果如下。

3.3、分解为12*8个图标

分解图标后,存入image_list列表中。

def screenshot(self):

image = ImageGrab.grab(self.screen_left_and_right_point)

save_im(image, 'image')

image_list = {}

offset = self.im_width

for x in range(8):

image_list[x] = {}

for y in range(12):

top = round(x * offset)

left = round(y * offset)

bottom = round((x + 1) * offset)

right = round((y + 1) * offset)

im = image.crop((left, top, right, bottom))

image_list[x][y] = im

save_im(image_list[x][y], str(x).zfill(2)+str(y).zfill(2))

return image_list

'

 保存为图片,分解效果如下图所示,应该有12*8个,图片没有截全。

3.4、图像数字化

现在已经得到图标图像的列表,但是计算机无法直接对比两个图标是否一致,需要图像数字化。

第一步,彩色转为黑白灰度。

image1 = im1.resize((20, 20), Image.ANTIALIAS).convert("L")

第二步,转为为一串01序列

pixels1 = list(image1.getdata())

avg1 = sum(pixels1) / len(pixels1)

hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pixels1))

第三步,对比完成后,存入 image_type_list列表。这里是考验前期参数与算法的地方了,理论上mage_type_list只会存入12个元素。调试时,放开如下代码。

for i in range(len(image_type_list)):

save_im(image_type_list[i], str(i).zfill(2))

涉及函数

def image2num(self, image_list):

arr = np.zeros((10, 14), dtype=np.int32)

image_type_list = []

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)

if index < 0:

image_type_list.append(im)

arr[i + 1][j + 1] = len(image_type_list)

else:

arr[i + 1][j + 1] = index + 1

print("图标数:", len(image_type_list))

for i in range(len(image_type_list)):

save_im(image_type_list[i], str(i).zfill(2))

self.im2num_arr = arr

return arr

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

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

match = sum(map(operator.ne, hash1, hash2))

return match < 40

相关知识

【Python程序】用200行Python代码制作有趣的桌面宠物(源码可分享),大打工人解压放松程序,如何用Python制作一个桌面宠物!
【Python教程】教你用Python代码制作一个桌面宠物,专属桌宠,体验感升级1000%(附源码)
【附源码】教你用Python代码制作一只你的专属宠物,桌面体验感升级100%!!
数你用Python代码。制作一个专属桌宠,附源代码
【附源码】用Python代码,制作出一只专属桌面宠物,确定不来一只?保姆级教程,小白也能学会!!
Python 入门
4399小游戏—宠物连连看经典版2—游戏辅助脚本
请问这个代码应该怎样写?(一个Tommy虚拟宠物猫的游戏,求用Python编写
python如何制作桌面宠物
听Python之父

网址: 【python】python制作 连连看 游戏脚本(一) https://m.mcbbbk.com/newsview733244.html

所属分类:萌宠日常
上一篇: 晴空物语力士职业全面攻略,技能加
下一篇: 【梦幻宠物连连看(微信小游戏)下