-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final vers.py
120 lines (89 loc) · 2.68 KB
/
Final vers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from ctypes import windll, Structure, c_long, byref
import time
import cv2
import mss
import numpy as np
import pyautogui
class POINT(Structure):
_fields_ = [("x", c_long), ("y", c_long)]
def queryMousePosition():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return {"x": pt.x, "y": pt.y}
def click():
pyautogui.mouseDown()
time.sleep(0.01)
pyautogui.mouseUp()
# 800x600 game window (based on 3440x1440 screen resolution)
# mon = {"top": 420, "left": 1320, "width": 800, "height": 600}
title = "Terraria Auto-Fishing"
sct = mss.mss()
print("STARTING after 15 seconds, please adjust your rod!")
time.sleep(5)
print("Started ...")
click()
print("Rod dropped ...")
last_time = time.time() # time last fish was catched
def screen_record_efficient():
global sct
cur = queryMousePosition()
mon = {"top": cur['y'] - 10, "left": cur['x'], "width": 10, "height": 10}
img = np.asarray(sct.grab(mon))
print(img)
title = '[MSS] FPS benchmark'
fps = 0
sct = mss.mss()
last_time = time.time()
while True:
img = np.asarray(sct.grab(mon))
fps +=1
cv2.imshow(title, img)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
print('Mss:', screen_record_efficient())
while True:
# must be at least 2 seconds before last catch
if time.time() - last_time < 2:
continue
cur = queryMousePosition()
mon = {"top": cur['y'] - 10, "left": cur['x'], "width": 10, "height": 10}
img = np.asarray(sct.grab(mon))
# cv2.imshow(title, img)
# if cv2.waitKey(25) & 0xFF == ord("q"):
# cv2.destroyAllWindows()
# quit()
# create hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define masks
# lower mask (0-10)
lower_red = np.array([0, 50, 50])
upper_red = np.array([10, 255, 255])
mask0 = cv2.inRange(hsv, lower_red, upper_red)
# upper mask (170-180)
lower_red = np.array([170, 50, 50])
upper_red = np.array([180, 255, 255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
# join masks
mask = mask0 + mask1
while True:
img = np.asarray(sct.grab(mon))
cv2.imshow(title, img)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
# check
hasRed = np.sum(mask)
if hasRed > 0:
# print("RED detected!") # do nothing
pass
else:
# print("RED NOT detected!") # catch!
# get the stuff
print("Catch! ...")
time.sleep(0.3)
click()
time.sleep(1) # wait som
print("New rod dropped ...")
click()
last_time = time.time() # time last fish was catched