Advertisement
Guest User

Untitled

a guest
Mar 16th, 2021
22,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. from ctypes import windll, Structure, c_long, byref
  2.  
  3. import time
  4. import cv2
  5. import mss
  6. import numpy as np
  7.  
  8. import pyautogui
  9.  
  10. class POINT(Structure):
  11.     _fields_ = [("x", c_long), ("y", c_long)]
  12.  
  13. def queryMousePosition():
  14.     pt = POINT()
  15.     windll.user32.GetCursorPos(byref(pt))
  16.     return { "x": pt.x, "y": pt.y}
  17.  
  18. def click():
  19.     pyautogui.mouseDown()
  20.     time.sleep(0.01)
  21.     pyautogui.mouseUp()
  22.  
  23.  
  24. # 800x600 game window (based on 3440x1440 screen resolution)
  25. # mon = {"top": 420, "left": 1320, "width": 800, "height": 600}
  26.  
  27. title = "Terraria Auto-Fishing"
  28. sct = mss.mss()
  29.  
  30. print("STARTING after 15 seconds, please adjust your rod!")
  31. time.sleep(15)
  32. print("Started ...")
  33.  
  34. click()
  35. print("Rod dropped ...")
  36. last_time = time.time() # time last fish was catched
  37.  
  38.  
  39.  
  40. while True:
  41.     # must be at least 2 seconds before last catch
  42.     if time.time() - last_time < 2:
  43.         continue
  44.  
  45.     cur = queryMousePosition()
  46.     mon = {"top": cur['y'] -5, "left": cur['x'] -5, "width": 10, "height": 10}
  47.     img = np.asarray(sct.grab(mon))
  48.  
  49.     #cv2.imshow(title, img)
  50.     #if cv2.waitKey(25) & 0xFF == ord("q"):
  51.     #   cv2.destroyAllWindows()
  52.     #   quit()
  53.  
  54.     # create hsv
  55.     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  56.  
  57.     # define masks
  58.     # lower mask (0-10)
  59.     lower_red = np.array([0,50,50])
  60.     upper_red = np.array([10,255,255])
  61.     mask0 = cv2.inRange(hsv, lower_red, upper_red)
  62.  
  63.     # upper mask (170-180)
  64.     lower_red = np.array([170,50,50])
  65.     upper_red = np.array([180,255,255])
  66.     mask1 = cv2.inRange(hsv, lower_red, upper_red)
  67.  
  68.     # join masks
  69.     mask = mask0+mask1
  70.  
  71.     # check
  72.     hasRed = np.sum(mask)
  73.     if hasRed > 0:
  74.         #print("RED detected!") # do nothing
  75.         pass
  76.     else:
  77.         #print("RED NOT detected!") # catch!
  78.  
  79.         # get the stuff
  80.         print("Catch! ...")
  81.         time.sleep(0.3)
  82.         click()
  83.  
  84.         time.sleep(1) # wait some
  85.         print("New rod dropped ...")
  86.         click()
  87.  
  88.         last_time = time.time() # time last fish was catched
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement