thatsyntaxerrorguy thatsyntaxerrorguy - 4 months ago 23
Python Question

python autopy problems/confusion

so im trying to make a bot script that when a certain hex color is on a certain pixel it will execute some code to move the mouse,click etc. and i have it to where it takes a screenshot every 1 second to the same png file and updates the png file's pic. i have the hex color for the pixel cords print to the console so i can see if its updating or not. it never updates it just stays the same. ive tried writing this script many ways and sadly i only have one version to show you but hopefully you will understand what i was trying to accomplish. im on python 2.7 btw. thank you all for your time!!!!

import autopy
from time import sleep

color_grabber = hex(autopy.bitmap.Bitmap.open("screen1.png").get_color(292,115))


def color_checker():
global color_grabber
color_grabber = color_grabber
return

def mouse_move_click():
autopy.mouse.smooth_move(433,320)
autopy.mouse.click()

def screen_grab():
autopy.bitmap.capture_screen().save("screen1.png")

def the_ifs(mouse_move_click):
if color_checker == "0xffcb05":
mouse_move_click()

while 1==1:
sleep(1)
screen_grab()
color_checker()
the_ifs(mouse_move_click)
print color_grabber

Answer

I believe your problem is how you're using color_grabber. Saying color_grabber = color_grabber does nothing. What's happening in your code is that when you run it, after the imports, you define the value of color_grabber to be the color in your image. Then your while loop executes and in that loop you call color_checker. This function brings in the variable color_grabber from the global namespace and then you set that variable equal to itself. You're not re-executing the command you used to define color_grabber in the first place. You're just storing the color value back into itself so clearly its not going to change.

You also have a problem in how you're calling your mouse_move_click function. You don't want to pass in the function name, as that isn't really necessary. However, you also performed the check color_checker == "0xffcb05" which was comparing your function (the function itself, not the returned value) to the hex code. That doesn't do you any good. You want to compare the color. The solution is to pass the color into the_ifs and use that color to compare to the hex code. I should note though that you don't need to make the_ifs into its own function. Just put that if statement in your while loop. I left it how you had it though.

What you want is something like this.

import autopy
from time import sleep

def color_checker():
    color_grabber = hex(autopy.bitmap.Bitmap.open("screen1.png").get_color(292,115))
    return color_grabber

def mouse_move_click():
    autopy.mouse.smooth_move(433,320)
    autopy.mouse.click()

def screen_grab():
    autopy.bitmap.capture_screen().save("screen1.png")  

def the_ifs(color):
    if color == "0xffcb05":
        mouse_move_click()

while 1==1:
    sleep(1)
    screen_grab()
    color = color_checker()
    the_ifs(color)
    print color

Note that I have not run this code myself so I can't guarantee it works, but I believe it should.

Comments