本文整理汇总了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):
#.........这里部分代码省略.........