Python 的海龟模块 - 我如何让海龟根据其下方的标记颜色执行操作?
Turtle Module for Python - How would I make the turtle do an action according to a stamped color underneath it?
我在 Python 中创建了 Langtons Ant,我想在 window 周围创建一个边界,这样“蚂蚁”就无法移动或打印到外部。我该怎么做(我已经用邮票画了一个盒子)。
Langton 的 Ant 序列永远不会结束,因此不可能在海龟图形中包含整个图像 window。
如果您打算让蚂蚁完成固定数量的步数,那么您可以使用这个 class 来模拟 Langton 的 Ant 序列,
和 returns 您需要的网格尺寸:
import turtle
SCREEN_WIDTH = 600 # You can customize this
SCREEN_HEIGHT = 600 # You can customize this
steps = 1000 # You can customize this
class LangtonsAnt:
def __init__(self, steps):
self.grid = [["AW"]]
self.facing = 0
self.steps = steps
def current_index(self):
for i, row in enumerate(self.grid):
for j, cell in enumerate(row):
if "A" in cell:
return i, j
def switch_color(self, i, j):
if "W" in self.grid[i][j]:
self.grid[i][j] = "B"
else:
self.grid[i][j] = "W"
def turn_cw(self):
self.facing += 1
if self.facing > 3:
self.facing = 0
def turn_ccw(self):
self.facing -= 1
if self.facing < 0:
self.facing = 3
def forward(self):
i, j = self.current_index()
self.switch_color(i, j)
if self.facing == 0:
if len(self.grid[i]) > j + 1:
self.grid[i][j + 1] = "A" + self.grid[i][j + 1]
else:
self.grid[i].append("AW")
for idx, row in enumerate(self.grid):
if idx != i:
row.append("W")
elif self.facing == 1:
if len(self.grid) > i + 1:
self.grid[i + 1][j] = "A" + self.grid[i + 1][j]
else:
self.grid.append(["AW" if idx == j else "W" for idx in range(len(self.grid[i]))])
elif self.facing == 2:
if j:
self.grid[i][j - 1] = "A" + self.grid[i][j - 1]
else:
self.grid[i] = ["AW"] + self.grid[i]
for idx, row in enumerate(self.grid):
if idx != i:
row.insert(0, "W")
elif self.facing == 3:
if i:
self.grid[i - 1][j] = "A" + self.grid[i - 1][j]
else:
self.grid.append(["AW" if idx == j else "W" for idx in range(len(self.grid[i]))])
def current_color(self):
for row in self.grid:
for cell in row:
if "A" in cell:
return cell[1]
def dimensions(self):
for _ in range(self.steps):
self.forward()
if self.current_color() == "W":
self.turn_cw()
else:
self.turn_ccw()
return len(self.grid[0]), len(self.grid)
wn = turtle.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
test = LangtonsAnt(steps)
turtle.speed("fastest")
turtle.shape("square")
s = min(SCREEN_WIDTH, SCREEN_HEIGHT) / max(test.dimensions())
turtle.shapesize(s / 20, s / 20)
turtle.penup()
for i, v in enumerate(test.grid):
for j, w in enumerate(v):
turtle.goto((-SCREEN_WIDTH + s)/ 2 + j * s, (SCREEN_HEIGHT - s) / 2 - i * s)
if "B" in w:
turtle.stamp()
输出:
我在 Python 中创建了 Langtons Ant,我想在 window 周围创建一个边界,这样“蚂蚁”就无法移动或打印到外部。我该怎么做(我已经用邮票画了一个盒子)。
Langton 的 Ant 序列永远不会结束,因此不可能在海龟图形中包含整个图像 window。
如果您打算让蚂蚁完成固定数量的步数,那么您可以使用这个 class 来模拟 Langton 的 Ant 序列, 和 returns 您需要的网格尺寸:
import turtle
SCREEN_WIDTH = 600 # You can customize this
SCREEN_HEIGHT = 600 # You can customize this
steps = 1000 # You can customize this
class LangtonsAnt:
def __init__(self, steps):
self.grid = [["AW"]]
self.facing = 0
self.steps = steps
def current_index(self):
for i, row in enumerate(self.grid):
for j, cell in enumerate(row):
if "A" in cell:
return i, j
def switch_color(self, i, j):
if "W" in self.grid[i][j]:
self.grid[i][j] = "B"
else:
self.grid[i][j] = "W"
def turn_cw(self):
self.facing += 1
if self.facing > 3:
self.facing = 0
def turn_ccw(self):
self.facing -= 1
if self.facing < 0:
self.facing = 3
def forward(self):
i, j = self.current_index()
self.switch_color(i, j)
if self.facing == 0:
if len(self.grid[i]) > j + 1:
self.grid[i][j + 1] = "A" + self.grid[i][j + 1]
else:
self.grid[i].append("AW")
for idx, row in enumerate(self.grid):
if idx != i:
row.append("W")
elif self.facing == 1:
if len(self.grid) > i + 1:
self.grid[i + 1][j] = "A" + self.grid[i + 1][j]
else:
self.grid.append(["AW" if idx == j else "W" for idx in range(len(self.grid[i]))])
elif self.facing == 2:
if j:
self.grid[i][j - 1] = "A" + self.grid[i][j - 1]
else:
self.grid[i] = ["AW"] + self.grid[i]
for idx, row in enumerate(self.grid):
if idx != i:
row.insert(0, "W")
elif self.facing == 3:
if i:
self.grid[i - 1][j] = "A" + self.grid[i - 1][j]
else:
self.grid.append(["AW" if idx == j else "W" for idx in range(len(self.grid[i]))])
def current_color(self):
for row in self.grid:
for cell in row:
if "A" in cell:
return cell[1]
def dimensions(self):
for _ in range(self.steps):
self.forward()
if self.current_color() == "W":
self.turn_cw()
else:
self.turn_ccw()
return len(self.grid[0]), len(self.grid)
wn = turtle.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
test = LangtonsAnt(steps)
turtle.speed("fastest")
turtle.shape("square")
s = min(SCREEN_WIDTH, SCREEN_HEIGHT) / max(test.dimensions())
turtle.shapesize(s / 20, s / 20)
turtle.penup()
for i, v in enumerate(test.grid):
for j, w in enumerate(v):
turtle.goto((-SCREEN_WIDTH + s)/ 2 + j * s, (SCREEN_HEIGHT - s) / 2 - i * s)
if "B" in w:
turtle.stamp()
输出: