jezv 1 年之前
父節點
當前提交
1172aebf49
共有 1 個文件被更改,包括 66 次插入60 次删除
  1. 66 60
      trex/main.py

+ 66 - 60
trex/main.py

@@ -9,7 +9,7 @@ import cv2
 
 cv2.namedWindow('View', cv2.WINDOW_NORMAL)
 DEFAULT_MONITOR = 3
-DEBUG = True
+DEBUG = False
 scheduler = sched.scheduler(time, sleep)
 dilation_kernel = np.array([[0,0,0], [1, 1, 1], [0,0,0]]).astype('uint8')
 
@@ -107,70 +107,76 @@ def to_bbox(bin_image):
 
 
 if __name__ == '__main__':
-    sct = mss()
+    with mss() as sct:
 
-    open_dino(sct)
-    gui.hotkey('fn', 'f11')
+        open_dino(sct)
+        gui.hotkey('fn', 'f11')
 
-    playing_bbox, pw, ph = get_playing_bbox(sct)
+        playing_bbox, pw, ph = get_playing_bbox(sct)
 
-    binScreen = take_binaryScreenshot(sct, playing_bbox)
-    binScreen = to_bbox(binScreen)
-    labels, labeledScreen = cv2.connectedComponents(binScreen.astype('uint8'))
-    if DEBUG: rgbScreen = binary_to_RGB(binScreen)
-    dino_label = min(range(1,labels), key=lambda x: np.where(labeledScreen==x)[1].min()) 
-    dino_pos = np.where(labeledScreen==dino_label)
-    dino_max_x = dino_pos[1].max()
-    dino_min_y = dino_pos[0].max()
-    dino_max_y = dino_pos[0].min()
-
-    coef = 1
-    trigger_second = (int(dino_pos[0].mean()), int(dino_max_x + int(coef*10)*pw))
-    trigger_feather = (dino_max_y, int(dino_max_x + int(coef*12)*pw))
-    jumping = False
-    taking = False
-    laing = False
-
-    print(ph, pw)
-
-    start = time()
-
-    if DEBUG: cv2.imshow('View', rgbScreen) 
-    while cv2.waitKey(1) != ord('q'):
         binScreen = take_binaryScreenshot(sct, playing_bbox)
         binScreen = to_bbox(binScreen)
         labels, labeledScreen = cv2.connectedComponents(binScreen.astype('uint8'))
+        if DEBUG: rgbScreen = binary_to_RGB(binScreen)
+        dino_label = min(range(1,labels), key=lambda x: np.where(labeledScreen==x)[1].min()) 
+        dino_pos = np.where(labeledScreen==dino_label)
+        dino_max_x = dino_pos[1].max()
+        dino_min_y = dino_pos[0].max()
+        dino_max_y = dino_pos[0].min()
+
+        coef = 0.9
+        trigger_second = (int(dino_pos[0].mean()), int(dino_max_x + int(coef*10)*pw))
+        trigger_feather = (dino_max_y, int(dino_max_x + int(coef*12)*pw))
+        jumping = False
+        taking = False
+        laing = False
+
+        print(ph, pw)
+
+        start = time()
+        start2= time()
+                                
+        if DEBUG: cv2.imshow('View', rgbScreen) 
+        while True:
+            binScreen = take_binaryScreenshot(sct, playing_bbox)                     
+            binScreen = to_bbox(binScreen)
+            labels, labeledScreen = cv2.connectedComponents(binScreen.astype('uint8'))
+
+            if not jumping and binScreen[trigger_second] != 0 or binScreen[trigger_feather] != 0: 
+                print('jumping')
+                gui.keyDown('space')
+                jumping = True
+
+            elif jumping and binScreen[trigger_second[0], trigger_second[1]+(int(coef-1)*pw)] == 0 and binScreen[trigger_feather[0], trigger_feather[1]+(int(coef-1)*pw)] == 0:
+                print('taking')
+                gui.keyUp('space')
+                gui.keyDown('down')
+                jumping = False
+                taking = True
+            
+            if taking and binScreen[dino_max_y-ph, dino_max_x] != 0:
+                print('take off')
+                gui.keyUp('down')
+                taking = False
+
+            if time() - start2 == 7:
+                print('speeded')
+                coef += 0.2
+                start2 = time()
+
+            if time() - start > 30:
+                print(time() - start)
+                trigger_second = (trigger_second[0], trigger_second[1]+pw)
+                trigger_feather = (trigger_feather[0], trigger_feather[1]+pw)
+                start = time()
+
+            if DEBUG: 
+                rgbScreen = binary_to_RGB(binScreen)
+                cv2.circle(rgbScreen, trigger_feather[::-1], radius=2, color=(255, 0, 0), thickness=2)
+                cv2.circle(rgbScreen, trigger_second[::-1], radius=2, color=(0, 255, 0), thickness=2)
+                cv2.imshow('View', rgbScreen)
+                if cv2.waitKey(1) == ord('q'): break
+            
 
-        if not jumping and binScreen[trigger_second] != 0 or binScreen[trigger_feather] != 0:
-            print('jumping')
-            gui.keyDown('space')
-            jumping = True
-
-        elif jumping and binScreen[trigger_second[0], trigger_second[1]+(int(coef-1)*pw)] == 0 and binScreen[trigger_feather[0], trigger_feather[1]+(int(coef-1)*pw)] == 0:
-            print('taking')
-            gui.keyUp('space')
-            gui.keyDown('down')
-            jumping = False
-            taking = True
-        
-        if taking and binScreen[dino_max_y-ph, dino_max_x] != 0:
-            print('take off')
-            gui.keyUp('down')
-            taking = False
-
-        if time() - start > 15:
-            print(time() - start)
-            coef += 0.1
-            trigger_second = (trigger_second[0], trigger_second[1]+pw)
-            trigger_feather = (trigger_feather[0], trigger_feather[1]+pw)
-            start = time()
-
-        if DEBUG: 
-            rgbScreen = binary_to_RGB(binScreen)
-            cv2.circle(rgbScreen, trigger_feather[::-1], radius=2, color=(255, 0, 0), thickness=2)
-            cv2.circle(rgbScreen, trigger_second[::-1], radius=2, color=(0, 255, 0), thickness=2)
-            cv2.imshow('View', rgbScreen)
         
-
-    
-    cv2.destroyAllWindows()
+        cv2.destroyAllWindows()