Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making a cross-platform product #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Inspired by [https://github.com/ivanseidel/IAMDinosaur](https://github.com/ivans
* Run the AI: `python ai.py`
* After 5/8 generations, the dino should be a ninja!

## FAQ
* Try this first is you have problem installing pyHook: https://stackoverflow.com/questions/48327695/pyhook-for-python-3-6
* You maybe should install this if you still cannot install pyHook: https://nchc.dl.sourceforge.net/project/pywin32/pywin32/Build%20221/pywin32-221.win-amd64-py3.6.exe

## TODO

- [ ] Find game automatically in the screen at the beginning
Expand Down
4 changes: 2 additions & 2 deletions dino-ml/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def execute(self):
scanner.find_game()
for genome in self.__genomes:
scanner.reset()
k.press_keys(['Command', 'r'])
k.release_key('Command')
k.press_keys([k.control_l_key, 'r'])
k.release_key(k.control_l_key)
sleep(1)
k.press_key(k.space)
while True:
Expand Down
13 changes: 8 additions & 5 deletions dino-ml/scanner.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from PIL import Image
from PIL import ImageGrab
from datetime import datetime
import os

dino_color = (83, 83, 83, 255)
dino_color = (83, 83, 83)

def screenshot(x, y, w, h):
os.system("screencapture -R{},{},{},{} tmp.png".format(x, y, w, h))
img = Image.open("tmp.png")
return img
# os.system("screencapture -R{},{},{},{} tmp.png".format(x, y, w, h))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be done same as the main code( taking screenshots of custom window size) by the following code :

import win32gui

...

def screenshot(x, y, w, h):

toplist, winlist = [] ,[]
def enum_cb(hwnd, results):
    winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
win32gui.EnumWindows(enum_cb, toplist)

chrome = [(hwnd, title) for hwnd, title in winlist if 'chrome' in title.lower()]
# just grab the hwnd for first window matching chrome
chrome = chrome[0]
hwnd = chrome[0]

win32gui.SetForegroundWindow(hwnd)
bbox = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox=(x,y,w,h))
img.save('tmp.PNG')
return img

img = ImageGrab.grab()
img.save("tmp.png")
save = Image.open("tmp.png")
return save

def is_dino_color(pixel):
return pixel == dino_color
Expand Down Expand Up @@ -35,7 +38,7 @@ def find_game(self):

if not pixels:
raise Exception("Game not found!")

print("Game found!")
self.__find_dino(pixels)

def __find_dino(self, pixels):
Expand Down
Binary file added pyHook-1.5.1-cp36-cp36m-win32.whl
Binary file not shown.
Binary file added pyHook-1.5.1-cp36-cp36m-win_amd64.whl
Binary file not shown.