标签:ash 一点 extc 事件 计数器 应用 rac speed 代码
(点击图片进入关卡)
您可以创建游戏对象。 但是你可以摧毁或破坏它们。
简介
我们最多时候为游戏 spawn 新对象。 但是,有时我们需要删除它们。 有两种方法可以做到这一点:
- 单元或可攻击对象(例如 generator )可以用`.defeat()``方法 "killed"。
- 任何物体都可以用`.destroy()``方法从游戏场景中移除
# 创建并击败小食人魔
munchkin = game.spawnXY("munchkin", 10, 10)
munchkin.defeat() # 我们可以看到被击败的食人魔。
# 创建并移除侦察兵
scout = game.spawnXY("munchkin", 20, 20)
scout.destroy() # 这没有东西
默认代码
# 用森林瓦片堵塞通道。
# 然后在玩家击败一些食人魔时摧毁他们。
# 设置玩家。
player = game.spawnPlayerXY("duelist", 6, 34)
player.attackDamage = 35
player.maxHealth = 750
player.maxSpeed = 15
# 玩家应该穿过森林才能获胜。
game.addMoveGoalXY(76, 34)
# 设置敌人。
munchkinSpawner = game.spawnXY("generator", 16, 56)
munchkinSpawner.spawnType = "munchkin"
munchkinSpawner.spawnDelay = 3
scoutSpawner = game.spawnXY("generator", 40, 10)
scoutSpawner.spawnType = "scout"
scoutSpawner.spawnDelay = 5
# 这些森林砖应该堵住通道
passageForest1 = game.spawnXY("forest", 28, 34)
# 创建第二个森林来阻止第二个通道:
game.defeated = 0
ui.track(game, "defeated")
def onDefeat(event):
defeated = event.target
game.defeated += 1
# 如果4个食人魔被击败:
if game.defeated == 3:
# 击败食人魔生成器。
munchkinSpawner.defeat()
# 摧毁第一片森林通道。
passageForest1.destroy()
# 如果8个食人魔被击败:
# 调用scoutSpawner的失败方法:
# 摧毁第二片森林通道。
# 为"munchkin"s 和"scout"s设置"defeat"事件处理程序。
game.setActionFor("munchkin", "defeat", onDefeat)
game.setActionFor("scout", "defeat", onDefeat)
# 击败这个游戏!
概览
defeat() 和 destroy() 是类似的方法,但它们之间存在巨大的差异。
defeat() 就像 unit.health = 0 一样,只能应用于单位或可攻击物体,比如 "generator" 。 此外, "defeated"的对象仍然在游戏场景中。 另外它会为敌方单位增加 game.defeated 计数器。 您可以将其用于 "restrictedzones",破坏事件中的某些对象等。
generator = game.spawnXY("generator", 40, 34)
while True:
if game.time == 10:
generator.defeat() # 它毁了,并且我们看到一些残留的石头。
player.say("I haven‘t touched it. It was broken itself.")
destroy() 从游戏场景中移除一个对象,并可用于任何事情。 尤其对障碍物有用,因为它们没有 health ,也不能 defeat 。 您可以移除某些事件的障碍物,例如,清除此级别的森林通道。 这种方法有更多的用法,我们稍后会看到。
potion = game.spawnXY("potion", 40, 34)
while True:
if game.time == 10:
potion.destroy() # 只是一个空的空间。
player.say("Where is it?! It was here just a second ago.")
从尘埃变到尘埃 解法
# 用森林瓦片堵塞通道。
# 然后在玩家击败一些食人魔时摧毁他们。
# 设置玩家。
player = game.spawnPlayerXY("duelist", 6, 34)
player.attackDamage = 35
player.maxHealth = 750
player.maxSpeed = 15
# 玩家应该穿过森林才能获胜。
game.addMoveGoalXY(76, 34)
# 设置敌人。
munchkinSpawner = game.spawnXY("generator", 16, 56)
munchkinSpawner.spawnType = "munchkin"
munchkinSpawner.spawnDelay = 3
scoutSpawner = game.spawnXY("generator", 40, 10)
scoutSpawner.spawnType = "scout"
scoutSpawner.spawnDelay = 6
# 这些森林砖应该堵住通道
passageForest1 = game.spawnXY("forest", 28, 34)
# 创建第二个森林来阻止第二个通道:
passageForest2 = game.spawnXY("forest", 52, 34)
game.defeated = 0
ui.track(game, "defeated")
def onDefeat(event):
defeated = event.target
game.defeated += 1
# 如果4个食人魔被击败:
if game.defeated == 3:
# 击败食人魔产卵器。
munchkinSpawner.defeat()
# 摧毁第一片森林通道。
passageForest1.destroy()
# 如果8个食人魔被击败:
if game.defeated == 6:
# 调用scoutSpawner的失败方法:
scoutSpawner.defeat()
# 摧毁第二片森林通道。
passageForest2.destroy()
# 为"munchkin"s 和"scout"s设置"defeat"事件处理程序。
game.setActionFor("munchkin", "defeat", onDefeat)
game.setActionFor("scout", "defeat", onDefeat)
# 击败这个游戏!
「网易官方」极客战记(codecombat)攻略-游戏开发2-从尘埃变到尘埃-from-dust-to-dust
标签:ash 一点 extc 事件 计数器 应用 rac speed 代码
原文地址:https://www.cnblogs.com/codecombat/p/12787657.html