「网易官方」极客战记(codecombat)攻略-游戏开发2-压盘-pressure-plate

                                                                          (点击图片进入关卡)

使用压力板来触发有趣的游戏效果!

简介

"collide" 事件可以告诉你两个物体何时碰撞,但是不碰撞的物体怎么办呢?

使用主游戏循环来检查英雄与非碰撞对象的接近程度:

player = game.spawnXY("captain", 10, 20)

 

# X标记不会产生碰撞(玩家可以穿过它)
xmark = game.spawnXY("x-mark-stone", 20, 20)

 

def checkForProximity():
    if xmark.distanceTo(player) <= 1:
        # 做点有趣的事!

 

# 这是主游戏循环。
while True:
    checkForProximity()

默认代码

# 学习使用X标记作为触发器!

 

# 设置迷宫。
game.spawnMaze(3)
game.spawnXY("forest", 20, 45)
game.spawnXY("forest", 20, 28)
game.spawnXY("forest", 20, 58)
game.spawnXY("forest", 36, 60)
game.spawnXY("forest", 36, 44)
game.spawnXY("forest", 36, 13)

 

# 玩家和目标。
player = game.spawnPlayerXY("knight", 60, 34)
goal = game.addManualGoal("Defeat 30 munchkins!")

 

# 火炮!
fire1 = game.spawnXY("fire-spewer", 27, 12)
fire2 = game.spawnXY("fire-spewer", 29, 12)
fire3 = game.spawnXY("fire-spewer", 11, 12)
fire4 = game.spawnXY("fire-spewer", 13, 12)
fire1.direction = "vertical"
fire2.direction = "vertical"
fire3.direction = "vertical"
fire4.direction = "vertical"
fire1.spamCooldown = 0
fire2.spamCooldown = 0
fire3.spamCooldown = 0
fire4.spamCooldown = 0

 

# 注意设置为禁用 True stops the fire.
fire1.disabled = True
fire2.disabled = True
fire3.disabled = True
fire4.disabled = True

 

# 这些是我们的触发器。
# 骨制X会引发火灾。
boneX = game.spawnXY("x-mark-bones", 60, 50)
# stoneX会触发矮人生成。
stoneX = game.spawnXY("x-mark-stone", 60, 22)

 

game.spawns = 0
game.defeated = 0
ui.track(game, "spawns")
ui.track(game, "defeated")

 

# 为被击败的矮人计数。
def onDefeat(event):
    game.defeated += 1

 

game.setActionFor("munchkin", "defeat", onDefeat)

 

# 每0.25秒生成2个矮人。
game.spawnTime = 0
game.spawnCooldown = 0.25

 

def spawnMunchkinsOverTime():
    if game.time >= game.spawnTime:
        m1 = game.spawnXY("munchkin", 11, 57)
        m2 = game.spawnXY("munchkin", 28, 57)
        m1.behavior = "Scampers"
        m2.behavior = "Scampers"
        # 为生成数量计数。
        game.spawns += 2
        game.spawnTime = game.time + game.spawnCooldown

 

# 打开火焰当玩家靠近骨头X
def checkBoneX():
    if boneX.distanceTo(player) <= 1:
        player.say("Firing!")
        fire1.disabled = False
        # 为其他加农炮将disabled设置为False

 

else:
        fire1.disabled = True
        fire2.disabled = True
        fire3.disabled = True
        fire4.disabled = True

 

# 如果玩家靠近石头X就生成食人魔
def checkStoneX():
    pass
    # 如果玩家到石头X之间的距离小于等于1

 

        # 调用函数spawnMunchkinsOverTime()

 

 

def checkVictory():
    if game.defeated >= 30:
        pass
        # 使用 game.setGoalState(goal, True)

 

# 主游戏循环
while True:
    checkStoneX()
    checkBoneX()
    checkVictory()

压盘 解法

# 学习使用X标记作为触发器!

 

# 设置迷宫。
game.spawnMaze(3)
game.spawnXY("forest", 20, 45)
game.spawnXY("forest", 20, 28)
game.spawnXY("forest", 20, 58)
game.spawnXY("forest", 36, 60)
game.spawnXY("forest", 36, 44)
game.spawnXY("forest", 36, 13)

 

# 玩家和目标。
player = game.spawnPlayerXY("knight", 60, 34)
goal = game.addManualGoal("Defeat 30 munchkins!")

 

# 火炮!
fire1 = game.spawnXY("fire-spewer", 27, 12)
fire2 = game.spawnXY("fire-spewer", 29, 12)
fire3 = game.spawnXY("fire-spewer", 11, 12)
fire4 = game.spawnXY("fire-spewer", 13, 12)
fire1.direction = "vertical"
fire2.direction = "vertical"
fire3.direction = "vertical"
fire4.direction = "vertical"
fire1.spamCooldown = 0
fire2.spamCooldown = 0
fire3.spamCooldown = 0
fire4.spamCooldown = 0

 

# 注意设置为禁用 True stops the fire.
fire1.disabled = True
fire2.disabled = True
fire3.disabled = True
fire4.disabled = True

 

# 这些是我们的触发器。
# 骨制X会引发火灾。
boneX = game.spawnXY("x-mark-bones", 60, 50)
# stoneX会触发矮人生成。
stoneX = game.spawnXY("x-mark-stone", 60, 22)

 

game.spawns = 0
game.defeated = 0
ui.track(game, "spawns")
ui.track(game, "defeated")

 

# 为被击败的矮人计数。
def onDefeat(event):
    game.defeated += 1

 

game.setActionFor("munchkin", "defeat", onDefeat)

 

# 每0.25秒生成2个矮人。
game.spawnTime = 0
game.spawnCooldown = 0.25

 

def spawnMunchkinsOverTime():
    if game.time >= game.spawnTime:
        m1 = game.spawnXY("munchkin", 11, 57)
        m2 = game.spawnXY("munchkin", 28, 57)
        m1.behavior = "Scampers"
        m2.behavior = "Scampers"
        # 为生成数量计数。
        game.spawns += 2
        game.spawnTime = game.time + game.spawnCooldown

 

# 打开火焰当玩家靠近骨头X
def checkBoneX():
    if boneX.distanceTo(player) <= 1:
        player.say("Firing!")
        fire1.disabled = False
        # 为其他加农炮将disabled设置为False
        fire2.disabled = False
        fire3.disabled = False
        fire4.disabled = False
    else:
        fire1.disabled = True
        fire2.disabled = True
        fire3.disabled = True
        fire4.disabled = True

 

# 如果玩家靠近石头X就生成食人魔
def checkStoneX():
    # 如果玩家到石头X之间的距离小于等于1
    if stoneX.distanceTo(player) <= 1:
        # 调用函数spawnMunchkinsOverTime()
        spawnMunchkinsOverTime()
def checkVictory():
    if game.defeated >= 30:
        # 使用 game.setGoalState(goal, True)
        game.setGoalState(goal, True)

 

# 主游戏循环
while True:
    checkStoneX()
    checkBoneX()
    checkVictory()
 

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

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

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

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