main.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import webbrowser
  2. from mss import mss
  3. import numpy as np
  4. from time import sleep
  5. import pyautogui as gui
  6. import cv2
  7. cv2.namedWindow('View', cv2.WINDOW_NORMAL)
  8. DEFAULT_MONITOR = 3
  9. def take_screenshot(sct, monitor=DEFAULT_MONITOR):
  10. monitor = sct.monitors[monitor]
  11. image = np.array(sct.grab(monitor))
  12. return image
  13. def take_binaryScreenshot(sct, monitor: dict):
  14. image = np.array(sct.grab(monitor))
  15. binaryScreen = np.zeros(image.shape[:-1])
  16. binaryScreen[np.all(image == (83, 83, 83, 0), axis=2)] = 1
  17. binaryScreen = cv2.erode(binaryScreen, None, iterations=1)
  18. return binaryScreen
  19. def open_dino(sct):
  20. prevScreen = take_screenshot(sct)
  21. webbrowser.open('https://chromedino.com/')
  22. sleep(1)
  23. Screen = take_screenshot(sct)
  24. while np.any(Screen != prevScreen):
  25. cv2.imshow('View', Screen)
  26. cv2.waitKey(1)
  27. sleep(1)
  28. prevScreen = Screen.copy()
  29. Screen = take_screenshot(sct)
  30. print('loaded')
  31. def short_jump():
  32. with gui.hold('space'):
  33. sleep(0.05)
  34. def long_jump():
  35. with gui.hold('space'):
  36. sleep(0.2)
  37. def get_playing_bbox(sct):
  38. Screen = take_screenshot(sct)
  39. binaryScreen = np.zeros(Screen.shape[:-1])
  40. binaryScreen[np.all(Screen == (83, 83, 83, 0), axis=2)] = 1
  41. short_jump()
  42. sleep(1)
  43. binScreen = take_binaryScreenshot(sct, sct.monitors[DEFAULT_MONITOR])
  44. x, y = np.where(binScreen == 1)
  45. mw = int((y.max()-y.min()) / 100)
  46. mh = int((x.max()-x.min()) / 100)
  47. playing_bbox = {'left':y.min()-2*mw, 'top':x.min()-5*mh, 'width':(y.max()-y.min())+4*mw, 'height':(x.max()-x.min())+10*mh}
  48. return playing_bbox
  49. if __name__ == '__main__':
  50. sct = mss()
  51. open_dino(sct)
  52. gui.hotkey('fn', 'f11')
  53. playing_bbox = get_playing_bbox(sct)
  54. while cv2.waitKey(1) != ord('q'):
  55. binScreen = take_binaryScreenshot(sct, playing_bbox)
  56. cv2.imshow('View', binScreen)
  57. cv2.waitKey(1)
  58. cv2.destroyAllWindows()