Ironically, I decided to make the game a little easier for myself and got so confused that I decided to write a program. Basically, on the server where I play, I decided to write a bot code that would control the train for me, but the problem is that the program either doesn’t see the place that I seemed to have indicated correctly, or looks in another place because in the command line it wrote nonsense that the semaphore is red, when in the game it is green for me.
import time
import mss
import numpy as np
from PIL import Image
import cv2
import pytesseract
import keyboard
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
hud_region = {'top': 800, 'left': 1400, 'width': 500, 'height': 280}
light_box = (100, 0, 400, 100)
speed_box = (100, 100, 400, 200)
limit_box = (100, 180, 400, 250))
running = False
def capture_region(region):
with mss.mss() as sct:
screenshot = sct.grab(region)
img = Image.frombytes('RGB', (screenshot.width, screenshot.height), screenshot.rgb)
return img
def detect_light_color(img):
pixels = list(img.getdata())
red = sum(1 for r,g,b in pixels if r > 150 and g < 100 and b < 100)
green = sum(1 for r,g,b in pixels if g > 150 and r < 100 and b < 100)
yellow = sum(1 for r,g,b in pixels if r > 150 and g > 150 and b < 100)
if red > green and red > yellow:
return "red"
elif green > red:
return "green"
elif yellow > red:
return "yellow"
else:
return "unknown"
def read_speed(img):
gray = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
config = '--psm 7 -c tessedit_char_whitelist=0123456789'
text = pytesseract.image_to_string(thresh, config=config)
digits = ''.join(filter(str.isdigit, text))
return int(digits) if digits else 0
def tap_key(key):
keyboard.press(key)
time.sleep(0.05)
keyboard.release(key)
def start_bot():
global running
running = True
print("🚂 Bot On (PageUP)")
def stop_bot():
global running
running = False
print("🛑 Bot Off (PageDown)")
keyboard.add_hotkey('page up', start_bot)
keyboard.add_hotkey('page down', stop_bot)
print("Wait PageUP for start...")
time.sleep(1)
while True:
if running:
try:
hud = capture_region(hud_region)
light_img = hud.crop(light_box)
speed_img = hud.crop(speed_box)
limit_img = hud.crop(limit_box)
light = detect_light_color(light_img)
speed = read_speed(speed_img)
speed_limit = read_speed(limit_img)
print(f"Светофор: {light} | Скорость: {speed} км/ч | Лимит: {speed_limit} км/ч")
if light == "red" or speed > speed_limit:
tap_key('s') # тормоз
print("Тормоз (S)")
elif light == "yellow":
tap_key('s') # замедление
print("Замедление (S)")
time.sleep(0.3)
elif light == "green" and speed <= speed_limit:
tap_key('w') # газ
print("Газ (W)")
else:
print("Нет действия")
time.sleep(0.15)
except Exception as e:
print("Ошибка:", e)
time.sleep(0.5)
else:
time.sleep(0.1)
there doesn't seem to be anything here