Ludo King Automation — Screen & Input Scripting
Automate Ludo King using Python screen capture, OpenCV template matching, and input simulation. Detect tokens, recognise dice results, and execute moves automatically on desktop or Android.
How Ludo King Automation Works
Ludo King automation captures the game screen, processes the image to detect board state, runs a decision algorithm, and simulates the appropriate touch or mouse input. The three core components are: screenshots via MSS, board state detection via OpenCV template matching, and input injection via PyAutoGUI or uiautomator2 for Android. The pipeline runs: capture → detect → decide → act → sleep.
For production use, skip screen capture entirely and connect directly to the Ludo API for structured JSON state without pixel-perfect detection.
Screen Capture and Region Configuration
Define the screen region covering the entire game board. On desktop, MSS provides the best performance. The region should include the dice area so the bot can confirm when the roll animation has completed before making decisions.
import mss, cv2, numpy as np, pyautogui, time
GAME_REGION = {"top": 100, "left": 200, "width": 600, "height": 900}
DICE_REGION = {"top": 80, "left": 400, "width": 80, "height": 80}
TOKEN_TEMPLATES = {
"red": "templates/red_token.png",
"green": "templates/green_token.png",
"blue": "templates/blue_token.png",
"yellow": "templates/yellow_token.png",
}
class ScreenCapture:
def __init__(self):
self.sct = mss.mss()
self.templates = {k: cv2.imread(v, cv2.IMREAD_COLOR)
for k, v in TOKEN_TEMPLATES.items()}
def capture_board(self) -> np.ndarray:
screenshot = self.sct.grab(GAME_REGION)
frame = np.array(screenshot)
return cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
def capture_dice(self) -> np.ndarray:
dice_img = self.sct.grab(DICE_REGION)
return cv2.cvtColor(np.array(dice_img), cv2.COLOR_BGRA2BGR)
def find_token(self, color: str, frame: np.ndarray) -> list:
template = self.templates.get(color)
if not template: return []
result = cv2.matchTemplate(frame, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= 0.8)
centres = []
for pt in zip(*loc[::-1]):
h, w = template.shape[:2]
centres.append((pt[0] + w // 2, pt[1] + h // 2))
return centres
Cross-Platform Input Controller
PyAutoGUI drives desktop input; uiautomator2 handles Android emulators via ADB. The abstraction
below lets you switch platforms with a single flag without changing any other code.
class InputController:
def __init__(self, platform: str = "desktop"):
self.platform = platform
if platform == "android":
import uiautomator2 as u2
self.u2 = u2.connect()
else:
pyautogui.PAUSE = 0.1
pyautogui.FAILSAFE = True
def tap(self, x: int, y: int, duration: float = 0.1) -> None:
if self.platform == "android":
self.u2.click(x, y)
else:
abs_x = x + GAME_REGION["left"]
abs_y = y + GAME_REGION["top"]
pyautogui.click(abs_x, abs_y, duration=duration)
def roll_dice(self) -> None:
dice_x = GAME_REGION["left"] + GAME_REGION["width"] // 2
dice_y = GAME_REGION["top"] + 50
self.tap(dice_x, dice_y)
Main Automation Loop
The loop polls the screen, waits for the correct turn, rolls the dice, identifies the best token, and executes the tap. A state flag prevents double-actions. Use the Ludo API for a more reliable approach that avoids computer vision entirely.
Frequently Asked Questions
uiautomator2 via ADB
(adb connect localhost:5555) to control BlueStacks or LDPlayer. Token templates must be
captured at the emulator's actual screen resolution.templates/. Test the
matchTemplate confidence threshold — values below 0.8 may produce false positives.Automate Ludo Games the Right Way
Skip screen capture entirely. Connect your bot to real Ludo games via the LudoKingAPI for structured state, reliable move submission, and full TOS compliance.