From 6f44817a1f500e86168932ba7eb95518fe55a292 Mon Sep 17 00:00:00 2001
From: anima
Date: Tue, 21 Apr 2026 19:22:47 +0200
Subject: [PATCH] add function to check position and find all matches on screen
---
source/command.py | 27 +++++++++++++++++++++++++--
source/window.py | 23 +++++++++++++++--------
2 files changed, 40 insertions(+), 10 deletions(-)
diff --git a/source/command.py b/source/command.py
index 08efa3c..160fcfe 100644
--- a/source/command.py
+++ b/source/command.py
@@ -16,7 +16,7 @@ except ImportError as e:
class Command:
"""Dataclass for commands"""
__AUTHOR__ = 'anima'
- __VERSION__ = '1.0.0'
+ __VERSION__ = '1.1.3'
image_dir: Path = Path('images/')
supported_extensions = {".png", ".jpg", ".jpeg", ".bmp"}
@@ -63,4 +63,27 @@ class Command:
return self.valid
- def check_position(self, )
\ No newline at end of file
+ def check_position(self, position: tuple) -> bool:
+ """check if image on expected position
+
+ Args:
+ position (tuple): expected x, y position
+
+ Returns:
+ bool: True if image on expected position
+ """
+ if self.position is None:
+ logging.debug('no expected position set, skip check')
+ return True
+ if not isinstance(position, tuple):
+ logging.debug(f'position check get wrong value {position=} ({type(position)})')
+ return False
+ i_x, i_y = self.position
+ p_x, p_y = position
+
+ if (i_x - self.pos_dif) < int(p_x) < (i_x + self.pos_dif) and \
+ (i_y - self.pos_dif) < int(p_y) < (i_y + self.pos_dif):
+ return True
+ else:
+ logging.debug(f'image is not on expected {position=}')
+ return False
\ No newline at end of file
diff --git a/source/window.py b/source/window.py
index b38ef12..125f6b4 100644
--- a/source/window.py
+++ b/source/window.py
@@ -21,7 +21,7 @@ from .command import Command
class Window:
__AUTHOR__ = 'anima'
- __VERSION__ = '1.0.0'
+ __VERSION__ = '1.1.6'
key_press_duration = 0.1
supported_keys = ['return', 'Up', 'Down', 'Right', 'Left']
@@ -95,13 +95,20 @@ class Window:
img_gray = cv2.cvtColor(command.image, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(screen_gray, img_gray, cv2.TM_CCOEFF_NORMED)
- _, match_value, _, match_pos = cv2.minMaxLoc(result)
-
- if match_value >= command.threshold and match_value > best_value:
- logging.debug(f'new best match: {command.name} ({match_value=}%, {match_pos=})')
- best_match = command
- best_value = match_value
- best_pos = match_pos
+
+ locations = numpy.where(result >= command.threshold)
+ matches = list(zip(*locations[::-1])) # [(x1,y1), (x2,y2), ...]
+
+ for match in matches:
+ if command.check_position(match):
+ print('match!')
+ match_value = float(result[match[1], match[0]])
+ if match_value > best_value:
+ best_match = command
+ best_value = match_value
+ best_pos = match
+ logging.debug(f'new best match: {command.name} ({match_value=}%, {match=})')
+ break
if best_match:
return best_match