-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimon.py
More file actions
57 lines (47 loc) · 1.66 KB
/
Simon.py
File metadata and controls
57 lines (47 loc) · 1.66 KB
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
import keyboard
from utils.mouse import record_buttons, click_pixel, get_mouse_position, set_mouse_position
from utils.screen import get_pixel_color
from win32api import Sleep
SLEEP_BETWEEN_COMPUTER = 600
SLEEP_BETWEEN_CLICKS = 1000
class Simon:
def __init__(self):
self.buttons = record_buttons()
self.base_colors = self.__get_base_colors(self.buttons)
self.moves = []
self.recording = True
keyboard.add_hotkey('p', callback=self.__switch__, args=(False,), suppress=True)
def __get_base_colors(self, buttons):
set_mouse_position((0, 0))
return [get_pixel_color(*pt) for pt in buttons]
def record_simon(self):
for i, pos in enumerate(self.buttons):
curr_color = get_pixel_color(*pos)
if self.base_colors[i] != curr_color:
self.moves.append(pos)
print(f"Recorded {i + 1} button")
Sleep(SLEEP_BETWEEN_COMPUTER)
def play_record(self):
start_position = get_mouse_position()
print(f"Executing moves: {self.moves}")
while self.moves:
mv = self.moves.pop(0)
Sleep(SLEEP_BETWEEN_CLICKS)
click_pixel(*mv)
set_mouse_position(start_position)
Sleep(SLEEP_BETWEEN_COMPUTER)
self.__switch__(True)
def __switch__(self, flag):
self.recording = flag
def play(self):
print("Press P to play recording")
print("Recording...")
loops = 0
while True:
if self.recording:
self.record_simon()
else:
self.play_record()
if __name__ == '__main__':
s = Simon()
s.play()