-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
76 lines (56 loc) · 2.32 KB
/
main.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
from playwright.sync_api import sync_playwright
from PIL import Image
import io
import numpy as np
import cv2
import time
with sync_playwright() as p:
navegador = p.firefox.launch(headless=False)
pagina = navegador.new_page()
pagina.goto('https://chromedino.com/')
pagina.press('//*[@id="t"]', ' ')
# TAKE A SCREENSHOT FROM DE COMPONENT IN HTML
img = Image.open(io.BytesIO(
pagina.locator('.runner-canvas').screenshot()))
# GETTING THE IMAGE SIZE
width, height = img.size
time_start = time.time()
distance_to_jump = 30
while True:
# TAKE A SCREENSHOT FROM DE COMPONENT IN HTML
img = Image.open(io.BytesIO(
pagina.locator('.runner-canvas').screenshot()))
# CROPPING IMAGE IN THE DISTANCE FOR DINO JUMP
image = img.crop((width//8-5, 0, width*3//8-distance_to_jump, height))
# CROPPING IMAGE TO VERIFY THE GAME OVER
end = img.crop((width//4+30, 25, width-width//4-30, height//2))
# CONVERT THE SCREENSHOT PIXELS TO NUMPY ARRAY FOR CV2 IMAGE AND CONVERT ITS COLOR TO RGB
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
end = cv2.cvtColor(np.array(end), cv2.COLOR_RGB2BGR)
# CONSIDERING AS BLACK ANY PIXEL BELOW 100
black_pixels = np.sum(image < 100)
black_pixels_end = np.sum(end < 100)
# CONSIDERING AS BLACK ANY PIXEL ABOVE 100
#white_pixels = np.sum(image > 100)
print('Number of black pixels = ', black_pixels)
#print('Number of white pixels = ', white_pixels)
# FOR LIGHY MODE
if black_pixels > 1000:
pagina.press('//*[@id="t"]', ' ')
# FOR DARK MODE
'''if white_pixels > 1000:
pagina.press('//*[@id="t"]', ' ')'''
# RESTART THE GAME AFTER LOSE
if black_pixels_end >= 1752:
pagina.press('//*[@id="t"]', ' ')
distance_to_jump = 30
time_start = time.time()
# OPENNING THE CV SCREEN
cv2.imshow('image', image)
# FUNCTION TO UPDATE THE CV SCREEN
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
# FUNCTION TO INCREASE THE JUMP AS TIME PASSES
if (time.time() - time_start) >= 10:
distance_to_jump -= 7
time_start = time.time()