main.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import webbrowser
  2. from mss import mss
  3. import numpy as np
  4. from time import sleep, time
  5. import pyautogui as gui
  6. import sched
  7. import cv2
  8. from collections import namedtuple
  9. cv2.namedWindow('View', cv2.WINDOW_NORMAL)
  10. DEFAULT_MONITOR = 3
  11. DEBUG = True
  12. scheduler = sched.scheduler(time, sleep)
  13. def take_screenshot(sct, monitor=DEFAULT_MONITOR):
  14. monitor = sct.monitors[monitor]
  15. image = np.array(sct.grab(monitor))
  16. return image
  17. def take_binaryScreenshot(sct, monitor: dict):
  18. image = np.array(sct.grab(monitor))
  19. binaryScreen = np.zeros(image.shape[:-1])
  20. binaryScreen[np.all(image == (83, 83, 83, 0), axis=2)] = 1
  21. binaryScreen = cv2.erode(binaryScreen, None, iterations=1)
  22. binaryScreen = cv2.dilate(binaryScreen, None, iterations=1)
  23. return binaryScreen
  24. def binary_to_RGB(image):
  25. rgb_image = np.zeros((*image.shape, 3))
  26. rgb_image[image == 1] = (255, 255, 255)
  27. return rgb_image
  28. def open_dino(sct):
  29. prevScreen = take_screenshot(sct)
  30. webbrowser.open('https://chromedino.com/')
  31. sleep(1)
  32. Screen = take_screenshot(sct)
  33. while np.any(Screen != prevScreen):
  34. cv2.imshow('View', Screen)
  35. cv2.waitKey(1)
  36. sleep(1)
  37. prevScreen = Screen.copy()
  38. Screen = take_screenshot(sct)
  39. print('loaded')
  40. def short_jump():
  41. with gui.hold('space'):
  42. sleep(0.07)
  43. def long_jump():
  44. with gui.hold('space'):
  45. sleep(0.2)
  46. def take_down():
  47. with gui.hold('down'):
  48. sleep(0.05)
  49. def get_playing_bbox(sct):
  50. Screen = take_screenshot(sct)
  51. binaryScreen = np.zeros(Screen.shape[:-1])
  52. binaryScreen[np.all(Screen == (83, 83, 83, 0), axis=2)] = 1
  53. short_jump()
  54. sleep(1)
  55. binScreen = take_binaryScreenshot(sct, sct.monitors[DEFAULT_MONITOR])
  56. x, y = np.where(binScreen == 1)
  57. mw = int((y.max()-y.min()) / 100)
  58. mh = int((x.max()-x.min()) / 100)
  59. playing_bbox = {'left':y.min()-2*mw, 'top':x.min()+15*mh, 'width':(y.max()-y.min())+4*mw, 'height':(x.max()-x.min())+10*mh}
  60. return playing_bbox, mw, mh
  61. def to_bbox(bin_image):
  62. bin_image = cv2.dilate(bin_image, None, iterations=2)
  63. labels, labeledScreen = cv2.connectedComponents(bin_image.astype('uint8'))
  64. for label in range(1, labels):
  65. x,y = np.where(labeledScreen==label)
  66. bin_image[x.min():x.max()+1, y.min():y.max()+1] = 1
  67. return bin_image
  68. if __name__ == '__main__':
  69. sct = mss()
  70. open_dino(sct)
  71. gui.hotkey('fn', 'f11')
  72. playing_bbox, mw, mh = get_playing_bbox(sct)
  73. dino_pos = None
  74. awaitable = None
  75. binScreen = take_binaryScreenshot(sct, playing_bbox)
  76. binScreen = to_bbox(binScreen)
  77. labels, labeledScreen = cv2.connectedComponents(binScreen.astype('uint8'))
  78. if DEBUG: rgbScreen = binary_to_RGB(binScreen)
  79. dino = min(range(1,labels), key=lambda x: np.where(labeledScreen==x)[1].min())
  80. dino_pos = np.where(labeledScreen == dino)[1].max() + 5*mw
  81. jump_zone = dino_pos + 13*mw
  82. if DEBUG: rgbScreen[labeledScreen==dino] = (0, 255, 0)
  83. start = time()
  84. while cv2.waitKey(1) != ord('q'):
  85. binScreen = take_binaryScreenshot(sct, playing_bbox)
  86. binScreen = to_bbox(binScreen)
  87. labels, labeledScreen = cv2.connectedComponents(binScreen.astype('uint8'))
  88. if DEBUG: rgbScreen = binary_to_RGB(binScreen)
  89. for label in range(1, labels):
  90. pos = np.where(labeledScreen == label)
  91. if pos[1].max() < dino_pos:
  92. if DEBUG: rgbScreen[pos] = (0, 255, 0)
  93. elif pos[1].max() < jump_zone + max(1*(pos[0].max() - pos[0].min()), 1*(pos[1].max() - pos[1].min())) and (pos[1].max() - pos[1].min()) > mw :
  94. # Ближайший Противник
  95. if DEBUG: rgbScreen[pos] = (0, 0, 255)
  96. if (pos[1].max() - pos[1].min()) > 8*mw:
  97. long_jump()
  98. else: short_jump()
  99. if time() - start > 10:
  100. jump_zone += mw
  101. start = time()
  102. cv2.imshow('View', rgbScreen)
  103. cv2.destroyAllWindows()