0
\$\begingroup\$

The code works perfectly.

It looks for a certain parts of the screen where the terraria fishing particles would be appear, after which it if the threshold is met it fishes. I was wondering if it was to bad have 2 completely separate functions for each location, Corrupt_Check() and Normal_Check().This is because in the future I would like to intergrate different locations and would like it to be easy to maintain, could classes be used then?

Also currently you have to switch locations by changing if Normal_Check(screenshot) to if Corrupt_Check in Main(). How could this be done better and less tediously.

Thank you very much.

import pyautogui as py
import time
import keyboard
import cv2
import numpy as np
import pydirectinput
import win32api, win32con


def setup_fish_detection():
    pydirectinput.click(x=483, y=559 )
    time.sleep(1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
    time.sleep(2)

def take_screenshot(file_name):
     screenie = py.screenshot()
     screenie.save("Is_Fish_There_cor.png")
     print("taking a screenshot")

def Corrupt_Check(screenshot):
    img = screenshot[590:620,432:700]
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    cv2.imshow("efvcewfv", hsv)
    lowerrange = np.array([133, 0, 0])
    upperrange = np.array([179, 79, 255])
    mask = cv2.inRange(hsv, lowerrange, upperrange)
    blue = np.sum(mask)
    print (blue)
    return blue > 8000

def Normal_Check(screenshot):
    img = screenshot[590:607,400:690]
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    lowerrange = np.array([106, 147, 0])
    upperrange = np.array([179, 255, 245])
    mask = cv2.inRange(hsv, lowerrange, upperrange)
    particles = np.sum(mask)
    print (particles)
    return particles > 15000

def post_detection_handling():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
    time.sleep(.5)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
    time.sleep(1.5)

def main():
    setup_fish_detection()
    screenshot_file_name = "Is_Fish_There_cor.png"
    while not keyboard.is_pressed('q'):
        take_screenshot(screenshot_file_name)
        screenshot = cv2.imread(screenshot_file_name)
        if Normal_Check(screenshot):
            print("fish has been found")
            post_detection_handling()
    print('Stopped Fishing')



if __name__=="__main__":
    main()
\$\endgroup\$
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.