Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, June 25, 2012

Python: Glide, instead of move, mouse cursor from one point to another

I couldn't find a function in pywin32 to smoothly glide a pointer from one point to another, instead of simply "moving" the cursor by making it jump from its current position to a given position.  I needed a way to make the mouse sort of "glide" from point A to point B at a seemingly natural pace, so here's my solution:

import time
import win32api

MOUSE_SPEED = .4 #seconds

def mouse_glide_to(x,y):
    """Smooth glides mouse from current position to point x,y with default timing and speed"""
    x1,y1 = win32api.GetCursorPos()
    smooth_glide_mouse(x1,y1, x, y, MOUSE_SPEED)

def smooth_glide_mouse(x1,y1,x2,y2, t, intervals):
    """Smoothly glides mouse from x1,y1, to x2,y2 in time t using intervals amount of intervals"""
    distance_x = x2-x1
    distance_y = y2-y1
    for n in range(0, intervals+1):
        move_mouse(x1 + n * (distance_x/intervals), y1 + n * (distance_y/intervals))
        time.sleep(t*1.0/intervals)

def move_mouse(x, y):
    win32api.SetCursorPos((x,y))
mouse_glide_to(x,y) will move the cursor from its current position to point (x,y) in MOUSE_SPEED seconds. It works perfectly!

Saturday, June 23, 2012

Python: Calculating the average color of an area of an image (PIL)

Here's a snippet of code I whipped up in Python to calculate the the average color of a square shaped area of an image.  I used the Python Imaging Library (PIL) to load the image, so be sure to have it available if you're using this.

import Image

def get_average_color((x,y), n, image):
    """ Returns a 3-tuple containing the RGB value of the average color of the
    given square bounded area of length = n whose origin (top left corner) 
    is (x, y) in the given image"""

    r, g, b = 0, 0, 0
    count = 0
    for s in range(x, x+n+1):
        for t in range(y, y+n+1):
            pixlr, pixlg, pixlb = image[s, t]
            r += pixlr
            g += pixlg
            b += pixlb
            count += 1
    return ((r/count), (g/count), (b/count))

image = Image.open('test.png').load()
r, g, b = get_average_color((24,290), 50, image)
print r,g,b

This is great for detecting the color of an area of an animated and constantly changing game screen, where finding the color of a single pixel may not be accurate enough for your needs.

Monday, October 10, 2011

WorldWinner Big Money Bot written in Python

What WW used to look like before GSN bought them.

I generally scored between 60,000-120,000 points on WorldWinner's Big Money compete-for-cash game, which, to me, was pretty good.  That was until I decided to looked up some strategies for improving my skill in BM and instead stumbled upon some YouTube videos of players scoring upwards of 350,000 points. My highest score was 137,582, and the highest I've ever seen in my experience was around 180k. You can imagine I was pretty shocked that a score higher than 200,000 was even possible, let alone achievable without cheating.


I then realized how much I suck at this game.  Well, what do you do when you can't beat a game?  Try, try again?  Practice?  Nope.  You cheat.


A simple Google search for "worldwinner big money bot" led me to one of those shady infomercial style sites with obviously fake and cheesy testimonials.  The product's slogan ended with "MAKE MORE MONEY THAN YOU COULD EVER IMAGINE!!!"  Riiiiiight.

Other than that shady site, I couldn't find any mention elsewhere of a bot for Big Money.  I decided to make my own.

It works, but it doesn't strategize, ponder, or think ahead yet; so it will usually end the game with a score between 60k - 120k.  I wrote this a while ago and I'm too busy at the moment to implement this feature.  If anybody would like to improve upon this, feel free.  Be warned, however: I didn't think I'd be sharing this so it's poorly commented.

To start, simply download the script here.  It was written for the latest version of Python 2, but any version > 2.5 should work fine IIRC.  It requires the Python Imaging Library (PIL) and Python for Windows Extensions (pywin32).


Before you can run it, you'll need to modify the script to set some variables.  You'll need to change variables 'xbeg' and 'ybeg' to fit your resolution and browser.  You can take a screenshot and measure the distance in MS Paint.  Use the picture below as a guide.


Here's a video of the bot in action: