Python Forum
Improving code for automated piano tiles
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improving code for automated piano tiles
#1
I just spent 2.5 hours straight on this and it works very well. I got the inspiration from a youtuber names Code Bullet. Anyways it could use a few improvements. Here is a list of stuff to be improved:
- In detect I find out if columnCount is past a certain number. I was doing this because it was becoming off centered and clicking more to the right. Does anyone know why and how, I can fix it. This would really speed up the code.

- Compacting the while loop in main and the detect function to speed things up.

- Any other suggestions are accepted

Note: the timer class is to slowly increase the y offset for the mouse click to match up with the fast keys.
import time
import win32con, win32api, win32gui
from PIL import ImageGrab
import cv2
import keyboard
import threading

'''
Box - 362, 89, 929, 864

Note - Scroll down once

_, _, mousePos = win32gui.GetCursorInfo()
'''

columnDict = {'left' : (100, 0, 101, 775), 'left-middle' : (200, 0, 201, 775), 'right-middle' : (350, 0, 351, 775), 'right' : (500, 0, 501, 775)}

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

class timer():
    def __init__(self):
        self.time = 0


    def decrease(self):
        while True:
            time.sleep(2)
            self.time += 1

def detect(t):
    img = cv2.imread('MagicTiles.png')
    for row in img:
        columnCount = 0
        for column in row:
            columnCount += 1
            if column[0] == 0 and column[1] == 0 and column[2] == 0 or column[0] in range(10, 30):
                if keyboard.is_pressed('s'):
                    return False
                if columnCount >= 300:
                    columnCount -= 100
                if columnCount >= 200:
                    columnCount -= 50
                elif columnCount >= 100:
                    columnCount -= 25
                click(columnCount + 396, 350 + t.time)
                return True
    return True
    

def main():
    time.sleep(3)
    click(700, 500)
    looping = True
    t = timer()
    thread = threading.Thread(target=t.decrease)
    thread.start()
    while looping:
        ImageGrab.grab((396, 400, 896, 401)).save('MagicTiles.png', 'PNG')
        looping = detect(t)
        if keyboard.is_pressed('s'):
            looping = False
            


if __name__ == '__main__':
    main()
If you wanna see the 2.5 hour long video of me creating it - https://youtu.be/DNo451FWvKs - you probably won't click on this link...
One last thing. If you are reading this before it has been up for 50 minutes, the link probably doesn't work. In case you were wondering, yes the video is going to take almost an hour to upload.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020