本文整理汇总了Python中ship.Ship.set_circuit方法的典型用法代码示例。如果您正苦于以下问题:Python Ship.set_circuit方法的具体用法?Python Ship.set_circuit怎么用?Python Ship.set_circuit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ship.Ship
的用法示例。
在下文中一共展示了Ship.set_circuit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from ship import Ship [as 别名]
# 或者: from ship.Ship import set_circuit [as 别名]
class GateField:
"""Game field with logic"""
def __init__(self, width, height, startpos, endpos, obstacles):
self.width = width
self.height = height
self.field = Field(width, height, obstacles)
self.ship = Ship(startpos)
self.finish = endpos
def _check_collision(self, coords):
check_horiz = fabs(coords[0] - trunc(coords[0])) > 0.05
check_vert = fabs(coords[1] - trunc(coords[1])) > 0.05
if check_horiz:
if check_vert:
return self.field.get_at(floor(coords[0]), floor(coords[1])) or \
self.field.get_at(floor(coords[0]), ceil(coords[1])) or \
self.field.get_at(ceil(coords[0]), floor(coords[1])) or \
self.field.get_at(ceil(coords[0]), ceil(coords[1]))
else:
return self.field.get_at(floor(coords[0]), trunc(coords[1])) or \
self.field.get_at(ceil(coords[0]), trunc(coords[1]))
else:
if check_vert:
return self.field.get_at(trunc(coords[0]), floor(coords[1])) or \
self.field.get_at(trunc(coords[0]), ceil(coords[1]))
else:
return self.field.get_at(trunc(coords[0]), trunc(coords[1]))
def _sensors_state(self):
circuit = {'s': False, 'w': False, 'n': False, 'e': False}
current_cell = (trunc(self.ship.coords[0]), trunc(self.ship.coords[1]))
circuit['s'] = self.field.get_at(current_cell[0], current_cell[1] + 1)
circuit['w'] = self.field.get_at(current_cell[0] - 1, current_cell[1])
circuit['n'] = self.field.get_at(current_cell[0], current_cell[1] - 1)
circuit['e'] = self.field.get_at(current_cell[0] + 1, current_cell[1])
return circuit
def step(self):
self.ship.move()
if self._check_collision(self.ship.coords):
self.ship.abort_move()
self.ship.set_circuit(self._sensors_state())
self.ship.process()