python:langton的ant pygame第一步问题

时间:2015-05-17 15:58:05

标签: python algorithm python-2.7 pygame

我正在尝试对Langton的蚂蚁算法进行图形表示,但是我被卡住了。

这是一个Langton's Ant video description

这就是输出的样子:

Animation of first 200 steps of Langton's ant

我的代码:

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"

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

1 个答案:

答案 0 :(得分:1)

您需要先绘制然后更新坐标。

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

差异很重要,因为正确完成后,下一步的移动取决于蚂蚁所处的颜色和蚂蚁的来源方向;在您的版本中,除了第一步之外,从不使用棋盘的颜色,因为您正在绘制正方形,然后在其内容上创建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"