python连连看小游戏_连连看小游戏
python制作连连看外挂
前几天看到github上的用python写的连连看外挂,最近几天一直在琢磨这个事情,昨天晚上弄到凌晨两点,终于把程序全部调通了,其中的关键部分算法全部重新写了一遍,其实原理非常简单。程序启动时,会自动从屏幕中进行截屏,找到连连看游戏窗口的起始坐标,根据坐标偏移,然后找到连连看的游戏区域.连连看最多可以放置的图片为19*11张。
按照约定图片的大小,游戏区域全部切分为31*35的小图片,然后将小图片按照不同的类别进行标记,具体的标记算法可以看源代码。我们设定empty背景为基准图片,然后计算所有图片与基准图片的像素之差的曼哈顿距离之和,按照距离的数值进行类别划分,且我们标记empty图片的类别为0。
将图片转换为标记矩阵后,我们就可以根据我们之前所知道的识别算法,来找到矩阵中的所有配对的坐标,然后驱动鼠标进行点击连接即可。
视频如下:
源代码: github地址:
import sys, time
import math, random
import cv2
import win32api
import win32gui
import win32con
from PIL import ImageGrab
import time
WINDOW_TITLE = "QQ游戏 - 连连看角色版"
MARGIN_LEFT = 14
MARGIN_HEIGHT = 181
COL_NUM = 19
ROW_NUM = 11
SQUARE_WIDTH = 31
SQUARE_HEIGHT = 35
SUB_LT_X = 4
SUB_LT_Y = 4
SUB_RB_X = 25
SUB_RB_Y = 29
CLICK_POS_X = 15
CLICK_POS_Y = 18
def TIME_INTERVAL():
return 0.001
#return 0.375+random.uniform(-0.075, 0.075)+0.13*progress
def TIME_INTERVAL_1():
return 0.001
#return 0.375+random.uniform(-0.075, 0.075)
def getGameWindowPosition():
window = win32gui.FindWindow(None, WINDOW_TITLE)
while not window:
print('unable to find window, try in 3 secs...')
time.sleep(3)
window = win32gui.FindWindow(None, WINDOW_TITLE)
win32gui.SetForegroundWindow(window)
pos = win32gui.GetWindowRect(window)
print("Window found at:" + str(pos))
return pos[0], pos[1]
def getScreenImage():
print('capturing screenshot...')
scim = ImageGrab.grab()
scim.save('screen.png')
return cv2.imread("screen.png")
def getAllSquare(screen_image, game_pos):
print('cutting pics...')
game_x = game_pos[0] + MARGIN_LEFT
game_y = game_pos[1] + MARGIN_HEIGHT
all_square = []
print("width:",len(screen_image))
print("height:",len(screen_image[0]))
for x in range(0, COL_NUM):
for y in range(0, ROW_NUM):
square = screen_image[game_y + y*SQUARE_HEIGHT:
game_y + (y+1)*SQUARE_HEIGHT,
game_x + x*SQUARE_WIDTH:
game_x + (x+1)*SQUARE_WIDTH]
all_square.append(square)
return list(map(lambda square:
square[SUB_LT_Y:SUB_RB_Y, SUB_LT_X:SUB_RB_X], all_square))
def subImageSquare(img1,img2):
res = 0
for i in range(len(img1)):
for j in range(len(img1[0])):
res += abs(int(img1[i][j][0]) - int(img2[i][j][0])) + \
abs(int(img1[i][j][1]) - int(img2[i][j][1])) + \
abs(int(img1[i][j][2]) - int(img2[i][j][2]))
return res
def getAllImageTypes(all_square):
print("sorting pics...")
empty_img = cv2.imread('empty.png')
types = set([0])
for square in all_square:
types.add(subImageSquare(empty_img,square))
return sorted(list(types))
def generateMatrix(all_square,imgTypes):
mat = []
empty_img = cv2.imread('empty.png')
for square in all_square:
diff = subImageSquare(empty_img,square)
for i in range(len(imgTypes)):
if diff == imgTypes[i]:
mat.append(i)
return mat
def checkConnect(x,y,mat):
width = COL_NUM
height = ROW_NUM
if x[0] < 0 or x[1] < 0 or x[0] >= width or x[1] >= height or\
y[0] < 0 or y[1] < 0 or y[0] >= width or y[1] >= width:
return False
if x[0] != y[0] and x[1] != y[1]:
return False
#check row
if x[0] == y[0]:
for i in range(min(x[1],y[1])+1,max(x[1],y[1])):
if mat[x[0]*height+i] > 0:
return False
#check colum
if x[1] == y[1]:
for i in range(min(x[0],y[0])+1,max(x[0],y[0])):
if mat[i*height+x[1]] > 0:
return False
return True
def isCanLink(x,y,mat):
width = COL_NUM
height = ROW_NUM
#check same value
if mat[x[0]*height+x[1]] != mat[y[0]*height+y[1]]:
return False
#one step check
if checkConnect(x,y,mat):
return True
#two step check
if mat[x[0]*height+y[1]] == 0:
if checkConnect(x,(x[0],y[1]),mat) and \
checkConnect((x[0],y[1]),y,mat):
return True
if mat[y[0]*height+x[1]] == 0:
if checkConnect(x,(y[0],x[1]),mat) and \
checkConnect((y[0],x[1]),y,mat):
return True
#three step check
for i in range(0,height):
if mat[x[0]*height+i] == 0 and mat[y[0]*height+i] == 0:
if checkConnect(x,(x[0],i),mat) and \
checkConnect((y[0],i),y,mat) and \
checkConnect((x[0],i),(y[0],i),mat):
return True
for i in range(0,width):
if mat[i*height+x[1]] == 0 and mat[i*height+y[1]] == 0:
if checkConnect(x,(i,x[1]),mat) and \
checkConnect((i,y[1]),y,mat) and \
checkConnect((i,x[1]),(i,y[1]),mat):
return True
def autoMouseClick(pos,delay = 0.001):
win32api.SetCursorPos((pos[0],pos[1]))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,pos[0],pos[1], 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,pos[0],pos[1],0,0)
time.sleep(TIME_INTERVAL())
def autoRemove(mat,game_pos):
game_x = game_pos[0] + MARGIN_LEFT
game_y = game_pos[1] + MARGIN_HEIGHT
width = COL_NUM
height = ROW_NUM
remove = 0
for i in range(width):
for j in range(height):
if mat[i*height+j] > 0:
remove += 1
while remove > 0:
for i in range(len(mat)):
for j in range(len(mat)):
if i != j and mat[i] == mat[j] and mat[i] > 0:
px = (i//height,i%height)
py = (j//height,j%height)
if isCanLink(px,py,mat):
x1 = game_x + px[0]*SQUARE_WIDTH
y1 = game_y + px[1]*SQUARE_HEIGHT
x2 = game_x + py[0]*SQUARE_WIDTH
y2 = game_y + py[1]*SQUARE_HEIGHT
pos_x = (x1+CLICK_POS_X,y1+CLICK_POS_Y)
pos_y = (x2+CLICK_POS_X,y2+CLICK_POS_Y)
autoMouseClick(pos_x)
autoMouseClick(pos_y)
mat[i] = 0
mat[j] = 0
remove -= 2
print(px,py)
print("remove one pair:",pos_x,pos_y)
if __name__ == '__main__':
game_pos = getGameWindowPosition()
time.sleep(1)
screen_image = getScreenImage()
all_square_list = getAllSquare(screen_image, game_pos)
imgTypes = getAllImageTypes(all_square_list)
mat = generateMatrix(all_square_list,imgTypes)
autoRemove(mat,game_pos)
python连连看小游戏_连连看小游戏相关推荐
- 怎么开发联机小游戏_微信小游戏创意大赛火热进行中,小游戏联机对战引擎免费用...
腾讯云为小游戏开发者升级工具箱 小游戏联机对战引擎免费用 由微信小游戏举办的"微信小游戏创意大赛"正在火热进行中.12月23日,腾讯云宣布,除了给创意大赛的参赛者提供基础云资源,还 ...
- python矩形碰撞检测算法_简易夺旗游戏(python像素级碰撞检测之颜色碰撞)
以下是部分代码预览: """ 简易夺旗游戏(python像素级碰撞检测之颜色碰撞) 按上下左右方向箭头操作小虫子去碰到小旗子,游戏就胜利了, 否则如果碰到黑色,游戏就失败 ...
- 广州python开发工程师招聘_【广州游戏开发工程师招聘_最新广州游戏开发工程师招聘信息】-前程无忧...
诚伯信息有限公司广州-天河区0.6-1.5万/月12-18 学历要求:本科|工作经验:1年|公司性质:民营公司|公司规模:5000-10000人 岗位职责1.与一流的开发团队协同工作,分析游戏各种玩法 ...
- 如何用python制作微信小程序_微信小程序可以用python开发么
现在很多的企业或者公司为了可以增加微信这个平台的附属功能,那么都会选择在微信的程序里面添加微信的小程序开发.所以怎么去做好微信小程序开发并不是只有小程序开发公司的问题,我们在帮助企业做小程序的时候一定 ...
- 荣耀v10玩flash游戏_“王者荣耀”游戏竟然还能这样玩?(送皮肤)
在王者荣耀的世界里:有人玩游戏喜欢打野位,有人玩游戏喜欢中单,有人喜欢抢人头和Buff,而她喜欢给英雄做画像... 她是谁?她是3小时留学的学生,喜欢搞一些爬虫和统计相关的内容,准备申请应用数学相关方 ...
- 幼儿课外活动游戏_幼儿园户外游戏活动大全
幼儿园户外游戏活动大全 幼儿园户外游戏活动收集 <玩转小布球> 游戏目标: 进行抛.接.跳.投等多种动作技能的练习,发展幼儿的上.下肢力量和 身体协调等多种素质,激发幼儿的练习兴趣. 游戏 ...
- h5跳转小程序_微信小程序吞掉H5?
近来小程序越来越火,如果能嵌入h5,微信小程序会吞掉H5吗?意味着未来小程序创业者能影响整个web世界的信息流分发和服务流分发.像他一样,不少开发者把这看作最近半年微信最重要的能力释放,那么这两项功能 ...
- 如何下载在线玩的游戏_在线下载游戏和玩游戏的最佳网站
如何下载在线玩的游戏 This week we have a list of fun websites for you. Playing games can help relieve stress a ...
- 龙之翼java游戏_龙之翼游戏下载-龙之翼最新版下载v1.0.3-一听下载站
龙之翼是一款经典的角色扮演类魔幻游戏,游戏中的画面特别的清晰,精彩纷呈的打斗场面,带给你更好的体验,游戏中登录后,还会获得很多惊喜,让你能够拥有更强的实力,炫酷的造型,独特的兵器,热血沸腾的打斗场景都 ...
- 微信公众账号后台怎么解除小程序_微信小程序怎么注销账号_微信小程序注销方法_快吧小程序...
微信小程序一直存在一个问题:开发者不能注销自己的小程序.之前当开发者需要彻底清除小程序信息时,只能够通过暂停服务或关闭"允许被搜索"的方式曲线救国.现在,这个问题终于解决了. 在微 ...
最新文章
- 提取IPv6地址的编码信息
- 牛客多校6 - Josephus Transform(线段树求k-约瑟夫环+置换群的幂)
- 工业机器人 答案 韩建海_中国将连续8年成为工业机器人第一大市场,还将持续多久?...
- 第13章 程序的动态加载和执行(一,引导)
- LeetCode 451. 根据字符出现频率排序(map+优先队列)
- KlayGE游戏引擎
- java日历查询代码,查询日历,万年历查询,日历格式JAVA原代码
- Web 实时推送技术如何弥补 HTTP 协议的缺陷? | 技术头条
- 一种软阴影的实现方法
- Center Loss
- 云视频会议对初创公司的益处
- LICEcap--小巧而精致的GIF录屏工具
- ALSA声卡驱动二之声卡的创建
- 三级网络技术--宽带接入技术--XDSL
- C#与宇电温控表自定义协议通信实例
- 欢迎来怼——第四次Scrum会议
- Java中文生僻字排序
- HDU 2838 Cow Sorting(双树状数组+求逆序数)
- IOS集成微信支付或者支付宝支付功能小结
- 【求整数 1-100 的累加值,但要求跳过所有个位数为3的数 】
热门文章
- ViewPage的使用与介绍
- 【python】py课上机作业3「谢尔宾斯基三角形」「递归输出列表」
- 字符转 ASCII 码
- 配置NodeJS免安装环境变量,win7,win10
- 如何编写系统设计说明书
- 腾讯笔试题 画家小Q
- VS2015配置OpenCV-contribu4.1.1及缺少xfeatures2d等无法打开包括文件: “features2d/test/test_detectors_regression.im问题
- 您的计算机性能不足 无法运行,绝地求生进不去游戏提示运行引擎需要DX11特性等级10.0,大神救我,刚做的系统W10 64位的,游戏也是刚下的...
- linux内核mtd驱动程序与sd卡驱动程序,Linux内核MTD驱动程序及SD卡驱动程序.doc
- 网上书店管理系统mysql代码_网上书店管理系统数据库 sql sever