「网易官方」极客战记(codecombat)攻略-游戏开发2-热宝石-hot-gems

                                                                          (点击图片进入关卡)

热宝石可能是危险的。 最好避免它们。 好吧,也许一个...... 或两个。

简介

让我们练习使用 "collect" 事件。

此事件允许您跟踪某些角色收集的物品。

在这个级别上,我们会这么做,因为宝石很热并且伤害了英雄,而金币很冷并且治愈了英雄。

然而,收集目标不会区分它们,所以玩家可以收集他们两个。

不要忘记收集至少一个宝石,以确保它的工作!

function onCollect(event){
    var who = event.target;
    var what = event.other;
    if(what.type ==“potion”){
        who.say(“哦,它中毒了。”);
        who.defeat();
    }
}
somebody.on(“collect”,onCollect);
def onCollect(event):
    who = event.target
    what = event.other
    如果what.type =="potion" :
        who.say(“哦,它中毒了。”)
        who.defeat()
    }
somebody.on(“collect”,onCollect)

默认代码

# 金币应该治愈你,宝石会伤害你。
player = game.spawnPlayerXY("captain", 40, 34)
player.maxSpeed = 20
# 我们使用相对健康价值的宝石/硬币。
healthValue = player.maxHealth / 4
# 该函数在随机地点产生一个物品/单位。
def spawnRandomPlaced(type):
    x = game.randomInteger(12, 68)
    y = game.randomInteger(12, 56)
    game.spawnXY(type, x, y)
# 辅助变量,goal和UI。
itemInterval = 2
itemSpawnTime = 0
game.collected = 0
ui.track(game, "time")
ui.track(game, "collected")
ui.track(player, "health")
game.addCollectGoal(10)
game.addSurviveGoal()
# 来,我们让食人魔狂暴。
def onSpawn(event):
    unit = event.target
    unit.behavior = "AttacksNearest"
game.setActionFor("munchkin", "spawn", onSpawn)
# 这定义了收集不同物品的结果。
def onCollect(event):
    # event.target 包含是收集者。
    collector = event.target
    # event.other包含收集的物品。
    item = event.other
    # 通过1 增加game.collected。
    game.collected += 1
    # 如果物品的类型是"gold-coin":

 

        # 通过healthValue增加collector.health :

 

    # 如果商品的类型是"gem":

 

        # 通过healthValue降低收集者的生命值:

 

# 在"collect"事件中为玩家分配onCollect处理程序。
player.on("collect", onCollect)
def checkSpawnTimer():
    if game.time >= itemSpawnTime:
        spawnRandomPlaced("gem")
        spawnRandomPlaced("gold-coin")
        spawnRandomPlaced("munchkin")
        itemSpawnTime += itemInterval
while True:
    checkSpawnTimer()

概览

"collect" 事件数据有两个重要的属性: target 和 other 。

target 是行动的主题 - 谁收集了一些东西(以及事件的 "owner")。

other 是收集的对象 - 收集的内容。

所以这就像是一个句子: target'收集了 other`。

我们使用术语 other 而不是 item ,因为会有更多的东西 将来有一个主题和对象的事件,以及所有这些 “其他” 都可以一致使用。

这也是一种用于专业游戏引擎的惯例,您可以在继续制作自己的游戏引擎时看到 CodeCombat 以外的游戏!

当收集器 “踩下” 一个项目(收集它)时,触发 ` "collect" 事件。 在这种情况下,会发生默认收集行动(例如,药水治疗)并且物品消失。

你可以跟踪这些事件并对它们进行计数,并为它们添加自己的行动(例如增加药剂的攻击伤害)。 有创意!

热宝石 解法

# 金币应该治愈你,宝石会伤害你。
player = game.spawnPlayerXY("captain", 40, 34)
player.maxSpeed = 20
# 我们使用相对健康价值的宝石/硬币。
healthValue = player.maxHealth / 4
# 该函数在随机地点产生一个物品/单位。
def spawnRandomPlaced(type):
    x = game.randomInteger(12, 68)
    y = game.randomInteger(12, 56)
    game.spawnXY(type, x, y)
# 辅助变量,goal和UI。
itemInterval = 2
itemSpawnTime = 0
game.collected = 0
ui.track(game, "time")
ui.track(game, "collected")
ui.track(player, "health")
game.addCollectGoal(10)
game.addSurviveGoal()
# 来,我们让食人魔狂暴。
def onSpawn(event):
    unit = event.target
    unit.behavior = "AttacksNearest"
game.setActionFor("munchkin", "spawn", onSpawn)
# 这定义了收集不同物品的结果。
def onCollect(event):
    # event.target 包含是收集者。
    collector = event.target
    # event.other包含收集的物品。
    item = event.other
    # 通过1 增加game.collected。
    game.collected += 1
    # 如果物品的类型是"gold-coin":
    if item.type == "gold-coin":
        # 通过healthValue增加collector.health :
        collector.health += healthValue
    # 如果商品的类型是"gem":
    if item.type == "gem":
        # 通过healthValue降低收集者的生命值:
        collector.health -= healthValue
# 在"collect"事件中为玩家分配onCollect处理程序。
player.on("collect", onCollect)
def checkSpawnTimer():
    if game.time >= itemSpawnTime:
        spawnRandomPlaced("gem")
        spawnRandomPlaced("gold-coin")
        spawnRandomPlaced("munchkin")
        itemSpawnTime += itemInterval
while True:
    checkSpawnTimer()
 
 

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

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

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

原文地址:https://www.cnblogs.com/codecombat/p/12799994.html