不限用什么语言,python方便些,后期可以封装为exe的最好
我瞧瞧,稍等
望采纳!!!点击回答右侧采纳即可
确定坐标系中的点坐标后,可以根据坐标的变化来判断方向,然后打印对应的提示信息。
代码如下:
coordinates = ["F2", "F4", "D4", "B4", "B6"]
for i in range(len(coordinates) - 1):
current_coord = coordinates[i]
next_coord = coordinates[i+1]
x1, y1 = ord(current_coord[0]) - ord('A') + 1, int(current_coord[1])
x2, y2 = ord(next_coord[0]) - ord('A') + 1, int(next_coord[1])
if x1 == x2 and y1 < y2:
print("前进")
elif x1 == x2 and y1 > y2:
print("后退")
elif x1 < x2 and y1 == y2:
print("右转前进")
elif x1 > x2 and y1 == y2:
print("左转前进")
else:
print("拐弯前进")
可以这么做:
首先,我们定义一个转化函数,将坐标转化为纯数字坐标。
def convert_coord(coord):
x = ord(coord[0]) - ord('A') + 1
y = int(coord[1])
return [x, y]
然后,我们使用一个 while 循环来遍历每个坐标。
coords = [['F', 2], ['F', 4], ['D', 4], ['B', 4], ['B', 6]]
start = convert_coord(coords[0])
end = convert_coord(coords[-1])
curr = start
while curr != end:
# 在这里,我们处理坐标变化并打印信息
第三步,我们计算当前坐标和目标坐标的差值。
dx = end[0] - curr[0]
dy = end[1] - curr[1]
然后,我们根据差值的正负来判断前进方向。
if dx > 0:
print("向右前进")
curr[0] += 1
elif dx < 0:
print("向左前进")
curr[0] -= 1
if dy > 0:
print("向上前进")
curr[1] += 1
elif dy < 0:
print("向下前进")
curr[1] -= 1
最后,我们判断是否需要转弯。
if curr[0] != start[0] and curr[1] != start[1]:
if curr[0] > start[0]:
print("向右转")
else:
print("向左转")
start = curr
完整代码如下:
# -*- coding: UTF-8 -*-
welcome = "\n\tWelcome to Moving map:\n"
height = 7
width = 7
# 坐标信息
startPoint = "F2"
endPoint = "B6"
wayPoints = ["F2", "F4", "D4", "B4", "B6"]
# 初始化地图
positions = [[0 for i in range(width)] for i in range(height)]
# 起点的横、纵坐标
startCol = ord(startPoint[0]) - 65
startRow = int(startPoint[1]) - 1
positions[startRow][startCol] = "S"
# 记录上一个坐标
lastCol = startCol
lastRow = startRow
# 遍历路线上的各个点
for i in range(1, len(wayPoints)):
# 当前坐标
curPoint = wayPoints[i]
curCol = ord(curPoint[0]) - 65
curRow = int(curPoint[1]) - 1
if curCol == lastCol and curRow > lastRow: # 前进
diff = curRow - lastRow
for k in range(diff):
positions[lastRow+k+1][curCol] = "{}".format(k+1)
print("前进{}米".format(k+1))
elif curCol == lastCol and curRow < lastRow: # 后退
diff = lastRow - curRow
for k in range(diff):
positions[lastRow-k-1][curCol] = "-{}".format(k+1)
print("后退{}米".format(k+1))
elif curCol > lastCol and curRow == lastRow: # 右移
diff = curCol - lastCol
for k in range(diff):
positions[curRow][lastCol+k+1] = "{}".format(k+1)
print("右移{}米".format(k+1))
elif curCol < lastCol and curRow == lastRow: # 左移
diff = lastCol - curCol
for k in range(diff):
positions[curRow][lastCol-k-1] = "-{}".format(k+1)
print("左移{}米".format(k+1))
elif curCol > lastCol and curRow > lastRow: # 右转前进
diff = curCol - lastCol
for k in range(diff):
positions[curRow][lastCol+k+1] = "{}".format(k+1)
diff = curRow - lastRow
for k in range(diff):
positions[startRow+k+1][curCol] = "{}".format(k+1)
print("右转前进{}米".format(k+1))
elif curCol > lastCol and curRow < lastRow: # 右转后退
diff = curCol - lastCol
for k in range(diff):
positions[curRow][lastCol+k+1] = "{}".format(k+1)
diff = lastRow - curRow
for k in range(diff):
positions[startRow-k-1][curCol] = "-{}".format(k+1)
print("右转后退{}米".format(k+1))
elif curCol < lastCol and curRow > lastRow: # 左转前进
diff = lastCol - curCol
for k in range(diff):
positions[curRow][lastCol-k-1] = "-{}".format(k+1)
diff = curRow - lastRow
for k in range(diff):
positions[startRow+k+1][curCol] = "{}".format(k+1)
print("左转前进{}米".format(k+1))
elif curCol < lastCol and curRow < lastRow: # 左转后退
diff = lastCol - curCol
for k in range(diff):
positions[curRow][lastCol-k-1] = "-{}".format(k+1)
diff = lastRow - curRow
for k in range(diff):
positions[startRow-k-1][curCol] = "-{}".format(k+1)
print("左转后退{}米".format(k+1))
lastCol = curCol
lastRow = curRow
positions[lastRow][lastCol] = "E"
# 打印地图及结果
print(welcome)
for pos in positions:
print(pos)
print("\n{} -> {}".format(startPoint, endPoint))
这样就可以实现根据坐标移动并打印的功能了。希望这些思路能够帮助你。