【发布时间】:2018-11-03 04:04:13
【问题描述】:
import Labyrinthe
laby = Labyrinthe.creer(9,13)
此代码将创建以下列表数组:
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 2, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 1, 0, 1, 0]
[0, 1, 1, 1, 1, 1, 0, 1, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 0, 0, 0, 0, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 1, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0]
[0, 1, 0, 1, 0, 1, 1, 1, 0]
[0, 1, 0, 1, 0, 0, 0, 1, 0]
[0, 1, 1, 1, 1, 1, 0, 3, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
在哪里,
- '0' - 是一堵墙
- '1' - 是路径
- '2' - 起始图块
- '3' - 目标图块
我使用以下代码将像素写入 .pgm 文件并将它们的颜色设置为 0 到 255 之间的色调,其中 0(100% 白色)是最浅的色调, 255 是最暗的(100% 黑色)。
size = 20 #size of a tile in pixels
rows = len(laby)
columns = len(laby[0])
height = size * rows
width = size * columns
f = open("laby.pgm", "w")
f.write("P2\n" + str(width) + " " + str(height) + "\n255\n")
for y in range(height):
for x in range(width):
indx = x // size
indy = y // size
a = laby[indy][indx]
if a == 0:
f.write(str(50) + " ") # colors the pixels
elif a == 2:
f.write(str(100) + " ")
elif a == 3:
f.write(str(170) + " ")
else:
f.write(str(a) + " ")
f.close()
上面的代码将输出如下插入的图像:
我需要什么代码来指示计算机为通向目标的图块着色?
【问题讨论】:
标签: python python-3.x list pgm