[Python] QQ小游戏脚本

1686 0
yang28852 2023-7-5 12:50:11 | 显示全部楼层 |阅读模式
QQ小游戏脚本(连连看
  1. import cv2
  2. import numpy as np
  3. import win32api
  4. import win32gui
  5. import win32con
  6. from PIL import ImageGrab
  7. import time
  8. import random
  9. import sys

  10. WINDOW_TITLE = "QQ游戏 - 连连看角色版"
  11. TIME_INTERVAL_MAX = 0
  12. TIME_INTERVAL_MIN = 0
  13. MARGIN_LEFT = 10
  14. MARGIN_HEIGHT = 180
  15. H_NUM = 19
  16. V_NUM = 11
  17. POINT_WIDTH = 31
  18. POINT_HEIGHT = 35
  19. EMPTY_ID = 0
  20. SUB_LT_X = 8
  21. SUB_LT_Y = 8
  22. SUB_RB_X = 27
  23. SUB_RB_Y = 27
  24. MAX_ROUND = 200
  25. def getGameWindow():
  26.     window = win32gui.FindWindow(None, WINDOW_TITLE)
  27.     while not window:
  28.         print('Failed to locate the game window , reposition the game window after 10 seconds...')
  29.         time.sleep(10)
  30.         window = win32gui.FindWindow(None, WINDOW_TITLE)
  31.     win32gui.SetForegroundWindow(window)
  32.     pos = win32gui.GetWindowRect(window)
  33.     print("Game windows at " + str(pos))
  34.     return (pos[0], pos[1])
  35. def getScreenImage():
  36.     print('Shot screen...')
  37.     scim = ImageGrab.grab()
  38.     scim.save('screen.png')
  39.     return cv2.imread("screen.png")

  40. def getAllSquare(screen_image, game_pos):
  41.     print('Processing pictures...')
  42.     game_x = game_pos[0] + MARGIN_LEFT
  43.     game_y = game_pos[1] + MARGIN_HEIGHT
  44.     all_square = []
  45.     for x in range(0, H_NUM):
  46.         for y in range(0, V_NUM):
  47.             square = screen_image[game_y + y * POINT_HEIGHT:game_y + (y + 1) * POINT_HEIGHT,
  48.                      game_x + x * POINT_WIDTH:game_x + (x + 1) * POINT_WIDTH]
  49.             all_square.append(square)
  50.     finalresult = []
  51.     for square in all_square:
  52.         s = square[SUB_LT_Y:SUB_RB_Y, SUB_LT_X:SUB_RB_X]
  53.         finalresult.append(s)
  54.     return finalresult

  55. def isImageExist(img, img_list):
  56.     i = 0
  57.     for existed_img in img_list:
  58.         b = np.subtract(existed_img, img)
  59.         if not np.any(b):
  60.             return i
  61.         i = i + 1
  62.     return -1

  63. def getAllSquareTypes(all_square):
  64.     print("Init pictures types...")
  65.     types = []
  66.     number = []
  67.     nowid = 0
  68.     for square in all_square:
  69.         nid = isImageExist(square, types)
  70.         if nid == -1:
  71.             types.append(square)
  72.             number.append(1)
  73.         else:
  74.             number[nid] = number[nid] + 1
  75.             if (number[nid] > number[nowid]):
  76.                 nowid = nid
  77.     global EMPTY_ID
  78.     EMPTY_ID = nowid
  79.     print('EMPTY_ID = ' + str(EMPTY_ID))
  80.     return types

  81. def getAllSquareRecord(all_square_list, types):
  82.     print("Change map...")
  83.     record = []
  84.     line = []
  85.     for square in all_square_list:
  86.         num = 0
  87.         for type in types:
  88.             res = cv2.subtract(square, type)
  89.             if not np.any(res):
  90.                 line.append(num)
  91.                 break
  92.             num += 1
  93.         if len(line) == V_NUM:
  94.             print(line)
  95.             record.append(line)
  96.             line = []
  97.     return record

  98. def canConnect(x1, y1, x2, y2, r):
  99.     result = r[:]
  100.     if result[x1][y1] == EMPTY_ID or result[x2][y2] == EMPTY_ID:
  101.         return False
  102.     if x1 == x2 and y1 == y2:
  103.         return False
  104.     if result[x1][y1] != result[x2][y2]:
  105.         return False
  106.     if horizontalCheck(x1, y1, x2, y2, result):
  107.         return True
  108.     if verticalCheck(x1, y1, x2, y2, result):
  109.         return True
  110.     if turnOnceCheck(x1, y1, x2, y2, result):
  111.         return True
  112.     if turnTwiceCheck(x1, y1, x2, y2, result):
  113.         return True
  114.     return False

  115. def horizontalCheck(x1, y1, x2, y2, result):
  116.     if x1 == x2 and y1 == y2:
  117.         return False
  118.     if x1 != x2:
  119.         return False
  120.     startY = min(y1, y2)
  121.     endY = max(y1, y2)
  122.     if (endY - startY) == 1:
  123.         return True
  124.     for i in range(startY + 1, endY):
  125.         if result[x1][i] != EMPTY_ID:
  126.             return False
  127.     return True

  128. def verticalCheck(x1, y1, x2, y2, result):
  129.     if x1 == x2 and y1 == y2:
  130.         return False

  131.     if y1 != y2:
  132.         return False
  133.     startX = min(x1, x2)
  134.     endX = max(x1, x2)
  135.     if (endX - startX) == 1:
  136.         return True
  137.     for i in range(startX + 1, endX):
  138.         if result[i][y1] != EMPTY_ID:
  139.             return False
  140.     return True

  141. def turnOnceCheck(x1, y1, x2, y2, result):
  142.     if x1 == x2 or y1 == y2:
  143.         return False

  144.     cx = x1
  145.     cy = y2
  146.     dx = x2
  147.     dy = y1
  148.     if result[cx][cy] == EMPTY_ID:
  149.         if horizontalCheck(x1, y1, cx, cy, result) and verticalCheck(cx, cy, x2, y2, result):
  150.             return True
  151.     if result[dx][dy] == EMPTY_ID:
  152.         if verticalCheck(x1, y1, dx, dy, result) and horizontalCheck(dx, dy, x2, y2, result):
  153.             return True
  154.     return False

  155. def turnTwiceCheck(x1, y1, x2, y2, result):
  156.     if x1 == x2 and y1 == y2:
  157.         return False
  158.     for i in range(0, len(result)):
  159.         for j in range(0, len(result[1])):
  160.             if result[i][j] != EMPTY_ID:
  161.                 continue
  162.             if i != x1 and i != x2 and j != y1 and j != y2:
  163.                 continue
  164.             if (i == x1 and j == y2) or (i == x2 and j == y1):
  165.                 continue
  166.             if turnOnceCheck(x1, y1, i, j, result) and (
  167.                     horizontalCheck(i, j, x2, y2, result) or verticalCheck(i, j, x2, y2, result)):
  168.                 return True
  169.             if turnOnceCheck(i, j, x2, y2, result) and (
  170.                     horizontalCheck(x1, y1, i, j, result) or verticalCheck(x1, y1, i, j, result)):
  171.                 return True
  172.     return False

  173. def autoRelease(result, game_x, game_y):
  174.     for i in range(0, len(result)):
  175.         for j in range(0, len(result[0])):
  176.             if result[i][j] != EMPTY_ID:
  177.                 for m in range(0, len(result)):
  178.                     for n in range(0, len(result[0])):
  179.                         if result[m][n] != EMPTY_ID:
  180.                             if canConnect(i, j, m, n, result):
  181.                                 result[i][j] = EMPTY_ID
  182.                                 result[m][n] = EMPTY_ID
  183.                                 print('Remove :' + str(i + 1) + ',' + str(j + 1) + ' and ' + str(m + 1) + ',' + str(
  184.                                     n + 1))
  185.                                 x1 = game_x + j * POINT_WIDTH
  186.                                 y1 = game_y + i * POINT_HEIGHT
  187.                                 x2 = game_x + n * POINT_WIDTH
  188.                                 y2 = game_y + m * POINT_HEIGHT

  189.                                 win32api.SetCursorPos((x1 + 15, y1 + 18))
  190.                                 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x1 + 15, y1 + 18, 0, 0)
  191.                                 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x1 + 15, y1 + 18, 0, 0)
  192.                                 time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX))

  193.                                 win32api.SetCursorPos((x2 + 15, y2 + 18))
  194.                                 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x2 + 15, y2 + 18, 0, 0)
  195.                                 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x2 + 15, y2 + 18, 0, 0)
  196.                                 time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX))
  197.                                 return True
  198.     return False


  199. def autoRemove(squares, game_pos):
  200.     game_x = game_pos[0] + MARGIN_LEFT
  201.     game_y = game_pos[1] + MARGIN_HEIGHT
  202.     while True:
  203.         if not autoRelease(squares, game_x, game_y):
  204.             return sys.exit()

  205. if __name__ == '__main__':
  206.     random.seed()
  207.     game_pos = getGameWindow()
  208.     time.sleep(1)
  209.     screen_image = getScreenImage()
  210.     all_square_list = getAllSquare(screen_image, game_pos)
  211.     types = getAllSquareTypes(all_square_list)
  212.     result = np.transpose(getAllSquareRecord(all_square_list, types))
  213.     print('The total elimination amount is ' + str(autoRemove(result, game_pos)))
  214.     win32api.system("pause")
复制代码
),大家帮忙看下能不能用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

yang28852

初入联盟

关注
  • 3
    主题
  • 0
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行