forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame1.py
163 lines (125 loc) · 4.27 KB
/
game1.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
import pyautogui
import time
# 模拟按键按下后释放
def simulate_key_press(key):
pyautogui.keyDown(key)
time.sleep(0.1)
pyautogui.keyUp(key)
# 模拟按键按下后释放,传入字符串数组
def simulate_keys_press(keys, re=1):
"""模拟按键按下后释放,传入字符串数组
Args:
keys (list): 按键数组
re (int, optional): 按键个数. Defaults to 1.
"""
num = 0
# 模拟按下释放按键
for key in keys:
# 限制触发的次数
if num >= re:
break
pyautogui.keyDown(key)
time.sleep(0.1)
pyautogui.keyUp(key)
num = num + 1
# 模拟鼠标点击
def simulate_mouse_press(x=0, y=0, button="left"):
# 模拟鼠标点击
pyautogui.click(x=x, y=y, button=button)
# 解析字符串,模拟按键/鼠标按压
def parse_key_and_simulate_key_mouse_press(key):
# 删除数组中不需要的其他字符串
# def remove_needless(keys):
# for i in range(len(keys)):
# if keys[i] not in ['1', '2', 're']:
# keys.pop(i)
# return keys
# keys = remove_needless(keys)
if key not in ['1', '2', 're']:
return
if key == '1':
key = 'w'
simulate_key_press(key)
elif key == '2':
key = 'up'
simulate_key_press(key)
elif key == 're':
# 根据实际情况设置坐标值
x = 1076
y = 771
simulate_mouse_press(x, y, 'left')
time.sleep(1)
x = 1311
y = 951
simulate_mouse_press(x, y, 'left')
# 解析字符串数组,根据字符串第一位判断是否需要转换按键后,按压按键
def parse_keys_and_simulate_keys_press(keys, re=1):
# print(f"keys={keys}")
# 删除数组中非 w a s d 1 2 3 的其他字符串
def remove_needless(keys):
for i in range(len(keys)):
if keys[i] not in ['w', 'a', 's', 'd', '1']:
keys.pop(i)
return keys
if isinstance(keys, list) and len(keys) > 0:
if keys[0] == '1':
keys = keys[1:]
keys = remove_needless(keys)
# 遍历数组,将123改为yui
for i in range(len(keys)):
if keys[i] == '1':
keys[i] = 'f'
# elif keys[i] == '2':
# keys[i] = 'u'
# elif keys[i] == '3':
# keys[i] = 'i'
elif keys[0] == '2':
keys = keys[1:]
keys = remove_needless(keys)
# 遍历数组,将wsad改为上下左右,123改为789
for i in range(len(keys)):
if keys[i] == 'w':
keys[i] = 'up'
elif keys[i] == 's':
keys[i] = 'down'
elif keys[i] == 'a':
keys[i] = 'left'
elif keys[i] == 'd':
keys[i] = 'right'
elif keys[i] == '1':
keys[i] = 'l'
# elif keys[i] == '2':
# keys[i] = '8'
# elif keys[i] == '3':
# keys[i] = '9'
elif keys[0] == 're':
# 鼠标按压的坐标,请手动重新校准坐标以适配
x = 1097
y = 779
simulate_mouse_press(x, y)
time.sleep(1)
x = 1314
y = 957
simulate_mouse_press(x, y)
return
simulate_keys_press(keys, re)
if __name__ == '__main__':
# 测试游戏:醉酒拔河 https://www.4399.com/flash/221542_1.htm
# 循环获取鼠标当前坐标
def get_mouse_pos():
# 定时获取鼠标坐标的时间间隔(秒)
interval = 1
try:
while True:
# 获取鼠标当前的坐标
x, y = pyautogui.position()
# 打印坐标信息
print(f"当前鼠标坐标:x={x}, y={y}")
# 等待一段时间后再次获取坐标
time.sleep(interval)
except KeyboardInterrupt:
print("获取鼠标坐标的程序已结束。")
get_mouse_pos()
# game1 = Game1()
# time.sleep(5)
# game1.parse_key_and_simulate_key_mouse_press('re')