36 · 借刀
先读关卡目标,观察地图,再运用本课知识。
循环、字典、判断、算法...
所有知识点大集合!
扣哒世界 · 计算机科学4 · Python · 第36关
先读关卡目标,观察地图,再运用本课知识。
学习路线:读目标 → 看图解 → 写代码 → 运行检查
while hero.gold > 20: hero.summon("soldier")
for friend in hero.findFriends(): hero.command(friend, "attack")
target = {"x": 50, "y": 40}
soldiers = ["士兵1", "士兵2"] points[0] = {"x": 33, "y": 42}
if enemy: hero.attack(enemy) else: hero.move(point)
if enemy and enemy.team == "ogres": hero.attack(enemy)
# 向右移动一小步 newX = friend.pos.x + 0.35
if enemy.health > mostHealth: toughest = enemy
enemy.health # 生命值 enemy.team # 队伍 enemy.type # 类型
friend.pos.x # 横坐标 (左右) friend.pos.y # 纵坐标 (上下)
for i in range(4): hero.summon("soldier")
count = len(hero.findFriends())
best = None maxH = 0 for e in enemies: if e.health > maxH: best = e
while True: # 外层:永远监控 for f in friends: # 内层:检查每个朋友 # ...
扣哒世界 · 计算机科学4 · Python · 第36关
while 在条件成立时重复;for 依次遍历;嵌套循环会为每一轮外层执行完整内层。
hero.summon("soldier")列表用索引取元素,字典用键取值;遍历时注意范围并及时更新数据。
用 if/else 按条件选择行动;先判断目标是否存在,再读取属性。
hero.attack(enemy)
用加减计算坐标和计数;用比较运算形成条件,注意括号和运算顺序。
位置用横坐标 x、纵坐标 y 表示;目标坐标表示地点,不是要走的步数。
把重复步骤封装为函数,分清输入、处理和输出;一次验证一个小步骤。
hero.summon("soldier")把重复步骤封装为函数,分清输入、处理和输出;一次验证一个小步骤。
本节课关卡:36. 借刀