본문 바로가기

꿀팁!

스터디6-T-Rex Runner game hack(openCV를 이용한 이미지처리)

T-Rex Runner game hack

 

canvas tag->2D 랜더링하는데 쓰임 -> 웹에서 그림 사각형 원 등

 

Runner.instance_.distanceRan=999999 ->소스분석후 score에 적용되는것을 알 수 있음

 

opencv를 이용하는 방법

이번에도 역시 virtualenv사용

pip install virtualenv

python -m venv env

call venv/scripts/activate 윈도우에선 call을 사용한다

 

pip install opencv-python
pip install pywin32
Pip install grab-screen
pip install pyautogui

 

공룡인식

 

import cv2
import numpy as np
from grabscreen import grab_screen
import glob
import pyautogui


dino=cv2.imread('dino.jpg',0)
w_dino, h_dino=dino.shape[::-1]

while True:
    scr=grab_screen(region=(75,250,750,450))
    scr_gray=cv2.cvtColor(scr,cv2.COLOR_BGR2GRAY)

    res_dino=cv2.matchTemplate(scr_gray, dino, cv2.TM_CCOEFF_NORMED)
    threshold=0.6
    loc_dino=np.where(res_dino>=threshold)

    for pt in zip(*loc_dino[::-1]):
        cv2.rectangle(scr, pt,(pt[0]+w_dino, pt[1]+h_dino),(0,0,255),1)
        dinox=pt[0]+w_dino
        dinoH=pt[1]

    cv2.imshow('screen',scr)
    if cv2.waitKey(1)&0xff==ord('q'):
        cv2.destoryAllWindows()
        break

main.py

 

 

import cv2
import numpy as np
import win32gui, win32ui, win32con, win32api

def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)

 

grabscreen.py

 

일반적인 장애물 피하기

import cv2
import numpy as np
from grabscreen import grab_screen
import glob
import pyautogui


dino=cv2.imread('dino.jpg',0)
w_dino, h_dino=dino.shape[::-1]

files=glob.glob('cacti/*.jpg')
cacti=[]
for file in files:
    tmp=cv2.imread(file,0)
    cacti.append(tmp)


while True:
    dinox=0
    leftest=1000
    scr=grab_screen(region=(75,250,750,450))
    scr_gray=cv2.cvtColor(scr,cv2.COLOR_BGR2GRAY)

    res_dino=cv2.matchTemplate(scr_gray, dino, cv2.TM_CCOEFF_NORMED)
    threshold=0.6
    loc_dino=np.where(res_dino>=threshold)

    for pt in zip(*loc_dino[::-1]):
        cv2.rectangle(scr, pt,(pt[0]+w_dino, pt[1]+h_dino),(0,0,255),1)
        dinox=pt[0]+w_dino
        dinoH=pt[1]
    pts=[]
    for cactus in cacti:
        res=cv2.matchTemplate(scr_gray, cactus, cv2.TM_CCOEFF_NORMED)
        w, h=cactus.shape[::-1]
        loc=np.where(res>=0.6)
        for pt in zip(*loc[::-1]):
            if(pt in pts):
                continue
            cv2.rectangle(scr,pt,(pt[0]+w,pt[1]+h),(0,0,255),1)
            pts.append(pt)
            if(leftest>pt[0]+w and pt[0] >dinox):
                leftest=pt[0]+w
    if(leftest-dinox<150 ):
        cv2.putText(scr,'Jump!',(0,20), cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,0),2,cv2.LINE_AA)
        pyautogui.press('space')
        print(dinox,leftest)

    cv2.imshow('screen',scr)
    if cv2.waitKey(1)&0xff==ord('q'):
        cv2.destoryAllWindows()
        break

main.py

 

 

   

'꿀팁!' 카테고리의 다른 글

modern C++ 공부했던것  (0) 2020.02.29
스터디7  (0) 2020.02.17
스터디5  (0) 2020.01.20
스터디4  (0) 2020.01.17
스터디2-파이썬을 이용한 암호화  (0) 2020.01.10