Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simon Says Bot
"""
Python Bot Player
iann0036 2014
http://www.miniclip.com/games/simon-says/en/#t-c-f-C
"""
import ImageGrab
import os
import time
import win32api, win32con
import ImageOps
def imageSnap():
global im
global box
im = ImageGrab.grab(box)
def testPixel(x,y,r,g,b):
#im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) + '.png', 'PNG')
global im
rgb = im.getpixel((x,y))
return rgb == (r,g,b)
def leftClickGame(x,y):
win32api.SetCursorPos((x + x_offset, y + y_offset))
time.sleep(.2)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.2)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
win32api.SetCursorPos((0,0))
def setGameOffset():
global x_offset
global y_offset
global box
box = ()
imageSnap()
x = 50
y = 550
while not im.getpixel((x,y))==(0,0,0):
x+=1
while im.getpixel((x,y))==(0,0,0):
y-=1
x_offset = x
y_offset = y
box = (x_offset,y_offset,x_offset+498,y_offset+397)
print "Offset detected as " + str(x) + "," + str(y)
def main():
setGameOffset()
time.sleep(2)
leftClickGame(250,305) # start game
time.sleep(1)
num_colors = 1
while True:
colors = []
i = 0
redflag = False
blueflag = False
greenflag = False
yellowflag = False
while i<num_colors:
imageSnap()
red = testPixel(168,82,141,64,64)
blue = testPixel(161,209,43,65,133)
green = testPixel(335,86,88,188,88)
yellow = testPixel(338,205,179,179,93)
if red:
redflag = False
else:
if not redflag:
redflag = True
print "Red"
colors.append(0)
i+=1
if blue:
blueflag = False
else:
if not blueflag:
blueflag = True
print "Blue"
colors.append(1)
i+=1
if green:
greenflag = False
else:
if not greenflag:
greenflag = True
print "Green"
colors.append(2)
i+=1
if yellow:
yellowflag = False
else:
if not yellowflag:
yellowflag = True
print "Yellow"
colors.append(3)
i+=1
print "---"
time.sleep(1)
for color in colors:
if color==0:
leftClickGame(168,82)
print "Replay Red"
if color==1:
leftClickGame(161,209)
print "Replay Blue"
if color==2:
leftClickGame(335,86)
print "Replay Green"
if color==3:
leftClickGame(338,205)
print "Replay Yellow"
time.sleep(.6)
print "---"
num_colors+=1
print "Your turn!"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment