Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import pyautogui
- import pygetwindow as gw
- import pytesseract
- import re
- import keyboard
- def read_word(window_title, region):
- # Activate the specified window
- window = gw.getWindowsWithTitle(window_title)[0]
- window.activate()
- # Calculate the absolute coordinates of the specified region inside the window
- absolute_region = (
- window.left + region[0],
- window.top + region[1],
- window.left + region[2],
- window.top + region[3]
- )
- pyautogui.moveTo(absolute_region[0], absolute_region[1]-50)
- # Capture the screenshot based on the absolute coordinates
- screenshot = pyautogui.screenshot(region=(absolute_region[0], absolute_region[1], absolute_region[2]-absolute_region[0], absolute_region[3]-absolute_region[1]))
- screenshot.save("window_region_screenshot.png")
- # Use OCR (Optical Character Recognition) to extract text from the image
- word = pytesseract.image_to_string(screenshot)
- return word.strip()
- def clean_and_compare_strings(compareTo, strList):
- # Remove special characters and numbers, leaving only letters
- clean_compareTo = re.sub('[^a-zA-Z]', '', compareTo)
- # Compare the cleaned strings ignoring case
- for str in strList:
- clean_str = re.sub('[^a-zA-Z]', '', str)
- if(clean_compareTo.lower() == clean_str.lower()):
- return str
- return None
- def main():
- expected_words = ["lichenology", "Recycling", "Mechanics"]
- #expected_words = ["cooking", "medicine", "Mechanics"]
- window_title = "StateOfDecay2"
- xModifier = [0, 300, 590]
- survivor = 0
- survivorBool = [False, False, False]
- print(xModifier[survivor])
- time.sleep(2)
- print('Starting...')
- completed_all_survivors = False
- # check all 3 survivors to see if any is desired survivor is already there
- for i in range(3):
- # Read the word from the specified region of the window
- target_region = (290 + xModifier[i], 695, 400 + xModifier[i], 715)
- word = read_word(window_title, target_region)
- result = clean_and_compare_strings(word, expected_words)
- # Check if the read word is correct
- if result != None:
- print("Word " + word + " is correct.")
- survivorBool[i] = True
- expected_words.remove(result)
- pyautogui.press('right')
- pyautogui.press('left')
- pyautogui.press('left')
- completed_all_survivors = True
- for bool in survivorBool:
- if bool == False:
- completed_all_survivors = False
- for i in range(3):
- if survivorBool[i] == False:
- survivor = i
- break
- while completed_all_survivors != True:
- # Read the word from the specified region of the window
- target_region = (290 + xModifier[survivor], 695, 400 + xModifier[survivor], 715)
- word = read_word(window_title, target_region)
- result = clean_and_compare_strings(word, expected_words)
- # Check if the read word is correct
- if result != None:
- print("Word " + word + " is correct.")
- expected_words.remove(result)
- survivorBool[survivor] = True
- completed_all_survivors = True
- for bool in survivorBool:
- if bool == False:
- completed_all_survivors = False
- while survivorBool[survivor] == True:
- survivor = survivor + 1
- if survivor >= len(survivorBool):
- break
- pyautogui.press('right')
- if survivor >= 3:
- print("All survivors found, exiting.")
- break
- else:
- print(word)
- pyautogui.press('t')
- time.sleep(0.01) # Adjust the sleep duration based on your requirements
- if keyboard.is_pressed('esc'):
- print("Escape key pressed. Exiting loop.")
- break
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement