-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlyPython_1.py
84 lines (65 loc) · 2.21 KB
/
OnlyPython_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# importing above defined libraries to
# implement the functionalities
#from pillow import ImageGrab, ImageOps
from pynput.mouse import Button,Controller
import pyscreenshot as ImageGrab,ImageOps
import pyautogui
import time
import numpy as np
class cordinates():
# coordinates of replay button to start the game
replaybutton =(337, 249)
# this coordinates represent the top-right coordinates
# that will be used to define the front box
dinasaur = (151, 283 )
def restartGame():
# using pyautogui library, we are clicking on the
# replay button without any user interaction
pyautogui.click(cordinates.replaybutton)
# we will keep our Bot always down that
# will prevent him to get hit by bird
pyautogui.keyDown('down')
def press_space():
# releasing the Down Key
pyautogui.keyUp('down')
# pressing Space to overcome Bush
pyautogui.keyDown('space')
# so that Space Key will be recognized easily
time.sleep(0.05)
# printing the "Jump" statement on the
# terminal to see the current output
print("jump")
time.sleep(0.10)
# releasing the Space Key
pyautogui.keyUp('space')
# again pressing the Down Key to keep my Bot always down
pyautogui.keyDown('down')
def imageGrab():
# defining the coordinates of box in front of dinosaur
box = (cordinates.dinasaur[0]+70, cordinates.dinasaur[1],
cordinates.dinasaur[0]+160, cordinates.dinasaur[1]+2)
# grabbing all the pixels values in form of RGB tupples
image = ImageGrab.grab(box)
# converting RGB to Grayscale to
# make processing easy and result faster
grayImage = ImageOps.grayscale(image)
# using numpy to get sum of all grayscale pixels
a = np.array(grayImage.getcolors())
# returning the sum
print(a.sum())
return a.sum()
#mouse=Controller()
# function to restart the game
restartGame()
while True:
# 435 is the sum of white pixels values of box.
# You may get different value is you are taking bigger
# or smaller box than the box taken in this article.
# if value returned by "imageGrab" function is not equal to 435,
# it means either bird or bush is coming towards dinosaur
#print(pyautogui.position())
#print(mouse.position)
if(imageGrab()!= 435):
press_space()
# time to recognize the operation performed by above function
time.sleep(0.1)