-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokemon_AI.py
184 lines (151 loc) · 3.61 KB
/
Pokemon_AI.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from PIL import ImageGrab
import keyboard
import random
import time
import sys
SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080
BATTLE_MENU_X = 1680
BATTLE_MENU_Y = 733
BATTLE_ACTIVE = (248, 248, 248)
HEALTH_X = 1290
HEALTH_Y = 613
used_keys = [
'w', 'a', 's', 'd', 'x', 'z', 'enter'
]
def get_img():
img = ImageGrab.grab(bbox=None, include_layered_windows=False, \
all_screens=False, xdisplay=None)
px = img.load()
return px
def check_state():
img = get_img()
state = "walk"
if img[BATTLE_MENU_X, BATTLE_MENU_Y] == BATTLE_ACTIVE:
health_px = img[HEALTH_X, HEALTH_Y]
if health_px[1] > 200:
state = "battle"
else:
state = "run"
return state
def battle():
# Exit out of any menu game is currently in
keyboard.press('x')
time.sleep(0.2)
release_all()
# Move to option
keyboard.press('w')
time.sleep(0.2)
keyboard.press('a')
time.sleep(0.2)
release_all()
keyboard.press('z')
time.sleep(0.2)
release_all()
move = random.randint(1, 4)
if move == 1:
keyboard.press('w')
time.sleep(0.2)
keyboard.press('a')
elif move == 2:
keyboard.press('w')
time.sleep(0.2)
keyboard.press('d')
elif move == 3:
keyboard.press('s')
time.sleep(0.2)
keyboard.press('a')
else:
keyboard.press('s')
time.sleep(0.2)
keyboard.press('d')
time.sleep(0.2)
release_all()
keyboard.press('z')
time.sleep(0.2)
release_all()
def switch_pokemon():
# Exit out of any menu game is currently in
keyboard.press('x')
time.sleep(0.2)
release_all()
# Move to option
keyboard.press('s')
time.sleep(0.2)
keyboard.press('a')
time.sleep(0.2)
release_all()
keyboard.press('z')
time.sleep(0.2)
release_all()
pokemon = random.randint(0, 5)
for i in range(pokemon):
keyboard.press('s')
time.sleep(0.2)
release_all()
time.sleep(0.2)
keyboard.press('z')
time.sleep(0.2)
release_all()
time.sleep(0.2)
keyboard.press('z')
time.sleep(0.2)
release_all()
def run():
# Exit out of any menu game is currently in
keyboard.press('x')
time.sleep(0.2)
release_all()
coin = random.randint(0, 9)
if coin > 0:
switch_pokemon()
else:
# Escape
# Move to option
keyboard.press('s')
time.sleep(0.2)
keyboard.press('d')
time.sleep(0.2)
release_all()
keyboard.press('z')
time.sleep(0.2)
release_all()
def walk():
randInt = random.randint(0, 360)
if(randInt < 100):
keyboard.press('z')
elif randInt < 150 :
keyboard.press('x')
elif randInt < 200 :
keyboard.press('w')
elif randInt < 250 :
keyboard.press('a')
elif randInt < 300 :
keyboard.press('s')
elif randInt < 350 :
keyboard.press('d')
else :
keyboard.press('enter')
time.sleep(0.2)
release_all()
def release_all():
for key in used_keys:
keyboard.release(key)
def main():
print('Press Space to begin')
keyboard.wait("space")
while (True):
if keyboard.is_pressed('Esc'):
print("Exiting...")
release_all()
sys.exit(0)
else:
state = check_state()
if state == "battle":
battle()
elif state == "run":
run()
else:
walk()
if __name__ == '__main__':
main()