Skip to content

Instantly share code, notes, and snippets.

Created August 11, 2012 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3325733 to your computer and use it in GitHub Desktop.
Save anonymous/3325733 to your computer and use it in GitHub Desktop.
import os
import ImageGrab
import Image
import time
import win32api, win32con
VK_CODE = {'left_arrow':0x25,
'spacebar':0x20,
'right_arrow':0x27}
def pressd(x,delay):
win32api.keybd_event(VK_CODE[x], 0,0,0)
time.sleep(delay)
win32api.keybd_event(VK_CODE[x],0 ,win32con.KEYEVENTF_KEYUP ,0)
def get_coord2(im):
ymaxp,ymax,ymin =0,0, 1000
rmax,rmin,rp = None,None,None
D = im.getdata()
k = 0
for c in D:
x = k % im.size[0]
y = k // im.size[0]
if c ==(0,0,0):
if y > ymax:
rmax = x,y
ymax = y
if y < ymin:
rmin = x,y
ymin = y
if c == (42,71,123):
if y > ymaxp:
rp = x,y
ymaxp = y
k+=1
return rmax,rmin,rp
def get_ball(posm,posmax,posmin):
if not posm:
return None
ym,ymax,ymin=posm[1],posmax[1],posmin[1]
if abs(ymin -ymax) < 2:
return None
if abs(ymin - ym) > abs(ymax - ym):
return posmin
else:
return posmax
def main():
time.sleep(2)
posm = None
minprev,maxprev=0,0
while True:
im=ImageGrab.grab(((850,240,1220,450)))
posmax,posmin,posp = get_coord2(im)
if posp and not posm and posmax == maxprev and posmin==minprev:
posm = posmax
posb = get_ball(posm,posmax,posmin)
print posp, posmax,posmin, posb, posm
time.sleep(0)
if posb :
win32api.SetCursorPos((850 +posb [0] ,240 + posb[1]))
if posb [1] < posm[1]:
pressd("spacebar",0.0)
maxprev,minprev=posmax,posmin
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment