Files
AoEBot/test.py
2020-11-08 15:37:01 +01:00

158 lines
4.0 KiB
Python

from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
import time
from PIL import Image, ImageDraw
# see https://github.com/KianBrose/Image-Recognition-Botting-Tutorial/blob/master/README.txt
#def click(x,y):
# win32api.SetCursorPos((x,y))
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
# Positionen
# Resourcen Bereich 60px hoch, 850px breit
# Spielerfarben:
# b = 255 -> Blau
# r = 0 & rest nicht 0 -> cyan
def merge_objects(objects):
for pos1 in range(len(objects)-1, 0 , -1):
for pos2 in range(len(objects)-1, 0 , -1):
if pos1 == pos2:
continue
t1, l1, b1, r1 = objects[pos1]
t2, l2, b2, r2 = objects[pos2]
if ((t1 <= t2 and t1 >= b2) and (l1 >= l2 and l1 <= r2)) or ((b1 >= t2 and b1 <= b2) and (r1 >= l2 and r1 <= r2)):
t = min(t1,t2)
l = min(l1,l2)
b = max(b1, b2)
r = max(r1, r2)
objects[pos2] = (t,l,b,r)
objects.remove(objects[pos1])
break
return objects
def image_files_in_folder(folder):
return [os.path.join(folder, f) for f in os.listdir(folder) if re.match(r'.*\.(jpg|jpeg|png)', f, flags=re.I)]
def wait_for_start():
start_started = False
while 1:
if pyautogui.locateOnScreen('images/startscreen.png', confidence=0.9) != None:
print("Startscreen found")
start_started = True
time.sleep(0.5)
elif start_started:
print("Proceeding")
return
else:
print("Startscreen not found")
time.sleep(0.5)
def zoomout():
"Tut nicht"
time.sleep(0.5)
print("Zooming out")
pyautogui.click(x=500, y=500)
time.sleep(WAIT4CLICK)
while keyboard.is_pressed('q') == False:
pyautogui.scroll(-10)
time.sleep(WAIT4CLICK)
print("Zooming out done")
def find_peasants():
print("Looking for peasants")
all_matches = []
for img_path in image_files_in_folder(os.path.join("images", "peasants")):
matches = list(pyautogui.locateAllOnScreen(img_path, confidence=0.8))
if len(matches) > 0:
all_matches += matches
print(f"Für Bild {img_path} wurden {len(matches)} matches gefunden: {matches}")
return all_matches
def assign_hotkeys():
pyautogui.press(',')
time.sleep(WAIT4CLICK)
pyautogui.hotkey('ctrl', '1')
time.sleep(WAIT4CLICK)
pyautogui.press('.')
time.sleep(WAIT4CLICK)
pyautogui.hotkey('ctrl', '2')
time.sleep(WAIT4CLICK)
pyautogui.press('.')
time.sleep(WAIT4CLICK)
pyautogui.hotkey('ctrl', '3')
time.sleep(WAIT4CLICK)
pyautogui.press('.')
time.sleep(WAIT4CLICK)
pyautogui.hotkey('ctrl', '4')
time.sleep(WAIT4CLICK)
def build_houses():
pyautogui.press('h')
time.sleep(WAIT4CLICK)
pyautogui.press('up')
time.sleep(0.5)
pyautogui.press('2')
time.sleep(WAIT4CLICK)
pyautogui.press('q')
time.sleep(WAIT4CLICK)
pyautogui.press('q')
time.sleep(WAIT4CLICK)
pyautogui.click(650, 90)
time.sleep(WAIT4CLICK)
pyautogui.press('3')
time.sleep(WAIT4CLICK)
pyautogui.rightClick(650, 90)
time.sleep(WAIT4CLICK)
pyautogui.press('4')
time.sleep(WAIT4CLICK)
pyautogui.press('q')
time.sleep(WAIT4CLICK)
pyautogui.press('q')
time.sleep(WAIT4CLICK)
pyautogui.click(300, 200)
time.sleep(WAIT4CLICK)
wait_for_start()
#zoomout() FUUU
order_peasants(4)
assign_hotkeys()
build_houses()
pyautogui.press('1')
pyautogui.press('1')
cnt = 0
while False: #keyboard.is_pressed('q') == False:
time.sleep(1)
peasant_locations = merge_objects(find_peasants())
pic = pyautogui.screenshot()
draw = ImageDraw.Draw(pic)
for x,y,a,b in peasant_locations:
draw.rectangle((x,y,x+a,y+b), outline="#ff0000")
pic.save(f"image_{cnt}.png")
cnt += 1