Загрузка данных
import cv2
import mss
import numpy as np
import pydirectinput
import time
print("ИИ-ШТУРМОВИК С СИСТЕМОЙ ОБХОДА СТЕН ЗАПУЩЕН! Жду входа в бой...")
# Размеры захвата экрана (Твои эталонные 650x370)
monitor = {"top": 0, "left": 0, "width": 650, "height": 370}
# ИДЕАЛЬНАЯ ЦВЕТОВАЯ ПАЛИТРА ИЗ ТВОИХ СКРИНШОТОВ (HSV)
lower_green = np.array([35, 50, 50])
upper_green = np.array([85, 255, 255])
lower_blue = np.array([90, 50, 50])
upper_blue = np.array([130, 255, 255])
lower_red1 = np.array([0, 50, 50])
upper_red1 = np.array([10, 255, 255])
lower_red2 = np.array([170, 50, 50])
upper_red2 = np.array([180, 255, 255])
ATTACK_JOYSTICK_X = 570
ATTACK_JOYSTICK_Y = 240
with mss.mss() as sct:
is_moving_forward = False
# Переменные для отслеживания застревания в стенах
last_me_y = 0
stuck_frames_count = 0
wall_avoid_mode = False
wall_avoid_start_time = 0
avoid_direction = 'a' # По умолчанию обходим влево
while True:
screenshot = sct.grab(monitor)
frame = np.array(screenshot)
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask_g = cv2.inRange(hsv, lower_green, upper_green)
mask_r1 = cv2.inRange(hsv, lower_red1, upper_red1)
mask_r2 = cv2.inRange(hsv, lower_red2, upper_red2)
mask_r = cv2.bitwise_or(mask_r1, mask_r2)
contours_g, _ = cv2.findContours(mask_g, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours_r, _ = cv2.findContours(mask_r, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
in_battle = False
if len(contours_g) > 0 or len(contours_r) > 0:
in_battle = True
if not in_battle:
if is_moving_forward:
pydirectinput.keyUp('w')
is_moving_forward = False
pydirectinput.keyUp('a')
pydirectinput.keyUp('d')
cv2.imshow("AI Vision - Waiting for Battle", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
continue
me_x, me_y = None, None
enemy_detected = False
enemy_x, enemy_y = None, None
screen_center_x = 650 // 2
screen_center_y = 370 // 2
closest_dist = 99999
# 1. Находим себя
for contour in contours_g:
if cv2.contourArea(contour) > 120:
x, y, w, h = cv2.boundingRect(contour)
if w > h * 2.5 and w < h * 5.5:
dist = abs(x + w//2 - screen_center_x) + abs(y + h//2 - screen_center_y)
if dist < closest_dist:
closest_dist = dist
me_x, me_y = x + w//2, y + h//2
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 2. Находим врага
closest_enemy_dist = 99999
for contour in contours_r:
if cv2.contourArea(contour) > 120:
x, y, w, h = cv2.boundingRect(contour)
if w > h * 2.5 and w < h * 5.5:
enemy_detected = True
ex, ey = x + w//2, y + h//2
if me_x is not None:
e_dist = np.sqrt((ex - me_x)**2 + (ey - me_y)**2)
if e_dist < closest_enemy_dist:
closest_enemy_dist = e_dist
enemy_x, enemy_y = ex, ey
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
# --- СИСТЕМА ДЕЙСТВИЙ И ОБХОДА ПРЕПЯТСТВИЙ ---
if enemy_detected and me_x is not None:
# ПРОВЕРКА НА ЗАСТРЕВАНИЕ В СТЕНЕ
# Если бот пытается идти вперед (W зажата), но его координата Y не меняется
if is_moving_forward and last_me_y is not None and abs(me_y - last_me_y) < 2:
stuck_frames_count += 1
else:
stuck_frames_count = 0
last_me_y = me_y
# Если мы стоим на месте больше 15 кадров подряд — значит перед нами стена!
if stuck_frames_count > 15 and not wall_avoid_mode:
print("⛔ СТЕНА НА ПУТИ! Включаю маневр обхода препятствия!")
wall_avoid_mode = True
wall_avoid_start_time = time.time()
# Выбираем случайное направление обхода: влево (A) или вправо (D)
avoid_direction = 'a' if (me_x > screen_center_x) else 'd'
pydirectinput.keyUp('w')
is_moving_forward = False
# РЕЖИМ ОБХОДА СТЕНЫ (активен 0.4 секунды)
if wall_avoid_mode:
if time.time() - wall_avoid_start_time < 0.4:
# Жмем кнопку шага вбок, чтобы выйти из-за стены
pydirectinput.keyDown(avoid_direction)
else:
# Время маневра вышло, отпускаем боковую кнопку и пробуем снова идти вперед
pydirectinput.keyUp(avoid_direction)
wall_avoid_mode = False
stuck_frames_count = 0
else:
# ОБЫЧНОЕ ДВИЖЕНИЕ ВПЕРЕД
if not is_moving_forward:
pydirectinput.keyDown('w')
is_moving_forward = True
# УМНОЕ КИБЕРСПОРТИВНОЕ ПРИЦЕЛИВАНИЕ (AIM)
dx = enemy_x - me_x
dy = enemy_y - me_y
distance = np.sqrt(dx**2 + dy**2)
if distance > 0:
aim_click_x = int(ATTACK_JOYSTICK_X + (dx / distance) * 45)
aim_click_y = int(ATTACK_JOYSTICK_Y + (dy / distance) * 45)
pydirectinput.moveTo(ATTACK_JOYSTICK_X, ATTACK_JOYSTICK_Y)
pydirectinput.mouseDown()
pydirectinput.moveTo(aim_click_x, aim_click_y)
pydirectinput.mouseUp()
pydirectinput.press('f')
pydirectinput.press('e')
else:
if is_moving_forward:
pydirectinput.keyUp('w')
is_moving_forward = False
pydirectinput.keyUp('a')
pydirectinput.keyUp('d')
cv2.imshow("AI Vision - Anti-Wall Mode", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
pydirectinput.keyUp('w')
pydirectinput.keyUp('a')
pydirectinput.keyUp('d')
break
cv2.destroyAllWindows()
print("Бот полностью остановлен.")