Proper classes

This commit is contained in:
2020-11-08 15:37:01 +01:00
parent de91c6a9cc
commit 5b742c9b5d
7 changed files with 163 additions and 8 deletions

7
aoe_bot.py Normal file
View File

@@ -0,0 +1,7 @@
import aoe_commands
import aoe_start_routine
import aoe_recognition
aoe_recognition.wait_for_start()
aoe_start_routine.run_start_routine()
aoe_recognition.collect_screenshots()

17
aoe_commands.py Normal file
View File

@@ -0,0 +1,17 @@
from pyautogui import *
import pyautogui
import keyboard
WAIT4CLICK = 0.04
def order_peasants(number):
print(f"Trying to order {number} peasants.")
select_town_center()
for i in range(number):
pyautogui.press('q')
time.sleep(WAIT4CLICK)
def select_town_center():
pyautogui.press('h')
time.sleep(WAIT4CLICK)

36
aoe_recognition.py Normal file
View File

@@ -0,0 +1,36 @@
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
import time
from PIL import Image, ImageDraw
import aoe_commands
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 collect_screenshots():
cnt = 0
while True: #keyboard.is_pressed('q') == False:
time.sleep(10)
pic = pyautogui.screenshot()
pic.save(f"image_{cnt}.png")
cnt += 1
def initial_scout_trace():
aoe_commands.select_town_center()
# TODO

65
aoe_start_routine.py Normal file
View File

@@ -0,0 +1,65 @@
from pyautogui import *
import pyautogui
import keyboard
WAIT4CLICK = 0.04
import aoe_commands
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)
def run_start_routine():
aoe_commands.order_peasants(4)
assign_hotkeys()
build_houses()
pyautogui.press('1')
pyautogui.press('1')

BIN
images/startscreen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 913 KiB

35
readme.md Normal file
View File

@@ -0,0 +1,35 @@
AoE Bot
=======
Preparation
-----------
- Install python > 3.8
- Install these libraries from an administrator terminal (windows):
- pip install pywin32
- pip install keyboard
- pip install pyautogui
- pip install opencv-python
Start
------
run aoe_bot.py before starting the game. It will wait for the startscreen and do some standard actions when the game has started:
- order 4 villagers
- assign hotkeys: 1: scout, 2-4: villagers
- build first house with villagers 1+2
- build second house with villager 3
- jump to scout
After this it will collect screenshots during the game to train data
Interesting Links
------------------
- [Tutorial video + notes](https://github.com/KianBrose/Image-Recognition-Botting-Tutorial/blob/master/README.txt)
- [Pyautogui Doc](https://pyautogui.readthedocs.io/en/latest/keyboard.html)
- [PIL doc](https://pillow.readthedocs.io/en/stable/reference/Image.html)
- [Draw Image api](https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html)
- [Draw Image Example](http://effbot.org/imagingbook/imagedraw.htm)
- [AoE Hotykey editor](https://aokhotkeys.appspot.com/editor)
- [Structureing your python project](https://python-docs.readthedocs.io/en/latest/writing/structure.html)

11
test.py
View File

@@ -7,7 +7,7 @@ import win32api, win32con
import time
from PIL import Image, ImageDraw
WAIT4CLICK = 0.04
# see https://github.com/KianBrose/Image-Recognition-Botting-Tutorial/blob/master/README.txt
#def click(x,y):
@@ -62,6 +62,7 @@ def wait_for_start():
def zoomout():
"Tut nicht"
time.sleep(0.5)
print("Zooming out")
pyautogui.click(x=500, y=500)
@@ -81,13 +82,7 @@ def find_peasants():
print(f"Für Bild {img_path} wurden {len(matches)} matches gefunden: {matches}")
return all_matches
def order_peasants(number):
print(f"Trying to order {number} peasants.")
pyautogui.press('h')
time.sleep(WAIT4CLICK)
for i in range(number):
pyautogui.press('q')
time.sleep(WAIT4CLICK)
def assign_hotkeys():
pyautogui.press(',')