How to Build a Python Bot That Can Play Web Games
In this tutorial we'll going to show the mechanism of developing a Python program that will play a simple flash game on line.
Here a helpful tutorial for learning how to build a Python bot that can play web games and tools needed :
Our Game's Name:
Apple Shooter
This is the Game Link.
Game Description:
it's consist of a man holding archer and shooting an apple holds on another man head which stands steps away from the archer man.
to win you must hit the apple by the arrow.
How to play :
first you must determine the angle of shooting ,second you must press lift click bottom on the right place and holds for second to determine the appropriate power you need to shoot the arrow with , and then release bottom
Game first overview:
Playing overview :
Power bar:
Angel bar:
Algorithm for the program :
the challenge in this game how to determine the angel and the power needed to hit the apple ,because the distance is increase in every level and it effect the speed an the accuracy of the arrow.
it could be don by tow ways:
first and simplest way is based on analyzing and expectation by mad a constant step for the angel every level.
that can be done by changing the mouse position -as shown in helpful tutorial- at every level by the same number of pixels by pass the mouse position parameters increasing every iteration in loop .
Code ex:
i=0
while true:
mousePos(cords[0]+i*5,cords[1]+i*9)
i++
to press left click use this function:
def leftClick():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
but we want to press down and holds -to full power tab- then we must press left down and holds for seconds and then release bottom.
that can be done by using leftdown function and then sleep .
function :
def leftDown():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
function to start game:
def start_game() :
mousePos((375 , 287))
time.sleep(.5)
leftClick()
function to play game:
def play() :
cords=(603 ,229)
i=0
while i<8 :
#screenGrab()
time.sleep(1)
hit_apple((cords[0]+i*5,cords[1]+i*9))
time.sleep(1)
next_level()
i+=1
cords=(603 ,229)
i=0
while i<8 :
#screenGrab()
time.sleep(1)
hit_apple((cords[0]+i*5,cords[1]+i*9))
time.sleep(1)
next_level()
i+=1
it calls function hit_apple which change mouse position by passing constant step increasing parameters.
def hit_apple(cords) :
mousePos((cords [0] , cords[1]))
time.sleep(.5)
leftDown()
time.sleep(1)
leftUp()
Second way based projectiles low :
distance=speed*cosθ*time.
box = (x_pad,y_pad,x_pad+725,y_pad+430)
im = ImageGrab.grab(box)
im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) +
'.png', 'PNG')
def get_color() :
box = (590,200,650,360)
im = ImageGrab.grab(box)
color=im.getcolors ()
and by making the angel constant every level . we need to calculate the speed as
speed=distance/(cosθ*time).
now we can determine the power need to hit the apple.
final code:-code not complete suitable for second way ,need some development-mousePos((cords [0] , cords[1]))
time.sleep(.5)
leftDown()
time.sleep(1)
leftUp()
Second way based projectiles low :
distance=speed*cosθ*time.
we should calculate the distance every level by determine the apple
position and the archer man position using function screen grab to
capture an image and get color function to determine the positions.
def screenGrab():box = (x_pad,y_pad,x_pad+725,y_pad+430)
im = ImageGrab.grab(box)
im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) +
'.png', 'PNG')
box = (590,200,650,360)
im = ImageGrab.grab(box)
color=im.getcolors ()
and by making the angel constant every level . we need to calculate the speed as
speed=distance/(cosθ*time).
now we can determine the power need to hit the apple.
import win32api, win32con
import ImageGrab
import os
import time
"""
All coordinates assume a screen resolution of 1280x1024, and Chrome
maximized with the Bookmarks Toolbar enabled.
Down key has been hit 6 times to center play area in browser.
x_pad = 220
y_pad = 130
Play area = x_pad+1, y_pad+1, 975, 560
"""
x_pad = 220
y_pad = 130
def screenGrab():
box = (x_pad,y_pad,x_pad+725,y_pad+430)
im = ImageGrab.grab(box)
im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) +
'.png', 'PNG')
def leftClick():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
def leftDown():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
def leftUp():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
time.sleep(.1)
def mousePos(cord):
win32api.SetCursorPos((x_pad + cord[0], y_pad + cord[1]))
def get_cords():
x,y = win32api.GetCursorPos()
x = x - x_pad
y = y - y_pad
print x,y
def start_game() :
mousePos((375 , 287))
time.sleep(.5)
leftClick()
def next_level() :
mousePos((372 ,169))
time.sleep(.5)
leftClick()
def hit_apple(cords) :
mousePos((cords [0] , cords[1]))
time.sleep(.5)
leftDown()
time.sleep(1)
leftUp()
def play() :
cords=(603 ,229)
i=0
while i<8 :
#screenGrab()
time.sleep(1)
hit_apple((cords[0]+i*5,cords[1]+i*9))
time.sleep(1)
next_level()
i+=1
while i>8 :
screenGrab()
time.sleep(1)
hit_apple((cords[0]+i,cords[1]+i*2))
time.sleep(1)
next_level()
i+=1
def get_color() :
box = (590,200,650,360)
im = ImageGrab.grab(box)
color=im.getcolors ()
#print color
#im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) +
# '.png', 'PNG')
def main():
# screenGrab()
start_game()
if __name__ == '__main__':
main()




