Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- import pyautogui
- import time
- from PIL import ImageGrab
- # Defining Template Images
- gameOver = cv2.imread('GameOver.png')
- dino = cv2.imread('Dino.png')
- smallCactus = cv2.imread('SmallCactus.png')
- bigCactus = cv2.imread('BigCactus.png')
- ptero = cv2.imread('Ptero.png')
- # Assigning Sample Image Dimensions
- h, w = dino.shape[:-1]
- sch, scw = smallCactus.shape[:-1]
- bch, bcw = bigCactus.shape[:-1]
- ph, pw = ptero.shape[:-1]
- # Time Variables
- lastTime = time.time()
- runningTime = 0
- # Key Variables
- keyDown = False
- while True:
- # Capturing Screen
- # 'bbox' Is Rectangle Around The Game
- screen = np.array(ImageGrab.grab(bbox=(150,125,800,300)))
- # Time stuff
- #print('Loop took {} seconds'.format(time.time() - lastTime))
- runningTime += time.time() - lastTime
- lastTime = time.time()
- # Checking If Game Over
- gameOverRes = cv2.matchTemplate(screen, gameOver, cv2.TM_CCOEFF_NORMED)
- minValG, maxValG, minLocG, maxLocG = cv2.minMaxLoc(gameOverRes)
- if maxValG >= 0.9 and runningTime > 4:
- print('Game Ended In ', int(round(runningTime)), ' Seconds')
- pyautogui.press('space')
- runningTime = 0
- # Finding Dinosaur
- dinoRes = cv2.matchTemplate(screen, dino, cv2.TM_CCOEFF_NORMED)
- minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(dinoRes)
- # Finding Small Cacti
- smallCactusRes = cv2.matchTemplate(screen, smallCactus, cv2.TM_CCOEFF_NORMED)
- smallCactusThreshhold = 0.725
- smallCactusLoc = np.where(smallCactusRes >= smallCactusThreshhold)
- # Finding Big Cacti
- bigCactusRes = cv2.matchTemplate(screen, bigCactus, cv2.TM_CCOEFF_NORMED)
- bigCactusThreshhold = 0.725
- bigCactusLoc = np.where(bigCactusRes >= bigCactusThreshhold)
- # Finding Pterodactyls
- pteroRes = cv2.matchTemplate(screen, ptero, cv2.TM_CCOEFF_NORMED)
- minValP, maxValP, minLocP, maxLocP = cv2.minMaxLoc(pteroRes)
- # Drawing Box Around Dinosaur
- cv2.rectangle(screen, maxLoc, (maxLoc[0] + w, maxLoc[1] + h), (0, 255, 0), 2)
- # Avoiding Closest Small Cactus
- if smallCactusLoc[0].size > 0:
- leftmostXS = min(smallCactusLoc[1])
- leftmostYS = min(smallCactusLoc[0])
- distS = (leftmostXS - maxLoc[0])
- if distS < 175 and distS > 0:
- pyautogui.press('space')
- cv2.rectangle(screen, (leftmostXS, leftmostYS), (leftmostXS+scw, leftmostYS+sch), (255, 160, 0), 2)
- # Avoiding Closest Big Cactus
- if bigCactusLoc[0].size > 0:
- leftmostXB = min(bigCactusLoc[1])
- leftmostYB = min(bigCactusLoc[0])
- distB = (leftmostXB - maxLoc[0])
- if distB < 175 and distB > 0:
- pyautogui.press('space')
- cv2.rectangle(screen, (leftmostXB, leftmostYB), (leftmostXB+bcw, leftmostYB+bch), (255, 0, 0), 2)
- # Avoiding Pterodactyls
- # Check 'maxValP' Because Otherwise Dino Gets Mistaken As Pterodactyl
- # 'keyDown' Is Needed For Down Arrow, Otherwise It Doesn't Work Properly
- if maxValP >= 0.60:
- distP = maxLocP[0] - maxLoc[0]
- heightP = maxLoc[1] - maxLocP[1]
- if distP < 190 and distP > 0:
- if heightP > 10:
- keyDown = True
- pyautogui.keyDown('down')
- else:
- pyautogui.press('space')
- cv2.rectangle(screen, maxLocP, (maxLocP[0] + pw, maxLocP[1] + ph), (0, 0, 255), 2)
- elif keyDown == True:
- pyautogui.keyUp('down')
- keyDown = False
- # Showing Image
- cv2.imshow('Dino Game', cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
- # Quit
- if cv2.waitKey(1) & 0xFF == 27:
- cv2.destroyAllWindows()
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement