「网易官方」极客战记(codecombat)攻略-游戏开发2-不要触摸他们-dont-touch-them-编程知识网
                                                                          (点击图片进入关卡)

设定食人魔人口的目标数量。

简介

手动目标可以标记为成功或失败。 你也可以通过属性 success 阅读目标的状态。

goal = game.addManualGoal(“Do the thing.”)
# 这会将目标设为成功状态
game.setGoalState(goal, True)
# 这会将目标设为失败状态
game.setGoalState(goal, False)
# 检查目标的状态
if goal.success:
    hero.say(“Done!”)

当您将手动目标标记为失败时,游戏将结束。

让我们用它来制作一个游戏,玩家只需要击败 侦察员 食人魔并且不能碰到 ` munchkin’ 食人魔。

默认代码

# 使用手动目标来指定哪些食人魔被击败。
def createGenerator(spawnType, x, y, spawnAI):
    generator = game.spawnXY(“generator”, x, y)
    generator.spawnType = spawnType
    generator.spawnAI = spawnAI
# 侦察员是积极活跃的,munchkins只是在行走。
createGenerator(“scout”, 12, 12, “AttacksNearest”)
createGenerator(“scout”, 68, 56, “AttacksNearest”)
createGenerator(“munchkin”, 12, 56, “Scampers”)
createGenerator(“munchkin”, 68, 12, “Scampers”)
player = game.spawnPlayerXY(“duelist”, 40, 34)
player.maxHealth = 1000
player.attackDamage = 20
player.maxSpeed = 20
# 这些是我们的目标。 注意我们将它们保存在变量中!
spawnMunchkinsGoal = game.addManualGoal(“产生6个munchkins”)
dontTouchGoal = game.addManualGoal(“不要攻击munchkins。”)
defeatScoutsGoal = game.addManualGoal(“击败6名侦察兵。”)
# 游戏属性用于统计新的和失败的食人魔。
game.spawnedMunchkins = 0
game.defeatedScouts = 0
ui.track(game, “spawnedMunchkins”)
ui.track(game, “defeatedScouts”)
def onSpawn(event):
    game.spawnedMunchkins += 1
def onDefeat(event):
    unit = event.target
    if unit.type == “scout”:
        game.defeatedScouts += 1
    if unit.type == “munchkin”:
        # 如果munchkin被击败,dontTouchGoal失败。
        game.setGoalState(dontTouchGoal, False)
        player.say(“Oops.”)
def checkGoals():
    # 如果game.defeatedScouts大于5:
        # 设置defeatScoutsGoal状态为成功。.
    # 如果game.spawnedMunchkins大于5:
        # 设置spawnMunchkinsGoal状态为成功。.
    # 如果其他目标都完成了。
    if spawnMunchkinsGoal.success:
        if defeatScoutsGoal.success:
            # 将dontTouchGoal状态设置为成功。.
game.setGoalState(dontTouchGoal, True)
game.setActionFor(“munchkin”, “spawn”, onSpawn)
game.setActionFor(“munchkin”, “defeat”, onDefeat)
game.setActionFor(“scout”, “defeat”, onDefeat)
while True:
    checkGoals()

概览

如果你使用手动目标,不要忘记设置他们的状态。

在标记完成之前,一些目标可能取决于其他目标。 在这个关卡中,如果玩家采取禁止动作,则 “dontTouchGoal” 被标记为缺失,并且仅标记 如果其他两个目标都成功的话,就可以成功。

对于目前的关卡,我们要等到 “成功” 目标完成,并且只有在这之后,我们才会将 “dontTouchGoal” 的状态设置为True。

这与 addSurviveGoal 工作方式类似。 只有在所有其他目标完成后才能成功。

不要触摸他们 解法

# 使用手动目标来指定哪些食人魔被击败。
def createGenerator(spawnType, x, y, spawnAI):
    generator = game.spawnXY(“generator”, x, y)
    generator.spawnType = spawnType
    generator.spawnAI = spawnAI
# 侦察员是积极活跃的,munchkins只是在行走。
createGenerator(“scout”, 12, 12, “AttacksNearest”)
createGenerator(“scout”, 68, 56, “AttacksNearest”)
createGenerator(“munchkin”, 12, 56, “Scampers”)
createGenerator(“munchkin”, 68, 12, “Scampers”)
player = game.spawnPlayerXY(“duelist”, 40, 34)
player.maxHealth = 1000
player.attackDamage = 20
player.maxSpeed = 20
# 这些是我们的目标。 注意我们将它们保存在变量中!
spawnMunchkinsGoal = game.addManualGoal(“产生6个munchkins”)
dontTouchGoal = game.addManualGoal(“不要攻击munchkins。”)
defeatScoutsGoal = game.addManualGoal(“击败6名侦察兵。”)
# 游戏属性用于统计新的和失败的食人魔。
game.spawnedMunchkins = 0
game.defeatedScouts = 0
ui.track(game, “spawnedMunchkins”)
ui.track(game, “defeatedScouts”)
def onSpawn(event):
    game.spawnedMunchkins += 1
def onDefeat(event):
    unit = event.target
    if unit.type == “scout”:
        game.defeatedScouts += 1
    if unit.type == “munchkin”:
        # 如果munchkin被击败,dontTouchGoal失败。
        game.setGoalState(dontTouchGoal, False)
        player.say(“Oops.”)
def checkGoals():
    # 如果game.defeatedScouts大于5:
    if game.defeatedScouts >= 6:
        # 设置defeatScoutsGoal状态为成功。
        game.setGoalState(defeatScoutsGoal, True)
    # 如果game.spawnedMunchkins大于5:
    if game.spawnedMunchkins >= 6:
        # 设置spawnMunchkinsGoal状态为成功。
        game.setGoalState(spawnMunchkinsGoal, True)
    # 如果其他目标都完成了。
    if spawnMunchkinsGoal.success:
        if defeatScoutsGoal.success:
            # 将dontTouchGoal状态设置为成功。.
            game.setGoalState(dontTouchGoal, True)
game.setActionFor(“munchkin”, “spawn”, onSpawn)
game.setActionFor(“munchkin”, “defeat”, onDefeat)
game.setActionFor(“scout”, “defeat”, onDefeat)
while True:
    checkGoals()
 
 

本攻略发于极客战记官方教学栏目,原文地址为:

https://codecombat.163.com/news/jikezhanji-buyaochumotamen

极客战记——学编程,用玩的!