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

Debug flag / efficiency increases #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
107 changes: 107 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
37 changes: 27 additions & 10 deletions bot.py
Original file line number Diff line number Diff line change
@@ -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]


Expand All @@ -21,45 +29,54 @@ 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()
if keyboard.is_pressed('q'): # Emergency Button
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))

# 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\
or getPixel(sct_img,i,y_search2) != bgColor:
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)