consolidate waiting time: 如果有多个步骤需要等待,可以将它们合并到一个步骤中,以减少等待次数。
use variables: 可以使用变量来存储 frequently used objects or locations, to reduce redundancy.
use functions: 可以将重复使用的代码块 abstract 到函数中,以提高可读性和可 maintainability。
error handling: 添加错误处理机制,以处理可能出现的异常情况。
定义变量
npc_dict = {
‘赏金猎人’: (109, 86),
‘仓库管理员’: (111, 88),
‘流浪商人’: (111, 80),
‘谜之门’: (117, 93),
}
item_dict = {
‘赏金令牌’: 0,
‘头盔’: 0,
‘靴子’: 0,
‘戒指’: 0,
‘手镯’: 0,
‘项链’: 0,
‘衣服’: 0,
‘武器’: 0,
}
map_name = ”
Copy
函数
def wait(seconds):
time.sleep(seconds * 1000)
def talk_npc(npc_name):
find_npc(npc_name)
talk_to_npc()
select_option(1) # 默认选择第1个对话选项
def sell_items():
select_option(2) # 选择卖东西
for item in [‘头盔’, ‘靴子’, ‘戒指’, ‘手镯’, ‘项链’, ‘衣服’, ‘武器’]:
sell_item(item)
def repair_items():
select_option(1) # 选择修理装备
for item in [‘头盔’, ‘项链’, ‘手镯’, ‘左戒指’, ‘右戒指’, ‘靴子’, ‘腰带’, ‘武器’, ‘衣服’]:
if item_dict[item] and item_dict[item + ‘_dur’] >= 4:
repair_item(item)
def buy_items():
select_option(2) # 选择买东西
buy_item(‘地牢卷轴’, 1)
buy_random_teleport_scrolls(4)
buy_item(‘特级金创药包’, 18)
buy_item(‘超级魔法药’, 13)
def find_npc(npc_name):
global map_name
if map_name != get_current_map():
use_item(‘地牢卷轴’)
wait(3)
map_name = get_current_map()
for x in range(110, 118):
for y in range(80, 94):
if is_npc_here(npc_dict[npc_name], x, y):
move_to(x, y)
return
def talk_to_npc():
select_option(0) # 选择和 NPC 对话
def select_option(index):
press_button(index)
def sell_item(item):
select_item(item)
sell_item_to_npc()
def repair_item(item):
select_item(item)
repair_item_to_npc()
def buy_item(item, count):
select_item(item)
buy_item_from_npc(count)
def sell_item_to_npc():
press_button(1)
def repair_item_to_npc():
press_button(2)
def buy_item_from_npc(count):
press_button(3)
input_number(count)
press_button(4)
while True:
# 使用地牢卷轴
use_item(‘地牢卷轴’)
wait(3)
# 等待3秒
wait(3)
if get_current_map() == ‘海岛’:
# 移动到特定位置
move_to(110, 84)
# 检查赏金令牌
if item_dict[‘赏金令牌’] == 0:
# 与赏金猎人对话
talk_npc(‘赏金猎人’)
# 交还赏金令牌
select_option(3)
input_number(1)
select_option(0)
# 结束对话
select_option(0)
# 与仓库管理员对话
talk_npc(‘仓库管理员’)
# 存放物品
select_option(1)
input_number(1)
select_option(0)
# 与流浪商人对话
talk_npc(‘流浪商人’)
# 卖掉所有物品
sell_items()
# 修理所有物品
repair_items()
# 结束对话
select_option(0)
# 与谜之门对话
talk_npc(‘谜之门’)
# 进入地牢
select_option(1)
# 战斗
while True:
if find_monster():
attack_monster()
else:
use_item(‘随机卷轴’)
else:
buy_items()