-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
78 lines (63 loc) · 2.07 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
77
78
import sys
import time
import pyautogui
import pydirectinput
import glob
import pyscreeze
import PIL.Image
import pynput
class Monopoly:
running = False
cache: dict[str, PIL.Image.Image] = {}
def __init__(self, delay: float) -> None:
self.PrintBanner()
self.SetupKeyHandler()
while True:
self.LoopImages()
time.sleep(delay)
def PrintBanner(self) -> None:
print("Monopoly Go! Bot")
print()
print("Press F2 to toggle running.")
print()
def SetupKeyHandler(self) -> None:
def onKeyPress(key) -> None:
if key == pynput.keyboard.Key.f2:
self.running = not self.running
if self.running:
print("Started")
print()
else:
print("Stopped. Press F2 to start again.")
print()
pynput.keyboard.Listener(onKeyPress).start()
def LoopImages(self) -> None:
for path in sorted(glob.glob(pathname="*.png", root_dir="images")):
if not self.running:
return
if self.ProcessImage(path):
break
def LoadImage(self, path: str) -> PIL.Image.Image:
image = self.cache.get(path)
if image is None:
image = self.cache[path] = PIL.Image.open(f"images/{path}")
return image
def Find(self, image: PIL.Image.Image) -> pyscreeze.Point | None:
result = pyautogui.locateOnScreen(image, grayscale=True, confidence=0.75)
if result is None:
return None
return pyautogui.center(result)
def ProcessImage(self, path: str) -> bool:
image = self.LoadImage(path)
point = self.Find(image)
if point is None:
print(f"Scanning for {path}")
return False
print(f"Scanning for {path} -> ({point.x}, {point.y})")
pyautogui.moveTo(x=point.x, y=point.y, duration=0.2)
pydirectinput.click()
return True
try:
Monopoly(delay=0.1)
except KeyboardInterrupt:
sys.exit()