← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #227548]: How do I get Sikuli to click randomly?

 

Question #227548 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/227548

    Status: Open => Answered

RaiMan proposed the following answer:
Of course, I can answer your question ;-)

--- so that sikuli will only click on a certain color

In the current version you have to step down to the Java Robot, which
has a function getPixelColor() (the new version will have a
Location.getColorAt(x.y) ).

the basics:
import java.awt.Robot as JR
import java.awt.Color as JC
print JR().getPixelColor(getCenter().x, getCenter().y) == JC.RED
print JR().getPixelColor(getCenter().x, getCenter().y) == JC.WHITE
print JR().getPixelColor(getCenter().x, getCenter().y) == JC(255,255,255)

To get more info, you have to look into the respective JavaDocs of Robot
and Color.

with the snippet from comment #1 (assuming to only want pure red
points):

import java.awt.Robot as JR
import java.awt.Color as JC
import random
rob = JC()
col = JC.RED
r = someRegion
for i in range(100): # click randomly 100 times
    locx = int(r.w * random.random())
    locy = int(r.h * random.random())
    while not rob.getPixelColor(locx, locy) == col:
        locx = int(r.w * random.random())
        locy = int(r.h * random.random())
    click(Location(locx, locy))
    wait(0.3) # optional: some time for the GUI to react

BeAware: if less then 100 red points will ever be found, this will loop
endless.

--- so that sikuli will only click on a certain pattern
this only makes sense if you want to say:
click only if in a certain region around the currently selected point the pattern (= image) is present

If this is what you want, here you are:

import random
r = someRegion
w = 50 # width and
h = 50 # heigth around point to check for pattern
p = "some-image.png"

for i in range(100): # click randomly 100 times
    locx = int(r.w * random.random())
    locy = int(r.h * random.random())
    while not Region(locx-w/2, locy-w/2, w, h).exists(p, 0):
        locx = int(r.w * random.random())
        locy = int(r.h * random.random())
    click(Location(locx, locy))
    wait(0.3) # optional: some time for the GUI to react

Same here with looping endless: if the pattern is never found.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.