diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7459211 --- /dev/null +++ b/.gitignore @@ -0,0 +1,107 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# screenshots +screenshot* diff --git a/README.md b/README.md index a5bfcf6..7357fc1 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Chrome Dino Bot -A bot that plays the boring chrome dinasour game that appears when no internet available. +A bot that plays the boring chrome dinosaur game that appears when no internet available. *** ![Game Screenshot](https://9to5google.com/wp-content/uploads/sites/4/2018/09/chrome-offline-dino-game.jpg?quality=82&strip=all) diff --git a/bot.py b/bot.py index 8e5262b..9e99353 100644 --- a/bot.py +++ b/bot.py @@ -1,11 +1,19 @@ import pyautogui as gui import keyboard -import time -import math +import time +# Set a debug flag for verbose output +debug_flag=True + + +if debug_flag: + print("Starting. Press Q to quit") + +#Startup time to switch tabs +time.sleep(5) # Helper function to get value of pixel in image def getPixel(Image,x, y): - px = Image.load() + px = Image.load() return px[x, y] @@ -21,12 +29,14 @@ def getPixel(Image,x, y): # helper variables to calculate time last = 0 total_time = 0 +screenshot_counter=0 # the intervals where the bot will search for obstacles y_search, x_start, x_end = 350, 435, 450 y_search2 = 275 # for the birds + time.sleep(1) while True: t1 = time.time() @@ -34,15 +44,11 @@ def getPixel(Image,x, y): break # increase the search width every second to simulate the dino acceleration - if math.floor(total_time) != last: + if int(total_time) != last: x_end += 4 if x_end >= width: x_end = width - last = math.floor(total_time) - - # a way to get a screen shot but it was too slow - # sct_img = sct.grab(screenDimensions) - # mss.tools.to_png(sct_img.rgb, sct_img.size, output="test.png") + last = int(total_time) # Get a screen shot sct_img = gui.screenshot(region=(left,top, width, height)) @@ -50,6 +56,12 @@ def getPixel(Image,x, y): # Get the color of the world background bgColor = getPixel(sct_img, 440, 30) + + if debug_flag: + sct_img.save("screenshot" + str(screenshot_counter) +".png") + screenshot_counter += 1 + + #Check for cacti - do a scan of the image low down for i in reversed(range(x_start, x_end)): # if i found a pixel in the search interval with a colour other than the bg colour, then it is an obstacle if getPixel(sct_img,i,y_search) != bgColor\ @@ -57,9 +69,14 @@ def getPixel(Image,x, y): keyboard.press(' ') # jump break + #check for birds - do a higher up scan of the images + #tba + + t2 = time.time()-t1 total_time += t2 # DEBUG - print(x_end) + if debug_flag: + print(x_end)