0% found this document useful (0 votes)
7 views3 pages

Imadethis

The document contains a Python script that automates gameplay for a match-3 game using libraries like PyAutoGUI and OpenCV. It captures the screen, identifies blocks, finds matches, and simulates moves based on detected patterns. The program runs in a loop until a right-click is detected, at which point it stops execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Imadethis

The document contains a Python script that automates gameplay for a match-3 game using libraries like PyAutoGUI and OpenCV. It captures the screen, identifies blocks, finds matches, and simulates moves based on detected patterns. The program runs in a loop until a right-click is detected, at which point it stops execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pyautogui

import cv2
import numpy as np
import time
import threading
import pygetwindow as gw
import random # Importing the random module for random delays

# Global variable to monitor if the program should stop


running = True

# Function to stop the program when the user right-clicks


def monitor_right_click():
global running
while running:
if pyautogui.mouseInfo().button == 'right':
running = False
print("Right-click detected, stopping the bot.")
break

# Function to capture the screen and process it using OpenCV


def capture_screen(region=None):
screenshot = pyautogui.screenshot(region=region)
frame = np.array(screenshot)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
return frame

# Function to identify the grid and the blocks by their shape


def identify_blocks(frame):
grid = []
height, width, _ = frame.shape
cell_width = width // 8
cell_height = height // 8

# Convert the frame to grayscale for easier shape detection


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)

# Find contours of shapes (blocks)


contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

block_shapes = []

for contour in contours:


# Ignore small contours that are not blocks
if cv2.contourArea(contour) < 500:
continue

# Approximate the contour to a polygon to identify its shape


epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)

if len(approx) == 4: # Assuming each block is a square (4 sides)


block_shapes.append(approx)

# Create a grid with block positions based on contours


for i in range(8):
row = []
for j in range(8):
# Assign the block shape to the grid
for block in block_shapes:
# Find the approximate center of the block and assign it to the
grid
M = cv2.moments(block)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])

# Map the center of the block to the corresponding grid position


if (i * cell_width < cx < (i + 1) * cell_width) and (j *
cell_height < cy < (j + 1) * cell_height):
row.append((cx, cy)) # Store the center coordinates of the
block
break
grid.append(row)

return grid

# Function to find possible matches in the grid (horizontal and vertical)


def find_matches(grid):
matches = []
# Check horizontal matches
for i in range(8):
for j in range(6): # Limit to last 3 columns for match-3
if grid[i][j] == grid[i][j + 1] == grid[i][j + 2]:
matches.append(((i, j), (i, j + 2))) # Match found from j to j+2

# Check vertical matches


for i in range(6): # Limit to last 3 rows for match-3
for j in range(8):
if grid[i][j] == grid[i + 1][j] == grid[i + 2][j]:
matches.append(((i, j), (i + 2, j))) # Match found from i to i+2

return matches

# Function to calculate the screen coordinates of the blocks


def get_block_coordinates():
screen_width, screen_height = pyautogui.size()
grid_top_left = (screen_width // 4, screen_height // 4) # Assuming the grid
starts at a certain position
return grid_top_left

# Function to simulate a move by clicking on two blocks


def simulate_move(start, end):
grid_top_left = get_block_coordinates()
start_x, start_y = start
end_x, end_y = end

start_pos = (grid_top_left[0] + start_y * 50, grid_top_left[1] + start_x * 50)


# 50px for each cell
end_pos = (grid_top_left[0] + end_y * 50, grid_top_left[1] + end_x * 50)

# Perform the move by clicking on the start and end positions


pyautogui.click(start_pos[0], start_pos[1])
time.sleep(0.2)
pyautogui.click(end_pos[0], end_pos[1])

# Adding a random delay between 2 and 3 seconds after each move


time.sleep(random.uniform(2, 3))

# Main loop to capture the screen, process the grid, and make moves
def main():
global running
monitor_thread = threading.Thread(target=monitor_right_click)
monitor_thread.start()

while running:
frame = capture_screen(region=(100, 100, 800, 800)) # Adjust region based
on your game window
grid = identify_blocks(frame)
matches = find_matches(grid)

if matches:
# Make the first match move
match = matches[0]
simulate_move(match[0], match[1])

# Adding a random delay between 2 and 3 seconds before making the next move
time.sleep(random.uniform(2, 3))

monitor_thread.join()

if __name__ == "__main__":
main()

You might also like