本文整理汇总了Python中ship.Ship.squares方法的典型用法代码示例。如果您正苦于以下问题:Python Ship.squares方法的具体用法?Python Ship.squares怎么用?Python Ship.squares使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ship.Ship
的用法示例。
在下文中一共展示了Ship.squares方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: populate_bg
# 需要导入模块: from ship import Ship [as 别名]
# 或者: from ship.Ship import squares [as 别名]
def populate_bg(self, bg):
"""
Populate a battleground with random ships.
:param bg: the battleground to populate
"""
it = SHIP_LENS.__iter__()
ship_len = it.next()
while True:
#for ship_len in SHIP_LENS:
ship = Ship(ship_len)
direction = random.randint(0, 1) # 0 = vertical, 1 = horizontal
if direction == 0:
start_x = random.randint(0, NUM_SQUARES - 1)
start_y = random.randint(0, NUM_SQUARES - 1 - ship_len)
elif direction == 1:
start_x = random.randint(0, NUM_SQUARES - 1 - ship_len)
start_y = random.randint(0, NUM_SQUARES - 1)
squares = []
for i in xrange(ship_len):
if direction == 0:
squares.append(Square(start_x, start_y + i, SquareState.intact))
elif direction == 1:
squares.append(Square(start_x + i, start_y, SquareState.intact))
ship.squares = squares
if bg.check_intersect(ship):
continue
bg.add_ship(ship)
# Python's fucked up way to check if an iterator reached the end of a container
try:
ship_len = it.next()
except:
break