# \[Adventurer\] Key Traps

**URL:** <https://discourse.codecombat.com/t/adventurer-key-traps/10622>\
**Category:** Adventurer\
**Created:** [February 19, 2017, 2:02pm UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622 "2017-02-19T14:02:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [February 19, 2017, 2:02pm UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622/1 "2017-02-19T14:02:06Z")

</div>

The new desert level with pets: [Key Traps](https://codecombat.com/play/level/key-traps).

This level finishes the series about `pet.findNearestByType` method.

Use your pet to find all keys, also the pet can help with potions.

---

<div class="post-metadata">

**Author:** ![CantMelee](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@CantMelee](https://discourse.codecombat.com/u/CantMelee)\
**Post date:** [February 19, 2017, 9:39pm UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622/2 "2017-02-19T21:39:39Z")

</div>

Tried playing this level, not sure if this is a bug/me messing up.  
I’m getting an error about “bronzeKey” where it’s not even mentioned:

> **[Gyazo](https://gyazo.com/f72932f3b09297de54c6828ec3795184)**
>
> ​

  
here is the code :

```python
# Get three keys and free the paladin.

def onSpawn(event):
    # The pet should find and fetch three keys.
    # You need next types: "bronze-key". "silver-key" and "gold-key".
    bronzeKey = pet.findNearestByType("bronze-key")
    if bronzeKey:
        pet.fetch(BronzeKey)
    silverKey = pet.findNearestByType("silver-key")
    if silverKey:
        pet.fetch(silverKey)
    goldKey = pet.findNearestByType("gold-key")
    if goldKey:
        pet.fetch(goldKey)

pet.on("spawn", onSpawn)

while True:
    enemy = hero.findNearestEnemy()
    if enemy and enemy.team == "ogres":
        hero.attack(enemy)
    if hero.health < 300:
        # You can use pets in the main thread too.
        potion = pet.findNearestByType("potion")
        if potion:
            hero.moveXY(potion.pos.x, potion.pos.y)

```

EDIT : Noticed typing error on my part. Fixed it and everything worked out.  
Was a very fun level once I got it to work. Found it a bit weird how the doors automatically open without you walking near them though. Maybe add to the end of it and make it so that the hero then has to walk up to the doors?

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [February 20, 2017, 5:37am UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622/3 "2017-02-20T05:37:20Z")

</div>

> [@CantMelee](#):
>
> Maybe add to the end of it and make it so that the hero then has to walk up to the doors?

It’s a little hard to detect for user’s code when you have all three keys. I wanted to add that mechanic, but it made the sample code too complex for the early desert, so I simplified it.

---

<div class="post-metadata">

**Author:** ![htrnblr](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@htrnblr](https://discourse.codecombat.com/u/htrnblr)\
**Post date:** [February 20, 2017, 7:05am UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622/4 "2017-02-20T07:05:08Z")

</div>

Minor bug  
peasant can pick up key but it not counted

Thanks for level  
Environment let me come up with really interesting solution

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [February 20, 2017, 7:07am UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622/5 "2017-02-20T07:07:42Z")

</div>

Thanks. Did you use boss start for that? Which command?

---

<div class="post-metadata">

**Author:** ![htrnblr](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@htrnblr](https://discourse.codecombat.com/u/htrnblr)\
**Post date:** [February 20, 2017, 7:09am UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622/6 "2017-02-20T07:09:29Z")

</div>

```python
hero.command( hero.findByType( "peasant" )[0], "move", { x : 18, y : 60 } );

```

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [February 20, 2017, 7:33am UTC](https://discourse.codecombat.com/t/adventurer-key-traps/10622/7 "2017-02-20T07:33:53Z")

</div>

Thanks. First I wanted to restrict boss stars. But I think it’s not “funny” enough 🙂 So I made peasants “lazy”.
