Skip to content

Instantly share code, notes, and snippets.

@DarkCoder28
Last active April 15, 2019 12:29
Show Gist options
  • Save DarkCoder28/a3e27b6fd1075b8a88b0b9b219c4413e to your computer and use it in GitHub Desktop.
Save DarkCoder28/a3e27b6fd1075b8a88b0b9b219c4413e to your computer and use it in GitHub Desktop.
The code used in my falling brick solver video
import time
import cv2
import mss
import numpy
import random
from pynput.keyboard import Key, Controller
keyboard = Controller()
topleft = (-1220, 492)
# -220, 1255
board = [["B" for y in range(8)] for x in range(5)]
def findPlayer():
player = (0, 0)
for x in range(5):
for y in range(8):
if board[x][y] == 'P':
player = (x, y)
return player
def solver(player):
solve(player[0], player[1])
def solve(x, y):
done = False
if locValid(x, y):
board[x][y] = 'N'
if (y == 7):
done = True
else:
done = solve(x, y+1)
if done == False:
done = solve(x+1, y)
if done == False:
done = solve(x-1, y)
if done and board[x][y] != 'P':
board[x][y] = 'F'
return done
def locValid(x, y):
if x >= 0 and x < 5 and y >= 0 and y < 8:
if board[x][y] == 'O' or board[x][y] == 'P':
return True
return False
def applySolution(player):
if player[0] >= 0 and player[0] < 5 and player[1]+1 >= 0 and player[1]+1 < 8 and board[player[0]][player[1]+1] == 'F':
pressKey(Key.space)#pressKey(Key.down)
elif player[0]+1 >= 0 and player[0]+1 < 5 and player[1] >= 0 and player[1] < 8 and board[player[0]+1][player[1]] == 'F':
pressKey(Key.right)
elif player[0]-1 >= 0 and player[0]-1 < 5 and player[1] >= 0 and player[1] < 8 and board[player[0]-1][player[1]] == 'F':
pressKey(Key.left)
else:
return False
return True
def pressKey(key):
keyboard.press(key)
keyboard.release(key)
with mss.mss() as sct:
# Part of screen to capture
monitor = {"top": 492, "left": -1220, "width": 1000, "height": 763}
print("Begin Capture")
while "Screen Capturing":
last_time = time.time()
# Get raw pixels from screen, save to Numpy array
img = numpy.array(sct.grab(monitor))
# Sample locations and add to array
# Then draw circles on all sample locations
for x in range(5):
for y in range(8):
pos = ((100+(200*x)), (60+(90*y)))
pixel = img[pos[1], pos[0]]
if pixel[0] == 86 and pixel[1] == 254 and pixel[2] == 254:
board[x][y] = 'P'
elif pixel[0] == 125 and pixel[1] == 125 and pixel[2] == 125:
board[x][y] = 'O'
else:
board[x][y] = 'B'
cv2.circle(img, pos, 5, (0, 0, 255))
# Find Player
player = findPlayer()
# Recursivly solve the game
solver(player)
for y in range(8):
for x in range(5):
if board[x][y] == 'F':
pos = ((100+(200*x)), (60+(90*y)))
pt1 = (pos[0]-70, pos[1]-30)
pt2 = (pos[0]+70, pos[1]+30)
cv2.rectangle(img, pt1, pt2, (255, 255, 0), -1)
applySolution(player)
# Print FPS on image
fps = int(1/(time.time()-last_time))
cv2.putText(img, "FPS: {}".format(str(fps)), (20, 20), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
# Display picture
cv2.imshow("OpenCV/Numpy normal", img)
# Press 'Q' to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
print("End of Capture")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment