CodeCombat 创世与毁灭

Method: .destroy() vs .defeat()

LEVEL 13

从尘埃变到尘埃 (From Dust to Dust)

魔法清除术

指挥官,森林的道路被树木挡住了!而且敌人不断地从生成器里涌出来。
我们需要掌握两种魔法:

defeat() - 击败

这对有生命的单位(如生成器、怪物)使用。
这就像把对方打晕了,或者机器坏掉了。它还在那里,只是不动了。
munchkinSpawner.defeat()

destroy() - 销毁

这对任何东西都有效(树木、墙壁)。
这就像灭霸打了个响指。彻底消失,连渣都不剩!
passageForest1.destroy()

Defeat
尸体残留
Destroy
完全消失
dust_magic.py
1
# 用森林瓦片堵塞通道。
2
# 然后在玩家击败一些食人魔时摧毁他们。
3
4
player = game.spawnPlayerXY("duelist", 6, 34)
5
player.attackDamage = 35
6
player.maxHealth = 750
7
player.maxSpeed = 15
8
game.addMoveGoalXY(76, 34)
9
10
munchkinSpawner = game.spawnXY("generator", 16, 56)
矮人生成器
11
munchkinSpawner.spawnType = "munchkin"
12
munchkinSpawner.spawnDelay = 3
13
scoutSpawner = game.spawnXY("generator", 40, 10)
侦察兵生成器
14
scoutSpawner.spawnType = "scout"
15
scoutSpawner.spawnDelay = 6
16
17
passageForest1 = game.spawnXY("forest", 28, 34)
生成第一片森林路障
18
passageForest2 = game.spawnXY("forest", 52, 34)
生成第二片森林路障
19
20
game.defeated = 0
初始化击败计数器
21
ui.track(game, "defeated")
22
23
def onDefeat(event):
定义死亡事件逻辑
24
defeated = event.target
25
game.defeated += 1
计数器 + 1
26
if game.defeated == 3:
条件:如果击败了3个
27
# munchkinSpawner对象调用defeat()方法
28
munchkinSpawner.defeat()
让生成器“坏掉”(停止生成)
29
# 摧毁第一片森林通道。
30
passageForest1.destroy()
让森林彻底消失!
31
if game.defeated == 6:
条件:如果击败了6个
32
# scoutSpawner对象调用defeat()方法
33
scoutSpawner.defeat()
让第二个生成器停止
34
# 摧毁第二片森林通道。
35
passageForest2.destroy()
让第二片森林消失
36
37
game.setActionFor("munchkin", "defeat", onDefeat)
38
game.setActionFor("scout", "defeat", onDefeat)
LEVEL 14

牢笼 (Cages)

释放野兽

我们把两个大 Boss 关在了围栏(Fence)里。但现在我们需要击败他们来通关。
我们需要利用“小兵”来触发机关:

Step 1: 计数生成 (Counting)

这次我们在 spawn(生成)时计数,而不是 defeat
只要生成了足够的小兵,就说明机关充能完毕。

Step 2: 拆除围栏 (Breakout)

当计数达到要求:
1. defeat() 生成器:别再出小兵了,烦人。
2. destroy() 围栏:把路障变没,让 Boss 走出来!

fence.destroy()
cage_break.py
1
# 了解destroy与defeat的区别。
2
ogre1 = game.spawnXY("ogre", 12, 18)
生成左边的Boss
3
ogre1.behavior = "AttacksNearest"
4
ogre2 = game.spawnXY("ogre", 68, 50)
生成右边的Boss
5
ogre2.behavior = "AttacksNearest"
6
7
munchkinGenerator = game.spawnXY("generator", 12, 50)
8
munchkinGenerator.spawnAI = "Scampers"
9
munchkinGenerator.spawnType = "munchkin"
10
scoutGenerator = game.spawnXY("generator", 68, 12)
11
scoutGenerator.spawnAI = "Scampers"
12
scoutGenerator.spawnType = "scout"
13
14
# 后面会被摧毁的围栏。
15
leftFence = game.spawnXY("fence", 26, 14)
左侧围栏(挡住Boss)
16
rightFence = game.spawnXY("fence", 54 , 50)
右侧围栏(挡住Boss)
17
player = game.spawnPlayerXY("knight", 40, 34)
18
player.maxSpeed = 16
19
player.attackDamage = 20
20
game.scoutsSpawned = 0
初始化侦察兵计数
21
game.munchkinsSpawned = 0
初始化矮人计数
22
game.bossesDefeated = 0
初始化Boss击杀数
23
ui.track(game, "scoutsSpawned")
24
ui.track(game, "munchkinsSpawned")
25
ui.track(game, "bossesDefeated")
26
bossesGoal = game.addManualGoal("击败两个大食人魔。")
27
28
def onSpawn(event):
生成事件处理器
29
unit = event.target
30
if unit.type == "scout":
如果是侦察兵生成
31
game.scoutsSpawned += 1
计数 + 1
32
if game.scoutsSpawned >= 3:
如果生成了3个侦察兵
33
# scoutGenerator(侦察兵生成器)调用击败defeat()方法
34
scoutGenerator.defeat()
停止生成侦察兵
35
# rightFence(右边的围栏)调用destroy()摧毁方法
36
rightFence.destroy()
移除右边围栏!
37
if unit.type == "munchkin":
如果是矮人生成
38
game.munchkinsSpawned += 1
计数 + 1
39
if game.munchkinsSpawned >= 2:
如果生成了2个矮人
40
# munchkinGenerator(矮人生成器)调用击败defeat()方法
41
munchkinGenerator.defeat()
停止生成矮人
42
# leftFence(左边的围栏)调用destroy()摧毁方法
43
leftFence.destroy()
移除左边围栏!
44
45
def onDefeat(event):
定义Boss死亡事件
46
unit = event.target
47
if unit.type == "ogre":
48
# 对game.bossesDefeated计数器加一:
49
game.bossesDefeated += 1
击杀计数
50
51
game.setActionFor("munchkin", "spawn", onSpawn)
52
game.setActionFor("scout", "spawn", onSpawn)
53
game.setActionFor("ogre", "defeat", onDefeat)
54
55
while True:
56
if game.bossesDefeated >= 2:
如果击败了2个Boss
57
game.setGoalState(bossesGoal, True)
游戏胜利!