def performAction( yp, rc, bc, perform):
if perform :
cursor[0] = 4*(yp[0]-110)
cursor[1] = 4*(yp[1]-120)
if yp[0]>110 and yp[0]<590 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo(cursor[0],cursor[1])
elif yp[0]<110 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo( 8 , cursor[1])
elif yp[0]>590 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo(1912, cursor[1])
elif yp[0]>110 and yp[0]<590 and yp[1]<120:
pyautogui.moveTo(cursor[0] , 8)
elif yp[0]>110 and yp[0]<590 and yp[1]>390:
pyautogui.moveTo(cursor[0] , 1072)
elif yp[0]<110 and yp[1]<120:
pyautogui.moveTo(8, 8)
elif yp[0]<110 and yp[1]>390:
pyautogui.moveTo(8, 1072)
elif yp[0]>590 and yp[1]>390:
pyautogui.moveTo(1912, 1072)
else:
pyautogui.moveTo(1912, 8)
if rc[0]!=-1 and bc[0]!=-1:
if abs(rc[0]-bc[0])<35 and abs(rc[1]-bc[1])<20:
pyautogui.click(button = 'left')
python类click()的实例源码
def setCursorPos( yc, pyp):
yp = np.zeros(2)
if abs(yc[0]-pyp[0])<5 and abs(yc[1]-pyp[1])<5:
yp[0] = yc[0] + .7*(pyp[0]-yc[0])
yp[1] = yc[1] + .7*(pyp[1]-yc[1])
else:
yp[0] = yc[0] + .1*(pyp[0]-yc[0])
yp[1] = yc[1] + .1*(pyp[1]-yc[1])
return yp
# Depending upon the relative positions of the three centroids, this function chooses whether
# the user desires free movement of cursor, left click, right click or dragging
def setCursorPos( yc, pyp):
yp = np.zeros(2)
if abs(yc[0]-pyp[0])<5 and abs(yc[1]-pyp[1])<5:
yp[0] = yc[0] + .7*(pyp[0]-yc[0])
yp[1] = yc[1] + .7*(pyp[1]-yc[1])
else:
yp[0] = yc[0] + .1*(pyp[0]-yc[0])
yp[1] = yc[1] + .1*(pyp[1]-yc[1])
return yp
# Depending upon the relative positions of the three centroids, this function chooses whether
# the user desires free movement of cursor, left click, right click or dragging
def chooseAction(yp, rc, bc):
out = np.array(['move', 'false'])
if rc[0]!=-1 and bc[0]!=-1:
if distance(yp,rc)<50 and distance(yp,bc)<50 and distance(rc,bc)<50 :
out[0] = 'drag'
out[1] = 'true'
# print 'dragging'
return out
elif distance(rc,bc)<40:
out[0] = 'left'
# print 'left click'
return out
elif distance(yp,rc)<40:
out[0] = 'right'
# print 'right click'
return out
else:
# print 'moving'
return out
else:
out[0] = -1
return out
# Movement of cursor on screen, left click, right click and dragging actions are performed here
def setCursorPos( yc, pyp):
yp = np.zeros(2)
if abs(yc[0]-pyp[0])<5 and abs(yc[1]-pyp[1])<5:
yp[0] = yc[0] + .7*(pyp[0]-yc[0])
yp[1] = yc[1] + .7*(pyp[1]-yc[1])
else:
yp[0] = yc[0] + .1*(pyp[0]-yc[0])
yp[1] = yc[1] + .1*(pyp[1]-yc[1])
return yp
# Depending upon the relative positions of the three centroids, this function chooses whether
# the user desires free movement of cursor, left click, right click or dragging
def chooseAction(yp, rc, bc):
out = np.array(['move', 'false'])
if rc[0]!=-1 and bc[0]!=-1:
if distance(yp,rc)<50 and distance(yp,bc)<50 and distance(rc,bc)<50 :
out[0] = 'drag'
out[1] = 'true'
return out
elif distance(rc,bc)<40:
out[0] = 'left'
return out
elif distance(yp,rc)<40:
out[0] = 'right'
return out
elif distance(yp,rc)>40 and rc[1]-bc[1]>120:
out[0] = 'down'
return out
elif bc[1]-rc[1]>110:
out[0] = 'up'
return out
else:
return out
else:
out[0] = -1
return out
# Movement of cursor on screen, left click, right click,scroll up, scroll down
# and dragging actions are performed here based on value stored in 'action'.
def setCursorPos( yc, pyp):
yp = np.zeros(2)
if abs(yc[0]-pyp[0])<5 and abs(yc[1]-pyp[1])<5:
yp[0] = yc[0] + .7*(pyp[0]-yc[0])
yp[1] = yc[1] + .7*(pyp[1]-yc[1])
else:
yp[0] = yc[0] + .1*(pyp[0]-yc[0])
yp[1] = yc[1] + .1*(pyp[1]-yc[1])
return yp
# Depending upon the relative positions of the three centroids, this function chooses whether
# the user desires free movement of cursor, left click, right click or dragging
def behaviour2() :
pyautogui.click (500, 200)
pyautogui.dragRel (100, 500, duration = 0.27)
pyautogui.doubleclick()
pyautogui.scroll (600)
pyautogui.rightclick()
def get_game_landscape_and_set_focus_or_die(screenshotter, threshold=0.7) -> Dict:
landscape = find_game_position(screenshotter, threshold)
if not landscape:
print("Can't find the game!")
exit(1)
pyautogui.click(landscape['left'], landscape['top'] + landscape['height'])
return landscape
def on_key_press(self, widget, event):
keyname = Gdk.keyval_name(event.keyval)
if event.state & Gdk.ModifierType.CONTROL_MASK and keyname == 'Return' or keyname == 'Return':
self.copy_to_clipboard()
self.destroy(self, widget)
p = pyautogui.position()
pyautogui.click(p)
pyautogui.hotkey('ctrl', 'v')
if keyname == 'Escape':
Gtk.main_quit()
def go(self):
run_counter = 0
#lets you get mouse over dota
while run_counter < self.max_runs:
run_counter += 1
self.run = Run(self.db.get_num_results() + 1)
print("Hi")
time.sleep(2.5)
self.run.start_game()
self.run.set_logs() # Shouldnt matter that this occurs after game launch. I only care about logs around pull
self.run.follow_bot()
self.run.wait_for_pull() # ASYNCIO time?
self.run.dump_console()
self.run.restart()
self.run.delay(pa.click, 282, 748, delay_secs=3) # click the skip button
# At this point the bot script sends the new result to database
new_result = self.run.read_log()
self.db.add_run(self.run.id, new_result)
time.sleep(0.01) # this is only because I expected game to send result to elastic search. not python
result = Result(self.db.get_run(self.run.id))
self.neural_net.add_result(result)
self.neural_net.iterate_weights_2(100)
# print(self.neural_net.weights)
logger.info(self.neural_net)
self.neural_net.update_params()
#self.run.read_log() # TODO this bit can be async whilst we are starting the next game
def launch_game(cls):
logger.info("Launching Game")
cls.delay(cls.click_pic, 'playdota.png')
cls.delay(cls.click_pic, 'createlobby.png')
cls.delay(cls.click_pic, 'startgame.png')
cls.delay(pa.click, 282, 748, delay_secs=10)
cls.delay(PressKey, 0x1C, delay_secs=8)
cls.delay(ReleaseKey, 0x1C)
cls.delay(pa.typewrite, "-startgame")
cls.delay(PressKey, 0x1C)
cls.delay(ReleaseKey, 0x1C)
def follow_bot(cls):
logger.info("Clicking bot portrait to follow bot")
# TODO have these coords depend on what slot bot is in
pa.click(744, 103)
pa.click(730, 927, clicks=2)
def click_pic(cls, picname):
coords = cls.get_coords_pic(picname)
if not coords:
raise Exception("Could not find button")
pa.moveTo(coords)
time.sleep(2)
pa.click(coords)
def presskey_1(self):
x = pyautogui.position()
pyautogui.click(x)
pyautogui.press("1", interval=0.1)
def presskey_2(self):
x = pyautogui.position()
pyautogui.click(x)
pyautogui.press("2", interval=0.1)
def presskey_3(self):
x = pyautogui.position()
pyautogui.click(x)
pyautogui.press("3", interval=0.1)
def presskey_3(self):
x = pyautogui.position()
pyautogui.click(x)
pyautogui.press("4", interval=0.1)
#num keys only for now
def presskey_q(self):
x = pyautogui.position()
pyautogui.click(x)
pyautogui.press("q", interval=0.1)
def presskey_e(self):
x = pyautogui.position()
pyautogui.click(x)
pyautogui.press("e", interval=0.1)