Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm loving this library. I'm having a ton of fun messing around with it.

Here's a script to play basketball at http://www.onlinegames.com/basketball

I'm having trouble getting calculate_shot_pos right given the ball pos and basketball pos. My trig is pretty rusty, so if anyone wants to help me out (even with some strategy) I would love to hear it! I've learned the library, so getting it to actually play the game well is just a bonus.

Anyway, here goes:

    from pydroid import mouse, bitmap
    from math import floor, sqrt
    import time

    rim_color = 15474
    ball_color = 10440243
    basket_pos = (143, 365)

    def is_ball_on_screen(screen):
        return len(screen.find_every_color(ball_color)) > 0 and find_ball_pos(screen)[0] > basket_pos[0]

    def is_basket_on_screen(screen):
        return screen.get_color(basket_pos[0], basket_pos[1]) == rim_color

    def average(xs):
        return int(sum(xs, 0.0) / len(xs))

    def find_ball_pos(screen): 
        coords = screen.find_every_color(ball_color)
        xs = [coord[0] for coord in coords]
        ys = [coord[1] for coord in coords]
        return (average(xs), average(ys))

    def calculate_shot_pos(basket, ball):
        x = basket[0] + (ball[0] - basket[0]) / 3.0
        y = basket[1] - (ball[1] - basket[1])
        return (int(x), int(y))

    while(True):
        screen = bitmap.capture_screen()
        if is_basket_on_screen(screen) and is_ball_on_screen(screen):
            ball_pos = find_ball_pos(screen)
            shot_pos = calculate_shot_pos(basket_pos, ball_pos)
            mouse.move(shot_pos[0], shot_pos[1])
            time.sleep(.5)
            mouse.click()
        time.sleep(1)


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: