如何解决如何在 Python 中扫描游戏中的每个图块?
所以我试图扫描游戏中的每个图块(“扫描”作为踏入图块并保存其坐标)。如果玩家撞到一个物体,那么它必须尝试向下一层并继续行走。我什至可以知道玩家面对的是哪张脸。
我使用 if gtype is not None:
if not curqry[pos]: # only initialize if empty
curqry[pos]["pos"] = ""
curqry[pos]["type"] = "group"
curqry[pos]["val"] = gtype
curqry[pos]["queryGroup"] = []
是因为 pydirectinput
对我的游戏并不真正有效。
这是我目前的代码:
pyautogui
此代码的(众多)问题之一是执行操作需要很长时间(我正在使用命名管道,因此我必须阅读每条收到的消息)。同样,这是经典的意大利面条式代码,我们都鄙视它。
我怎样才能写出更“可读”和更高效的代码?
另一方面,一旦我越过那个边界,我就无法真正回来。我应该让玩家不要再向左移动,但我真的不知道怎么做,因为我一直在重置def coordinates():
visited_coordinates = []
t_end = time.time() + 90
movements = []
while time.time() < t_end:
for message in generated_object(): # generated object returns a list with 4 elements
if [message[0],message[1]] in visited_coordinates:
if "left" in movements:
if "down" in movements:
if "right" in movements:
pydirectinput.press("up")
if message[3] != 4: # 4 means it is facing up
pydirectinput.press("up") # if it isn't facing up,it needs to be pressed twice
movements.append("up")
else:
pydirectinput.press("right")
if message[3] != 12: # 12 means it is facing right
pydirectinput.press("right") # same with facing right,and so on
movements.append("right")
else:
pydirectinput.press("down")
if message[3] != 0: # 0 means it is facing down
pydirectinput.press("down")
movements.append("down")
else:
pydirectinput.press("left")
if message[3] != 8: # 8 means it is facing left
pydirectinput.press("left")
movements.append("left")
continue
movements = []
if message[2] != 4: # this means it went beyond the border,and it must return
pydirectinput.press("right",presses=2)
visited_coordinates.append([message[0],message[1]])
return visited_coordinates
列表
解决方法
可以将项目放入字典中,然后遍历这些条目。
directions = { 'up': 4,'right': 12,'down': 0,'left':8 }
for direction,val in directions .items():
if direction in movements:
pydirectinput .press( direction )
if message[3] != val:
pydirectinput .press( direction )
movements .append( direction )
break ## you might want to break out of the loop here
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。