当前位置: 首页>>代码示例>>Python>>正文


Python Inventory.add_block方法代码示例

本文整理汇总了Python中inventory.Inventory.add_block方法的典型用法代码示例。如果您正苦于以下问题:Python Inventory.add_block方法的具体用法?Python Inventory.add_block怎么用?Python Inventory.add_block使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在inventory.Inventory的用法示例。


在下文中一共展示了Inventory.add_block方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Game

# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import add_block [as 别名]
class Game(object):
    STATUS_NEW = "new"
    STATUS_ON = "on"
    STATUS_VICTORY = "victory"
    STATUS_DEFEAT = "defeat"

    def __init__(self, grid):
        self.step_n = 0
        self.grid = grid
        self.inventory = Inventory()
        self.ball = None
        self.exit = None

    def build_inventory(self):
        for pos, block in self.grid.items():
            if not block.locked:
                del self.grid[pos]
                self.inventory.add_block(block.__class__)

    def erase_block(self, pos):
        try:
            self.inventory.add_block(self.grid[pos].__class__)
            del self.grid[pos]
        except KeyError:
            pass

    def place_block(self, pos, block_class, use_inventory=True):
        self.erase_block(pos)
        if use_inventory:
            self.inventory.use_block(block_class)
        self.grid[pos] = block_class()

    def grid_size(self):
        return tuple(n + 1 for n in map(max, zip(*self.grid.keys())))

    def start(self):
        assert self.get_status() == Game.STATUS_NEW, "game has been started already"

        ball = None
        exit = None
        portals = set()

        for pos, block in self.grid.items():
            if isinstance(block, Launcher):
                # TODO Shove balls into a list: there are may be multiple launchers, and thus balls
                if ball is not None:
                    raise Exception("must be a single launcher")
                ball = Ball(direction=block.direction, pos=pos)
            elif isinstance(block, Exit):
                if exit is not None:
                    raise Exception("must be a single exit")
                exit = block
            elif isinstance(block, Portal):
                portals.add(pos)

        if ball is None:
            raise Exception("no launcher found")
        if len(portals) == 1:
            raise Exception("single portal is non-sense")

        self.ball = ball
        self.exit = exit
        for pos in portals:
            self.grid[pos].other_portals = tuple(portals - set((pos,)))

        self._update_exit()

    def get_status(self):
        if not self.ball:
            return Game.STATUS_NEW
        elif self.ball.status == Ball.STATUS_LEFT:
            return Game.STATUS_VICTORY
        elif self.ball.status == Ball.STATUS_DEAD:
            return Game.STATUS_DEFEAT
        else:
            return Game.STATUS_ON

    def step(self):
        state = self.get_status()
        assert state != Game.STATUS_NEW, "game has not been started"
        if state != Game.STATUS_ON:
            return state

        keep_moving = True
        while keep_moving:
            self.ball.move()

            block = self.grid.get(self.ball.pos)
            if block:
                keep_moving = block.act(self.ball)
            else:
                keep_moving = False

            self._update_exit()

        self.step_n += 1

        return state

    def _update_exit(self):
#.........这里部分代码省略.........
开发者ID:vickenty,项目名称:ookoobah,代码行数:103,代码来源:core.py


注:本文中的inventory.Inventory.add_block方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。