コード例 #1
0
def level():
    contents = []
    item = Item("exit", "The real exit")
    item.place((1,2))

    roach = Cockroach("roach", "a big green roach")
    roach.place((2,3))

    contents.append(item)
    contents.append(roach)
    return Level(contents, 4)
コード例 #2
0
    def loadItems(self, dataFilename):
        with open(dataFilename, 'r') as f:
            for line in f:
                item = Item()
                item.id = len(self.items)
                item.loadFromData(line.rstrip("\n").split(","))
                self.items.append(item)
                self.itemsProbabilities.append(item.weight)

        self.itemsProbabilities = numpy.array(
            map(float, self.itemsProbabilities)) / 10
        self.itemsProbabilities = self.itemsProbabilities / sum(
            self.itemsProbabilities)

        print "ItemManager: Loaded %d items" % len(self.items)
コード例 #3
0
 def test_remove_many_items_will_remove_only_as_many_as_exist(self):
     self.bag.add_many(Item("rhymes"), 5)
     removed_count, item = self.bag.remove_many("rhymes", 7)
     assert 5 == removed_count
     assert 0 == self.bag.how_many("rhymes")
     assert "rhymes" == item.name
     assert self.bag.isEmpty()
コード例 #4
0
 def test_adding_multiple_of_the_same_item_increases_item_count(self):
     butter = Item("butter")
     self.bag.add(butter)
     self.bag.add(butter)
     assert 2 == self.bag.item_count()
     self.bag.add(butter)
     assert 3 == self.bag.item_count()
コード例 #5
0
 def test_can_remove_an_item(self):
     self.bag.add(Item("cheezburger"))
     removed_count, item = self.bag.remove("cheezburger")
     assert 1 == removed_count
     assert "cheezburger" == item.name
     assert type(item) is Item
     assert 0 == self.bag.how_many("cheezburger")
コード例 #6
0
    def test_removed_items_behave_like_items_that_never_existed(self):
        self.bag.add(Item("smoke"))
        removed_count, item = self.bag.remove("smoke")
        assert 1 == removed_count

        removed_count, item = self.bag.remove("smoke")
        assert 0 == removed_count
        assert item == None
        assert 0 == self.bag.how_many("smoke")
        assert 0 == self.bag.item_count()
コード例 #7
0
    def pick_up_item(self, item):
        """Allows the player to pick up and item by removing an item from the
        room and placing it in their bag
        """
        if self.level.get_by_name(item) != None:
            player_coord = self.player.locate()
            item_coord = self.level.get_by_name(item).locate()
            if player_coord == item_coord:
                self.bag.add(Item(item))
                self.level.remove(item)
                return True

        return False
コード例 #8
0
ファイル: engine.py プロジェクト: trymoretimes/python
    def pick_up_item(self, item):
        """Allows the player to pick up and item by removing an item from the
        room and placing it in their bag
        """
        if self.level.get_by_name(item) != None:
            player_coord = self.player.locate()
            item_coord = self.level.get_by_name(item).locate()
            if player_coord == item_coord:

                if item == "food":
                    self.player.take_damage(-50)
                    if self.player.calc_health() > self.player.maxph:
                        self.player.take_damage(self.player.calc_health() -
                                                self.player.maxph)
                elif item == "yao":
                    self.player.maxph += 20
                else:
                    self.bag.add(Item(item))
                self.level.remove(item)
                return True

        return False
コード例 #9
0
def test_can_get_highest_display_priority(level):
    items = []

    high = Item("high", "highest display priority")
    high.display_priority = 100
    items.append(high)

    med = Item("med", "medium display priority")
    med.displau_priority = 10
    items.append(med)

    low = Item("low", "low low low")
    low.display_priority = 0
    items.append(low)

    result = highest_display_priority(items)
    assert result.display_priority == 100
コード例 #10
0
 def test_after_adding_item_bag_is_no_longer_empty(self):
     item = Item("rock")
     self.bag.add(item)
     assert not self.bag.is_empty()
コード例 #11
0
def hydrate(data):
    """Populates the level with data from the file"""
    contents = []

    if data == None:
        return contents

    for key, value in data.items():
        content_type = "item"
        keys = value.keys()
        description = None
        target = None
        damage = None

        if "x" not in keys or "y" not in keys:
            pass
        else:
            if "type" in keys:
                content_type = value["type"]

            if "description" in keys:
                description = value["description"]

            if "target" in keys:
                target = value["target"]

            if "damage" in keys:
                damage = value["damage"]

            if content_type == "creature":
                locatable = Cockroach(key, description)
                locatable.target = target
                # if target:
                #     for itm in contents:
                #         if itm.name == target:
                #             locatable.set_target(itm)
                #     else:
                #         #What should go here?
                #         pass
            else:
                locatable = Item(key, description)

            if content_type == "weapon":
                locatable = Weapon(damage)
                locatable.name = key
                locatable.description = description

            locatable.place((value["x"], value["y"]))

            if "display" in keys:
                locatable.set_display(value["display"])

            contents.append(locatable)

    index = None
    for item in contents:
        if isinstance(item, Monster):
            index = contents.index(item)
    if index is not None:
        creature = contents.pop(index)
        for itm in contents:
            if itm.name == creature.target:
                creature.set_target(itm)
        contents.append(creature)

    return contents
コード例 #12
0
 def test_item_accepts_name_desc_on_init(self):
     item = Item("dragonglass", "seeing dragons")
     assert item.name == "dragonglass"
     assert item.description == "seeing dragons"
コード例 #13
0
 def test_can_add_many(self):
     turtle = Item("turtle")
     self.bag.add_many(turtle, 5)
     assert 5 == self.bag.how_many("turtle")
     assert 5 == self.bag.item_count()
コード例 #14
0
 def bag_n_dump(self, name):
     item = Item(name)
     self.bag.add(item)
     return self.bag.dump()
コード例 #15
0
 def test_can_fund_how_many_of_an_item_are_in_bag(self):
     planet = Item("planet")
     self.bag.add(planet)
     self.bag.add(planet)
     assert 2 == self.bag.how_many("planet")
コード例 #16
0
 def test_can_look_in_bag(self):
     stick = Item("stick")
     self.bag.add(stick)
     seen = self.bag.look()
     assert "1 stick" in seen
コード例 #17
0
 def test_remove_many_items_from_bag_at_once(self):
     self.bag.add_many(Item("blind mouse"), 5)
     removed_count, item = self.bag.remove_many("blind mouse", 3)
     assert 3 == removed_count
     assert 2 == self.bag.how_many("blind mouse")
     assert "blind mouse" == item.name
コード例 #18
0
def hydrate(data, size):
    random_place = []
    """Populates the level with data from the file"""
    contents = []

    if data == None:
        return contents

    for key, value in data.items():
        content_type = "item"
        keys = value.keys()
        description = None
        target = None
        damage = None

        if "x" not in keys or "y" not in keys:
            pass
        else:
            if "type" in keys:
                content_type = value["type"]

            if "description" in keys:
                description = value["description"]

            if "target" in keys:
                target = value["target"]

            if "damage" in keys:
                damage = value["damage"]
            if content_type == "creature":
                if value["display"] == "蟑":
                    locatable = Cockroach(key, description)
                elif value["display"] == "狗":
                    locatable = Dog(key, description)
                elif value["display"] == "狼":
                    locatable = Wolf(key, description)
                else:
                    locatable = Dog(key, description)
                locatable.target = target
# if target:
#     for itm in contents:
#         if itm.name == target:
#             locatable.set_target(itm)
#     else:
#         #What should go here?
#         pass
            else:
                locatable = Item(key, description)

            if content_type == "weapon":
                if value["display"] == "剑":
                    locatable = Excalibur(damage)
                locatable.name = key
                locatable.description = description
            post_x = random.randint(0, size - 1)
            post_y = random.randint(0, size - 1)
            post_data = str(post_x) + '*' + str(post_y)
            while post_data in random_place:
                post_x = random.randint(0, size - 1)
                post_y = random.randint(0, size - 1)
                post_data = str(post_x) + '*' + str(post_y)
            random_place.append(post_data)

            locatable.place((post_x, post_y))

            if "display" in keys:
                locatable.set_display(value["display"])

            contents.append(locatable)
    index = []
    for item in contents:
        if isinstance(item, Monster):
            index.append(contents.index(item))
    for i in index:
        creature = contents.pop(i)
        for itm in contents:
            if itm.name == creature.target:
                creature.set_target(itm)
        contents.insert(i, creature)
    return contents
コード例 #19
0
 def test_can_add_item_to_bag(self):
     assert self.bag.item_count() == 0
     item = Item("thing")
     self.bag.add(item)
     assert self.bag.item_count() == 1