python: langton's ant pygame 第一步问题

python: langton's ant pygame first step issues

我正在尝试制作兰顿蚂蚁算法的图形表示,但我被卡住了。

这里是 Langton's Ant video description

输出应该是这样的:

我的代码:

import pygame
width = 800
height = 600

display = display=pygame.display.set_mode((width,height))
pygame.init()

direction = "up"

pixel_width_height = 2
qg = pixel_width_height
x=400
y=300

display.fill((0,0,0))
pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    try:
        a = display.get_at((x, y))
    except:
        break
    if a == (255, 255, 255, 255) or a == (230, 230, 230, 255):
        if direction == "right":
            direction = "up"
            y=y-qg
            pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
        elif direction == "up":
            direction = "left"
            x = x-qg
            pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
        elif direction == "left":
            direction = "down"
            y=y+qg
            pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
        elif direction == "down":
            direction = "right"
            x = x+qg
            pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
    elif a == (255,0,0,255):
        if direction == "right":
            direction = "down"
            y=y+qg
            pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
        elif direction == "up":
            direction = "right"
            x = x+qg
            pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
        elif direction == "left":
            direction = "up"
            y = y-qg
            pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
        elif direction == "down":
            direction = "left"
            x=x-qg
            pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
    pygame.display.update()
print "end"

当它启动时,蚂蚁进入左上角,然后程序停止。 我真的不明白为什么它不起作用..关于如何修复它的任何线索?

需要先绘制再更新坐标。

您的代码首先更新坐标然后绘制(这意味着下一步移动将不取决于您移动到的颜色)。

区别很重要,因为如果正确完成,下一步取决于蚂蚁着陆的颜色和蚂蚁来自的方向;在您的版本中,除了第一步之外,从未使用过棋盘的颜色,因为您正在绘制正方形,然后在其内容上制作 if(这是您刚刚使用的颜色)。

您需要更改代码

        y=y-qg
        pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))

交换两条线得到

        pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
        y=y-qg

在所有地方你正在更新坐标。

另一个错误是在模拟开始时屏幕应该被白色(255,255,255)填充(当前代码使用(0, 0, 0)代替)。

通过这些更改,模拟就像视频中一样工作。

请注意,您可以使用不同的方法更好地适应更复杂的情况(超过两种颜色)。这个想法是保留一个 directions 的数组和一个指向当前 direction 的索引;这样左转或右转你只需要增加或减少direction(模4)。

import pygame
width, height = 800, 600
display = pygame.display.set_mode((width,height))
pygame.init()
qg = 2
x, y = 400, 300
display.fill((255,255,255))
directions = ((0, -1), (-1, 0), (0, 1), (1, 0))
direction = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    # update position
    dx, dy = directions[direction]
    x += dx * qg
    y += dy * qg

    try:
        a = display.get_at((x, y))
    except:
        break

    if a == (255, 255, 255, 255):
        # White square
        pygame.draw.rect(display,(255,0,0),(x,y,qg,qg)) # paint red
        direction = (direction + 1) % 4                 # turn left
    else:
        # Red square
        pygame.draw.rect(display,(255,255,255),(x,y,qg,qg)) # paint white
        direction = (direction + 3) % 4                     # turn right
    pygame.display.update()
print "end"