Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from PIL import ImageGrab
- import cv2
- import time
- ## import pyautogui
- ## from directkeys import PressKey, W, A, S, D
- import ctypes
- SendInput = ctypes.windll.user32.SendInput
- W = 0x11
- # C struct redefinitions
- PUL = ctypes.POINTER(ctypes.c_ulong)
- class KeyBdInput(ctypes.Structure):
- _fields_ = [("wVk", ctypes.c_ushort),
- ("wScan", ctypes.c_ushort),
- ("dwFlags", ctypes.c_ulong),
- ("time", ctypes.c_ulong),
- ("dwExtraInfo", PUL)]
- class HardwareInput(ctypes.Structure):
- _fields_ = [("uMsg", ctypes.c_ulong),
- ("wParamL", ctypes.c_short),
- ("wParamH", ctypes.c_ushort)]
- class MouseInput(ctypes.Structure):
- _fields_ = [("dx", ctypes.c_long),
- ("dy", ctypes.c_long),
- ("mouseData", ctypes.c_ulong),
- ("dwFlags", ctypes.c_ulong),
- ("time",ctypes.c_ulong),
- ("dwExtraInfo", PUL)]
- class Input_I(ctypes.Union):
- _fields_ = [("ki", KeyBdInput),
- ("mi", MouseInput),
- ("hi", HardwareInput)]
- class Input(ctypes.Structure):
- _fields_ = [("type", ctypes.c_ulong),
- ("ii", Input_I)]
- # Actuals Functions
- def PressKey(hexKeyCode):
- extra = ctypes.c_ulong(0)
- ii_ = Input_I()
- ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
- x = Input( ctypes.c_ulong(1), ii_ )
- ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
- def ReleaseKey(hexKeyCode):
- extra = ctypes.c_ulong(0)
- ii_ = Input_I()
- ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
- x = Input( ctypes.c_ulong(1), ii_ )
- ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
- template = cv2.imread('template1.png', 0)
- widtht, heightt = template.shape[::-1]
- def process_img(image):
- original_image = image
- # convert to gray
- processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- # edge detection
- processed_img = cv2.Canny(processed_img, threshold1 = 200, threshold2=300)
- return processed_img
- def tap():
- PressKey(W)
- time.sleep(0.23)
- ReleaseKey(W)
- time.sleep(0.2)
- def main():
- print('PLEASE FOCUS ON THE GAME!')
- for i in list(range(4))[::-1]:
- print(i+1)
- time.sleep(1)
- last_time = time.time()
- while 1:
- # TODO: Implement real AI
- ## tap()
- screen = np.array(ImageGrab.grab(bbox=(0,45,800,500)))
- #print('Frame took {} seconds'.format(time.time()-last_time))
- last_time = time.time()
- new_screen = process_img(screen)
- match = cv2.matchTemplate(new_screen, template, cv2.TM_CCOEFF_NORMED)
- threshold = 0.8
- positiont = np.where(match >= threshold)
- for point in zip(*positiont[::-1]): #draw the rectangle around the matched template
- cv2.rectangle(new_screen, point, (point[0] + widtht, point[1] + heightt), (0, 204, 153), 0)
- print(1)
- cv2.imshow('window', new_screen)
- #cv2.imshow('window',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
- if cv2.waitKey(25) & 0xFF == ord('q'):
- cv2.destroyAllWindows()
- break
- if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement