Search code examples
pythonkeyboardbotspyautogui

How can I make my bot that plays piano tiles faster?


I scripted a bot to play an online version of piano tiles on the computer. It constantly checks the color of the pixels on my screen to see if there is a clickable tile, and if there is, then my bot performs a function to click that spot on the screen. Everything works just fine, but I want to speed up how fast my bot detects pixels and clicks those spots. Any help is appreciated. The code is below:

from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con


def click(x, y):
    win32api.SetCursorPos((x, y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
x_coord = 480
x = 0

while keyboard.is_pressed("q") == False:
    x = x + 1
    print(x)
    if pixel(481, x_coord)[0] == 17:
        click(481, x_coord)
    elif pixel(581, x_coord)[0] == 17:
        click(581, x_coord)
    elif pixel(681, x_coord)[0] == 17:
        click(681, x_coord)
    elif pixel(781, x_coord)[0] == 17:
        click(781, x_coord)

If you need more information regarding the code or the game, please comment and I will try my best to respond.


Solution

  • First of all you have to check for the reaction time. This should give you a better view to the problem. If it is too high like seconds, the problem is probably about how pixels detected.

    If the reaction time is good but you want to achieve more you can try other libraries. I couldn't find one right now but I am sure that you can find easily if you use the right keywords. I remember using one that was faster than pyautogui.