Untitled

 avatar
unknown
plain_text
10 months ago
6.6 kB
16
Indexable
import pyautogui
import datetime
import keyboard
import time
from PIL import Image
import random
import math

# Colors (in hex, converted to RGB tuples)
JUMP_COLOR = (60, 0, 200)  # Confirmed jump color (3C00C8)
COIN_COLOR = (255, 0, 255)  # Pink coin color (FF00FF)

# Color tolerance for pixelated areas
COLOR_TOLERANCE = 40  # ±40 for each RGB channel to ensure detection

# Screen resolution (1440p)
SCREEN_WIDTH, SCREEN_HEIGHT = 2560, 1440  # Updated for 2560x1440
# Define regions based on 1440p (scaled coordinates)
COIN_SEARCH_REGION = (933, 533, 800, 533)  # Central area for coins (left, top, width, height)
TREE_SEARCH_REGION = (640, 360, 1280, 720)  # Reduced to central 1280x720 area for jumps

# Human-like mouse movement settings
pyautogui.MINIMUM_DURATION = 0.01  # Reduced for faster response
pyautogui.MINIMUM_SLEEP = 0.01   # Reduced for faster response
pyautogui.PAUSE = 0.05          # Reduced pause after each action

# Time thresholds (in seconds)
JUMP_TIMEOUT = 6         # Time to wait before retrying jump click

def human_move_to(x, y, fast=False):
    """Move mouse to (x, y) with minimal human-like randomness and varied speed."""
    duration = 0.001 if fast else random.uniform(0.05, 0.15)
    pyautogui.moveTo(x, y, duration=duration)
    time.sleep(0.001 if fast else random.uniform(0.04, 0.2))

def human_click_random(x=None, y=None, fast=False):
    """Ihmismäisempi klikkaus isommalla satunnaisuudella ja logituksella."""
    if x is None or y is None:
        pos = pyautogui.position()
        x, y = pos.x, pos.y

    if fast:
        dx = random.randint(-3, 3)  # Minimal randomness for JUMP_COLOR
        dy = random.randint(-3, 3)
        x_final = x + dx
        y_final = y + dy
    else:
        dx = random.randint(-12, 12)
        dy = random.randint(-10, 1)
        x_final = x + dx
        y_final = y + dy

        if random.random() < 0.3:
            mid_x = x_final + random.randint(-5, 5)
            mid_y = y_final + random.randint(-5, 5)
            human_move_to(mid_x, mid_y, fast=fast)
            time.sleep(random.uniform(0.05, 0.2))

    human_move_to(x_final, y_final, fast=fast)

    pyautogui.mouseDown()
    time.sleep(0.001 if fast else random.uniform(0.05, 0.35))
    pyautogui.mouseUp()

    time.sleep(0.001 if fast else random.uniform(0.1, 1.0))

    ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"[{ts}] [CLICK] Klikattu kohtaan ({x_final},{y_final})")

def is_color_within_tolerance(pixel, target_color, tolerance):
    return all(abs(pixel[i] - target_color[i]) <= tolerance for i in range(3))

def find_color_center(color, region=None):
    screenshot = pyautogui.screenshot(region=region)
    img = screenshot
    matching_pixels = []

    for x in range(img.width):
        for y in range(img.height):
            pixel = img.getpixel((x, y))
            if is_color_within_tolerance(pixel, color, COLOR_TOLERANCE):
                matching_pixels.append((x, y))

    if not matching_pixels:
        return None

    avg_x = sum(x for x, _ in matching_pixels) // len(matching_pixels)
    avg_y = sum(y for _, y in matching_pixels) // len(matching_pixels)

    if region:
        return avg_x + region[0], avg_y + region[1]
    return avg_x, avg_y

def verify_color_at_position(color, x, y, region=None):
    if region:
        rel_x, rel_y = x - region[0], y - region[1]
        if 0 <= rel_x < region[2] and 0 <= rel_y < region[3]:
            screenshot = pyautogui.screenshot(region=region)
            pixel = screenshot.getpixel((rel_x, rel_y))
            return is_color_within_tolerance(pixel, color, COLOR_TOLERANCE)
    else:
        pixel = pyautogui.pixel(x, y)
        return is_color_within_tolerance(pixel, color, COLOR_TOLERANCE)
    return False

def main():
    print("Starting agility bot. Press 'q' to quit.")
    time.sleep(3)

    last_jump_time = time.time()
    last_jump_pos = None
    failed_coin_attempts = 0

    while not keyboard.is_pressed('q'):
        current_time = time.time()

        # --- Coin detection ---
        coin_pos = find_color_center(COIN_COLOR, region=COIN_SEARCH_REGION)
        if coin_pos:
            if failed_coin_attempts < 2:
                if verify_color_at_position(COIN_COLOR, coin_pos[0], coin_pos[1], COIN_SEARCH_REGION):
                    human_move_to(coin_pos[0], coin_pos[1])
                    human_click_random()
                time.sleep(random.uniform(5, 7))
                post_coin_pos = find_color_center(COIN_COLOR, region=COIN_SEARCH_REGION)
                if post_coin_pos:
                    failed_coin_attempts += 1
                    print(f"Coin still present after click, attempts: {failed_coin_attempts}")
                else:
                    failed_coin_attempts = 0
                    print("Coin collected successfully")
                last_jump_time = time.time()
                time.sleep(random.uniform(0.5, 1.5))  # Normal delay after coin
                continue

            else:
                failed_coin_attempts = 0

        # --- Jump detection ---
        search_region = TREE_SEARCH_REGION if last_jump_pos else None
        jump_pos = find_color_center(JUMP_COLOR, region=search_region)

        if jump_pos:
            if verify_color_at_position(JUMP_COLOR, jump_pos[0], jump_pos[1], TREE_SEARCH_REGION):
                human_move_to(jump_pos[0], jump_pos[1], fast=True)
                human_click_random(fast=True)
            click_time = time.time()
            while find_color_center(JUMP_COLOR, region=search_region):
                if time.time() - click_time >= JUMP_TIMEOUT:
                    jump_pos = find_color_center(JUMP_COLOR, region=search_region)
                    if jump_pos:
                        human_move_to(jump_pos[0], jump_pos[1], fast=True)
                        human_click_random(fast=True)
                    click_time = time.time()
                time.sleep(0.01)  # Minimal delay in jump loop
                if keyboard.is_pressed('q'):
                    break
            last_jump_time = time.time()
            last_jump_pos = jump_pos
            time.sleep(0.01)  # Minimal delay after jump
            continue

        time.sleep(random.uniform(0.05, 0.2))  # Main loop delay

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("Bot stopped by user.")
    except Exception as e:
        print(f"An error occurred: {e}")
Editor is loading...
Leave a Comment