Needs to pick someones brain

Asked by xyz_User on 2019-09-19

Started a new gaming project and I'm faced with a small problem which I can't think of a clever function.
The issue is the health bar of characters, they're always green in color (i know sikuli probably doesn't see colors). When the character looses health the green color depletes from the health bar

Now these health bars can change positions as the character moves up or down, this is no problem because I can use region.
How can I make sure sikuli clicks the bar with the lowest amount of Health or the colour green? The health bars can also change in shape for eg: The one at the very top can look the the 3rd one... Really stumped :S

This is what the health bars look like:
https://i.imgur.com/jhOB1IG.png

Any suggestion is greatly appreciated!

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
xyz_User
Solved:
2019-09-21
Last query:
2019-09-21
Last reply:
2019-09-21
RaiMan (raimund-hocke) said : #1

You might use the Location.getColor() feature.

a sample with the most left bar in your image:
switchApp("opera")
img = Pattern("img.png").targetOffset(-24,1)
patR = Pattern("img.png").targetOffset(21,1)
offsetR = patR.getTargetOffset()
hover(img)
start = Mouse.at()
end = start.offset(offsetR.x * 2, 0)
for n in range(offsetR.x * 2 + 10):
    print start.offset(n,0).getColor().getGreen()

To get specific points in an image (as here the left side middle and the right side middle of the bar) you can use the image Preview feature in the IDE.
There are about 40 pixels to check.
If you use the check-the-middle algorithm, you would have your result latest with 6 checks in a loop:
- test the middle of the bar
- if it is black, test the middle of the left half of the bar
- if it is not black, test the middle of the right half of the bar
- ... hope you got it ;-)

xyz_User (userseven) said : #2

Not on a PC atm going to check as soon as I get home, thanks RaiMan. As always, very prompt in your response!

xyz_User (userseven) said : #3

Just got a chance to try this, I think i'm missing the point and require some more explanation. If you have the time, can you show me the function for this scenerio:

Let's say I'm the healer character in the game, and I need to scan who has the lowest amount of health comparing all of those 4 health bars and then click it.

xyz_User (userseven) said : #4

The last sentence I should have said comparing who has the least of "GREEN" health bar

RaiMan (raimund-hocke) said : #5

ok, but might take till somewhen tomorrow.

xyz_User (userseven) said : #6

thanks very much! This will be amazing

RaiMan (raimund-hocke) said : #7

ok, this is all I can do for you - other priorities.

Based on the given image shown in browser Opera
The offsets have been manually evaluated in IDE Preview.
Not all bar images in your shot show the left end, so I decided to evaluate right to left.

def init():
    switchApp("opera")
    pat1R = Pattern("img.png").targetOffset(20,1)
    pat1L = Pattern("img.png").targetOffset(-23,1)
    offsetR = pat1R.getTargetOffset()
    offsetL = pat1L.getTargetOffset()
    length = offsetR.x - offsetL.x
    hover(pat1R)
    start1 = Mouse.at()
    pat2 = Pattern("img.png").targetOffset(47,-41)
    start2 = start1.offset(pat2.getTargetOffset()).offset(-offsetR.x -1, 0)
    pat3 = Pattern("img.png").targetOffset(82,11)
    start3 = start1.offset(pat3.getTargetOffset()).offset(-offsetR.x -1, 0)
    pat4 = Pattern("img.png").targetOffset(94,-19)
    start4 = start1.offset(pat4.getTargetOffset()).offset(-offsetR.x -1, 0)
    return (length, start1, start2, start3, start4)

def evalHealth(start):
    for n in range(length + 1):
        color = start.offset(-n,0).getColor().getGreen()
        if color > 50: break
    return 100 - int(n * 100.0/length)

length, start1, start2, start3, start4 = init()
print evalHealth(start1)
hover(start2)
print evalHealth(start2)
hover(start3)
print evalHealth(start3)
hover(start4)
print evalHealth(start4)

xyz_User (userseven) said : #8

THANK YOU!! This did it!!