本文整理汇总了Python中maze.Maze.dist_to_wall方法的典型用法代码示例。如果您正苦于以下问题:Python Maze.dist_to_wall方法的具体用法?Python Maze.dist_to_wall怎么用?Python Maze.dist_to_wall使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类maze.Maze
的用法示例。
在下文中一共展示了Maze.dist_to_wall方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1:
# 需要导入模块: from maze import Maze [as 别名]
# 或者: from maze.Maze import dist_to_wall [as 别名]
# set the robot in the start position. Note that robot position
# parameters are independent of the robot itself.
robot_pos = {'location': [init[0], init[1]], 'heading': 'up'}
run_active = True
hit_goal = False
while run_active:
# check for end of time
total_time += 1
if total_time > max_time:
run_active = False
print 'Allotted time exceeded.'
break
# provide robot with sensor information, get actions
sensing = [testmaze.dist_to_wall(robot_pos['location'], heading)
for heading in dir_sensors[robot_pos['heading']]]
rotation, movement = testrobot.next_move(sensing)
# render simulator
simulator.render()
# check for a reset
if (rotation, movement) == ('Reset', 'Reset'):
if run == 0 and hit_goal:
run_active = False
runtimes.append(total_time)
print 'Ending first run. Starting next run.'
break
elif run == 0 and not hit_goal:
print 'Cannot reset - robot has not hit goal yet.'
示例2: run
# 需要导入模块: from maze import Maze [as 别名]
# 或者: from maze.Maze import dist_to_wall [as 别名]
def run():
'''
This script tests a robot based on the code in robot.py on a maze given
as an argument when running the script.
'''
gui = False
if 3 <= len(sys.argv) and sys.argv[2] == '--gui':
gui = True
# Create a maze based on input argument on command line.
testmaze = Maze( str(sys.argv[1]) )
if gui:
pygame.init()
size = 50 * testmaze.dim + 100
screen = pygame.display.set_mode((size, size))
image = Image(testmaze.dim, screen)
thread = threading.Thread(target=pygame_loop)
thread.start()
else:
image = DummyImage()
# Intitialize a robot; robot receives info about maze dimensions.
game_objects.append(image)
testrobot = Robot(testmaze.dim, image)
# Record robot performance over two runs.
runtimes = []
total_time = 0
for run in range(2):
print "Starting run {}.".format(run)
# Set the robot in the start position. Note that robot position
# parameters are independent of the robot itself.
robot_pos = {'location': [0, 0], 'heading': 'up'}
run_active = True
hit_goal = False
while run_active:
total_time += 1
# check for end of time
if total_time > max_time:
run_active = False
print "Allotted time exceeded."
break
# provide robot with sensor information, get actions
sensing = [testmaze.dist_to_wall(robot_pos['location'], heading)
for heading in dir_sensors[robot_pos['heading']]]
rotation, movement = testrobot.next_move(sensing)
# check for a reset
if (rotation, movement) == ('Reset', 'Reset'):
if run == 0 and hit_goal:
run_active = False
runtimes.append(total_time)
print "Ending first run. Starting next run."
break
elif run == 0 and not hit_goal:
print "Cannot reset - robot has not hit goal yet."
continue
else:
print "Cannot reset on runs after the first."
continue
# perform rotation
if rotation == -90:
robot_pos['heading'] = dir_sensors[robot_pos['heading']][0]
elif rotation == 90:
robot_pos['heading'] = dir_sensors[robot_pos['heading']][2]
elif rotation == 0:
pass
else:
print "Invalid rotation value, no rotation performed."
# perform movement
if abs(movement) > 3:
print "Movement limited to three squares in a turn."
movement = max(min(int(movement), 3), -3) # fix to range [-3, 3]
while movement:
if movement > 0:
if testmaze.is_permissible(robot_pos['location'], robot_pos['heading']):
robot_pos['location'][0] += dir_move[robot_pos['heading']][0]
robot_pos['location'][1] += dir_move[robot_pos['heading']][1]
movement -= 1
else:
print "Movement stopped by wall."
movement = 0
else:
rev_heading = dir_reverse[robot_pos['heading']]
if testmaze.is_permissible(robot_pos['location'], rev_heading):
robot_pos['location'][0] += dir_move[rev_heading][0]
robot_pos['location'][1] += dir_move[rev_heading][1]
movement += 1
else:
print "Movement stopped by wall."
movement = 0
# check for goal entered
goal_bounds = [testmaze.dim/2 - 1, testmaze.dim/2]
if robot_pos['location'][0] in goal_bounds and robot_pos['location'][1] in goal_bounds:
hit_goal = True
#.........这里部分代码省略.........