【问题标题】:Turtle Module for Python - How would I make the turtle do an action according to a stamped color underneath it?Python 的海龟模块 - 我如何让海龟根据它下面的标记颜色做一个动作?
【发布时间】:2021-01-26 13:07:54
【问题描述】:

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

【问题讨论】:

    标签: python turtle-graphics python-turtle


    【解决方案1】:

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

    输出:

    【讨论】:

    • 哇,看起来你真的花了一些时间在这上面。谢谢你! :)