-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
152 lines (129 loc) · 5.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pyautogui
import pygetwindow
import pydirectinput
import time
import random
import mss
import numpy as np
from PIL import Image
import gc
import win32api
import win32con
import bettercam
import cv2
def main():
"""
Main function for the program
"""
# Set to True to see the live feed
visuals = False
# Time to wait between each note
deplayBetweenNotes = 0.4
quitKey = "Q"
# Finds all Windows with the title "New World"
newWorldWindows = pygetwindow.getWindowsWithTitle("New World")
# TODO - Fix this, cause it could choose the wrong window
# Find the Window titled exactly "New World" (typically the actual game)
for window in newWorldWindows:
if window.title == "New World":
newWorldWindow = window
break
# Select that Window
newWorldWindow.activate()
# Move your mouse to the center of the game window
centerW = newWorldWindow.left + (newWorldWindow.width/2)
centerH = newWorldWindow.top + (newWorldWindow.height/2)
print(centerH, centerW)
# pyautogui.moveTo(centerW, centerH)
# win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE, int(centerW), int(centerH), 0, 0)
win32api.SetCursorPos((int(centerW), int(centerH)))
# Clicky Clicky
time.sleep(0.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(0.01)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
time.sleep(0.5)
# TODO - newWorldWindow.top keeps giving a negative number, so I'm just going to use 0 for now
region = (
newWorldWindow.left + round(newWorldWindow.width/3) - 50,
newWorldWindow.top + 800,
newWorldWindow.left + (round(newWorldWindow.width/3)) + 50,
newWorldWindow.top + newWorldWindow.height - 100
)
camera = bettercam.create(region=region, output_color="BGRA", max_buffer_len=512)
camera.start(target_fps=120, video_mode=True)
# This should resolve issues with the first cast being short
time.sleep(2)
while win32api.GetAsyncKeyState(ord(quitKey)) == 0:
time.sleep(deplayBetweenNotes)
npImg = np.array(camera.get_latest_frame())
sctImg = Image.fromarray(npImg)
if visuals:
cv2.imshow('Live Feed', npImg)
if (cv2.waitKey(1) & 0xFF) == ord(quitKey):
exit()
try:
if pyautogui.locate("imgs/click.PNG", sctImg, grayscale=True, confidence=.60) is not None:
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(0.01)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
time.sleep(0.01)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
time.sleep(0.01)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)
print("Clicked")
continue
except Exception as e:
pass
try:
if pyautogui.locate("imgs/a.PNG", sctImg, grayscale=True, confidence=.60) is not None:
win32api.keybd_event('A', 0,0,0) # Press the 'a' key (key down)
time.sleep(0.02)
win32api.keybd_event('A',0 ,win32con.KEYEVENTF_KEYUP ,0) # Release the 'a' key (key up)
print("a")
continue
except Exception as e:
pass
try:
if pyautogui.locate("imgs/s.PNG", sctImg, grayscale=True, confidence=.60) is not None:
win32api.keybd_event(ord('S'), 0,0,0) # Press the 'a' key (key down)
time.sleep(0.02)
win32api.keybd_event(ord('S'),0 ,win32con.KEYEVENTF_KEYUP ,0) # Release the 'a' key (key up)
print("s")
continue
except Exception as e:
pass
try:
if pyautogui.locate("imgs/d.PNG", sctImg, grayscale=True, confidence=.60) is not None:
win32api.keybd_event(ord('D'), 0,0,0) # Press the 'a' key (key down)
time.sleep(0.01)
win32api.keybd_event(ord('D'),0 ,win32con.KEYEVENTF_KEYUP ,0) # Release the 'a' key (key up)
print("d")
continue
except Exception as e:
pass
try:
if pyautogui.locate("imgs/w.PNG", sctImg, grayscale=True, confidence=.60) is not None:
win32api.keybd_event(ord('W'), 0,0,0) # Press the 'a' key (key down)
time.sleep(0.01)
win32api.keybd_event(ord('W'),0 ,win32con.KEYEVENTF_KEYUP ,0) # Release the 'a' key (key up)
print("w")
continue
except Exception as e:
pass
try:
if pyautogui.locate("imgs/space.PNG", sctImg, grayscale=True, confidence=.60) is not None:
win32api.keybd_event(ord(' '), 0,0,0) # Press the 'a' key (key down)
time.sleep(0.01)
win32api.keybd_event(ord(' '),0 ,win32con.KEYEVENTF_KEYUP ,0) # Release the 'a' key (key up)
print("space")
continue
except Exception as e:
pass
# Runs the main function
if __name__ == '__main__':
try:
main()
except Exception as e:
import traceback
traceback.print_exc()