-
Notifications
You must be signed in to change notification settings - Fork 2
/
cards.py
72 lines (57 loc) · 2 KB
/
cards.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
from pynput.mouse import Button, Controller
import time
import pyautogui
from PIL import Image
import imagehash
mouse = Controller()
class Card:
def __init__(self, pos, first_card_coords, delta, is_first_of_pair):
self.img_width = 55
self.img_height = 55
self.pos = pos
self.coords = (first_card_coords[0] + delta[0] * pos[0],
first_card_coords[1] + delta[1] * pos[1])
self.is_first_of_pair = is_first_of_pair
self.matched = False
def flip(self):
if self.is_first_of_pair:
time.sleep(1)
mouse.position = self.coords
mouse.click(Button.left, 2)
def flip_and_save(self):
self.flip()
time.sleep(0.5)
self.save_img()
def save_img(self):
img = pyautogui.screenshot()
self.img = img.crop(
(self.coords[0] - self.img_width / 2, self.coords[1] - self.img_height / 2,
self.coords[0] + self.img_width / 2, self.coords[1] + self.img_height / 2))
def generate_cards(cards, first_card_coords, game_dim, delta):
is_first_of_pair = True
for x in range(game_dim[0]):
for y in range(game_dim[1]):
card = Card((x, y), first_card_coords, delta, is_first_of_pair)
cards.append(card)
is_first_of_pair = not is_first_of_pair
time.sleep(1)
def images_difference(img1, img2):
return abs(imagehash.average_hash(img1) - imagehash.average_hash(img2))
def sort_comp(x):
return x[0]
def clc_diffs(diffs, cards):
for x in range(len(cards)):
for y in range(x + 1, len(cards)):
diffs.append(
(images_difference(cards[x].img, cards[y].img), cards[x], cards[y]))
diffs.sort(key=sort_comp)
def match(cards, diffs):
time.sleep(1)
for x in diffs:
if x[1].matched or x[2].matched:
continue
x[1].matched = x[2].matched = True
x[1].is_first_of_pair = True
x[2].is_first_of_pair = False
x[1].flip()
x[2].flip()