字母方块图片:宽75px高77px,需要字母A~Z和问号共27个方块,自行在游戏中截取,截好之后记得问Deepseek怎么修改代码中的路径
import requests
from bs4 import BeautifulSoup
import tkinter as tk
from PIL import Image, ImageChops, ImageGrab
import numpy as np
import time
import pyautogui
import win32gui
xx, yy, x0, y0, dx, dy = 0, 0, 456, 501, 75, 77
LETTER_ICONS = {}
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ?"
for LETTER in LETTERS:
img = Image.open(fr"D:\shell\{LETTER}.png")
img = img.convert("L").resize((48, 48))
LETTER_ICONS[LETTER] = img
def findGameWindow():
global xx, yy # 游戏窗口位置
hwnd = win32gui.FindWindow(None, "Bookworm Adventures Vol. 2 v1.0.4.8465 ")
xx, yy, _, _ = win32gui.GetWindowRect(hwnd)
print("游戏窗口位置:", xx, yy)
def saveLetter(letter, i, j):
letters = Image.open(r"D:\录像/1.png")
cropped = letters.crop((j*dx, i*dy, (j+1)*dx, (i+1)*dy))
cropped.save(fr"D:\shell/{letter}.png")
def shot(save=False):
global xx, yy # 游戏窗口位置
findGameWindow()
screenshot = ImageGrab.grab()
cropped = screenshot.crop((xx + x0, yy + y0, xx + x0 + dx * 4, yy + y0 + dy * 4))
if save:
cropped.save(r"D:\录像/1.png")
return cropped
def onPressButton(index):
result = ''
pyautogui.click(xx + 850, yy + 640)
if index == 1:
time.sleep(2)
img, letters = shot(), ''
for i in range(4):
for j in range(4):
minErr, letter = 114514, 'a'
cropped = img.crop((j*dx, i*dy, (j+1)*dx, (i+1)*dy))
cropped = cropped.convert("L").resize((48, 48))
for LETTER in LETTERS:
err = ImageChops.difference(cropped, LETTER_ICONS[LETTER])
err = err.point(lambda x: 0 if x < 160 else 255, '1')
err = np.sum(np.array(err))
if err < minErr:
minErr = err
if LETTER == 'Q': letter = "QU"
elif LETTER == '?': letter = '*'
else: letter = LETTER
letters += letter
result = letters
entry.delete(0, tk.END)
entry.insert(0, result)
else:
result = entry.get()
label.config(text=getAnswer(result))
def onPressButton1(): onPressButton(1)
def onPressButton2(): onPressButton(2)
def getAnswer(letters):
url = f'https://crossword-solver.io/words-for/{letters}/?dictionary=wwf'
response = requests.get(url) # 发送GET请求
if response.status_code == 200: # 确保请求成功
soup = BeautifulSoup(response.text, 'html.parser') # 解析HTML内容
elements = soup.find_all(class_='changeable-word', limit=3) # 查找第一个含有类名'resultword'的元素
if elements:
result = ''
for element in elements:
result += element.get_text(strip=True).upper()
result += '\n'
return f"识别到的字母:{letters},结果:\n" + result
else:
return "无解"
else:
return print("请求失败")
# shot(True)
findGameWindow()
window = tk.Tk()
window.geometry("500x300")
window.focus_set() # 设置窗口焦点以接收按键事件
label = tk.Label(window, font=("Courier New", 12), text='贪吃虫冒险')
label.pack()
entry = tk.Entry(window)
entry.pack()
button1 = tk.Button(window, text="截图计算", command=onPressButton1)
button1.pack()
button2 = tk.Button(window, text="输入计算", command=onPressButton2)
button2.pack()
window.mainloop() # 开始Tkinter的事件循环