def test_with_key(self): player = Player("julie", "f") key = Item("key", "door key") key.door_code = 12345 hall = Location("hall") door = Door("north", hall, "a locked door", locked=True, opened=False) with self.assertRaises(ActionRefused): door.unlock(player) with self.assertRaises(ActionRefused): door.unlock(player, key) door.door_code = 12345 self.assertTrue(door.locked) door.unlock(player, key) self.assertFalse(door.locked) door.locked = True with self.assertRaises(ActionRefused): door.unlock(player) key.move(player, player) door.unlock(player) self.assertFalse(door.locked) door.lock(player) self.assertTrue(door.locked)
# define items and NPCs class Cat(NPC): def init(self): self.aliases={"cat"} mud_context.driver.defer(4, self, self.do_purr) def do_purr(self, driver): if random.random() > 0.5: self.location.tell("%s purrs happily." % capital(self.title)) else: self.location.tell("%s yawns sleepily." % capital(self.title)) driver.defer(random.randint(5, 20), self, self.do_purr) def notify_action(self, parsed, actor): if parsed.verb in ("pet", "stroke", "tickle", "cuddle", "hug"): self.tell_others("{Title} curls up in a ball and purrs contently.") elif parsed.verb in ("hello", "hi", "greet"): self.tell_others("{Title} stares at you incomprehensibly.") else: message = (parsed.message or parsed.unparsed).lower() if self.name in message: self.tell_others("{Title} looks up at you.") cat = Cat("garfield", "m", race="cat", description="A very obese cat, orange and black. It looks tired, but glances at you happily.") livingroom.insert(cat, None) key = Item("key", "small rusty key", "This key is small and rusty. It has a label attached, reading \"garden door\".") key.door_code = 1 closet.insert(key, None)