An automated chrome dino game Using PIL
and Pyautogui
with dark mode support.
All you need to know is in this article credits @ayushgemini
- To add 3 features
- Linux support
- Handle dark mode
- Automatic switch to chrome window
ImageGrab
is not supported in linux, its replacement is pyscreenshot and pyautogui
also has built-in screenshot method but both are slow and inconsistent.
mss fixes these issues, its both fast and cross-platform.
After score goes around 700 the game inverts the colour turning into dark mode and our logic breaks. I fixed it using random pixel on screen which is white in normal mode and turns dark in dark mode. Based on its colour two different functions are called. Function for dark mode is written by inversing the conditions. The chosen pixel should never come in way of dinosaur,bird or cactus otherwise it will satisfy conditions of dark mode (prefer pixel below the ground line of game)
In original code first chrome tab is opened then program is runned in terminal and then focus is switched to browser window, to avoide registering key presses in terminal time.sleep()
is used.
these steps are automated using webdrivers of selenium
- The game only uses keyboard inputs so initially I considered using keyboard library as its relatively small. but scrapped the idea later as this library needs root permissions for ubuntu and is still in beta phase.
- I also considered
OpenCv
but lot of online forums suggested usingPIL
for basic tasks as its lightweight and simple
- Install
python3.5+
(preferably 3.7). - Install pip3
- Download chrome driver.
- Install chrome driver. (for ubuntu)
- Run
pip3 install -r requirement.txt
- Run
python3 dino.py
-
pixel values for each screen may differ based on its resolution. so I would have to change pixel values in original code to make it work on my pc but then the code would not work on original authors pc.
-
Being beginner, I prefer doing everything from scratch, it helps me better understand the concepts
- Pixel cordinates start at (0,0) from top left corner of the screen, x coordinates increase towards right, y coordinates increase towards bottom
- wrong version of chromedriver gives
ChromeDriver error “unknown error: cannot get automation extension”
if this error occures update chromedriver(preferred) or downgrade chrome browser - In terms of grayscale image 255 is absolute white and 0 is absolute black color
- The code responds at constant speed while speed of game keeps on increasing, at one point the speed of game exceeds the response speed and dino collides. The highest score achived so far is 2113