main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import cv2 as cv
  2. from mss import mss
  3. import numpy as np
  4. from time import sleep, time
  5. import pyautogui as inp
  6. from matplotlib import pyplot as plt
  7. import sys
  8. np.set_printoptions(threshold=sys.maxsize)
  9. inp.sleep(10)
  10. img = 0
  11. dino_rel_x = 35
  12. dino_rel_y = 101
  13. start_trig_x = dino_rel_x + 68
  14. trig_x = start_trig_x
  15. trig_y_high = dino_rel_y - 7
  16. trig_y_low = dino_rel_y + 25
  17. start_hold_coeff = 1000
  18. hold_coeff = start_hold_coeff
  19. def hold_key(key, hold_time):
  20. inp.hold(key, hold_time)
  21. cv.namedWindow("opencv")
  22. with mss() as sct:
  23. monitor = {"top": 0, "left": 0, "width": 2560, "height": 1440}
  24. dino_img = cv.imread("dino-u.png")
  25. dino_img = cv.cvtColor(dino_img, cv.COLOR_RGB2GRAY)
  26. img = np.array(sct.grab(monitor))
  27. img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
  28. t_min_val, t_max_val, t_min_loc, t_max_loc = cv.minMaxLoc(
  29. cv.matchTemplate(img, dino_img, cv.TM_SQDIFF_NORMED)
  30. )
  31. print(t_min_val, t_max_val, t_min_loc, t_max_loc)
  32. monitor = {
  33. "top": t_min_loc[1] - 94,
  34. "left": t_min_loc[0] - 4,
  35. "width": 300,
  36. "height": 153,
  37. }
  38. start_time = time()
  39. inp.press("space")
  40. while True:
  41. trig_x = np.clip(int(start_trig_x + (time() - start_time) / 2), 0, 152)
  42. img = np.array(sct.grab(monitor))
  43. img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
  44. _, img = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
  45. low_trig = np.any(img[trig_y_low, trig_x - 25 : trig_x])
  46. high_trig = np.any(img[trig_y_high, trig_x - 10 : trig_x])
  47. if cv.waitKey(1) == ord("q"):
  48. cv.destroyAllWindows()
  49. break
  50. if low_trig:
  51. left = 9999
  52. right = 0
  53. for x in range(trig_x - 10, trig_x + 100):
  54. if img[trig_y_low - 1 : trig_y_low + 1, x].max() == 0:
  55. continue
  56. if x < left:
  57. left = x
  58. if x > right:
  59. right = x
  60. ln = (right - left) * 20 / (hold_coeff + (time() - start_time) * 40)
  61. if high_trig:
  62. ln += 0.1
  63. print("hi-cac")
  64. if ln < 0:
  65. print("xx")
  66. ln = 0.2
  67. print("cactus/low-bird", left - trig_x, right - trig_x, right - left, ln)
  68. inp.press("up")
  69. sleep(ln / 4)
  70. inp.keyDown("down")
  71. sleep(0.03)
  72. inp.keyUp("down")
  73. elif high_trig:
  74. print("bird")
  75. inp.keyDown("down")
  76. sleep(0.15)
  77. inp.keyUp("down")
  78. else:
  79. pass
  80. # high-box
  81. img = cv.rectangle(
  82. img, (trig_x - 10, trig_y_high), (trig_x, trig_y_high), (255, 0, 0), 1
  83. )
  84. # low-trig
  85. img = cv.rectangle(
  86. img, (trig_x - 25, trig_y_low), (trig_x, trig_y_low), (255, 0, 0), 1
  87. )
  88. # low-box
  89. img = cv.rectangle(
  90. img,
  91. (trig_x - 10, trig_y_low - 5),
  92. (trig_x + 100, trig_y_low),
  93. (255, 0, 0),
  94. 1,
  95. )
  96. cv.imshow("opencv", img)