Ver Fonte

Solved T-Rex

Vsevolod Levitan há 11 meses atrás
pai
commit
6956a14212
2 ficheiros alterados com 123 adições e 0 exclusões
  1. BIN
      t-rex/dino-u.png
  2. 123 0
      t-rex/main.py

BIN
t-rex/dino-u.png


+ 123 - 0
t-rex/main.py

@@ -0,0 +1,123 @@
+import cv2 as cv
+from mss import mss
+import numpy as np
+from time import sleep, time
+import pyautogui as inp
+from matplotlib import pyplot as plt
+
+import sys
+
+np.set_printoptions(threshold=sys.maxsize)
+
+inp.sleep(10)
+
+img = 0
+
+dino_rel_x = 35
+dino_rel_y = 101
+
+start_trig_x = dino_rel_x + 68
+trig_x = start_trig_x
+trig_y_high = dino_rel_y - 7
+trig_y_low = dino_rel_y + 25
+start_hold_coeff = 1000
+hold_coeff = start_hold_coeff
+
+
+def hold_key(key, hold_time):
+    inp.hold(key, hold_time)
+
+
+cv.namedWindow("opencv")
+
+with mss() as sct:
+    monitor = {"top": 0, "left": 0, "width": 2560, "height": 1440}
+
+    dino_img = cv.imread("dino-u.png")
+    dino_img = cv.cvtColor(dino_img, cv.COLOR_RGB2GRAY)
+
+    img = np.array(sct.grab(monitor))
+    img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
+
+    t_min_val, t_max_val, t_min_loc, t_max_loc = cv.minMaxLoc(
+        cv.matchTemplate(img, dino_img, cv.TM_SQDIFF_NORMED)
+    )
+
+    print(t_min_val, t_max_val, t_min_loc, t_max_loc)
+
+    monitor = {
+        "top": t_min_loc[1] - 94,
+        "left": t_min_loc[0] - 4,
+        "width": 300,
+        "height": 153,
+    }
+
+    start_time = time()
+
+    inp.press("space")
+
+    while True:
+        trig_x = np.clip(int(start_trig_x + (time() - start_time) / 2), 0, 152)
+
+        img = np.array(sct.grab(monitor))
+        img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
+        _, img = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
+
+        low_trig = np.any(img[trig_y_low, trig_x - 25 : trig_x])
+
+        high_trig = np.any(img[trig_y_high, trig_x - 10 : trig_x])
+
+        if cv.waitKey(1) == ord("q"):
+            cv.destroyAllWindows()
+            break
+
+        if low_trig:
+            left = 9999
+            right = 0
+            for x in range(trig_x - 10, trig_x + 100):
+                if img[trig_y_low - 1 : trig_y_low + 1, x].max() == 0:
+                    continue
+                if x < left:
+                    left = x
+                if x > right:
+                    right = x
+            ln = (right - left) * 20 / (hold_coeff + (time() - start_time) * 40)
+            if high_trig:
+                ln += 0.1
+                print("hi-cac")
+            if ln < 0:
+                print("xx")
+                ln = 0.2
+            print("cactus/low-bird", left - trig_x, right - trig_x, right - left, ln)
+            inp.press("up")
+            sleep(ln / 4)
+            inp.keyDown("down")
+            sleep(0.03)
+            inp.keyUp("down")
+        elif high_trig:
+            print("bird")
+            inp.keyDown("down")
+            sleep(0.15)
+            inp.keyUp("down")
+        else:
+            pass
+
+        # high-box
+        img = cv.rectangle(
+            img, (trig_x - 10, trig_y_high), (trig_x, trig_y_high), (255, 0, 0), 1
+        )
+
+        # low-trig
+        img = cv.rectangle(
+            img, (trig_x - 25, trig_y_low), (trig_x, trig_y_low), (255, 0, 0), 1
+        )
+        # low-box
+        img = cv.rectangle(
+            img,
+            (trig_x - 10, trig_y_low - 5),
+            (trig_x + 100, trig_y_low),
+            (255, 0, 0),
+            1,
+        )
+
+        cv.imshow("opencv", img)