首页 > 解决方案 > Python 的海龟模块 - 我如何让海龟根据它下面的标记颜色做一个动作?

问题描述

我用 Python 制作了Langtons Ant,我想在窗口周围创建一个周边,这样“蚂蚁”就不能在外面移动或打印。我该怎么做(我已经用邮票画了一个盒子)。

标签: pythonturtle-graphicspython-turtle

解决方案


Langton's Ant 序列永远不会结束,因此不可能将整个图像包含在海龟图形窗口中。

如果您打算允许蚂蚁执行固定数量的步骤,那么您可以使用模拟兰顿蚂蚁序列的此类,并返回您需要的网格尺寸:

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()

输出:

在此处输入图像描述


推荐阅读