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

标签 python algorithm python-2.7 pygame

我正在尝试制作 Langton Ant 算法的图形表示,但我被卡住了。

这是一个 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"

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

最佳答案

需要先绘制,再更新坐标。

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

区别很重要,因为正确完成下一步行动取决于 Ant 着陆的颜色和 Ant 来自的方向;在您的版本中,除了第一步之外,从未使用过棋盘的颜色,因为您正在绘制正方形,然后在其内容上制作 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"

关于python: langton's ant pygame 第一步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288910/

相关文章:

algorithm - 在位数组中查找缺失的数字

algorithm - 算法的效率

arrays - 数组练习

python - 我试图从 def 方程中获得整数返回

python - Postgres : unterminated quoted string at or near using psycopg2

Python - Google OAuth2 - token 中的段数错误

python - INSERT datetime variable INTO VALUES 的语法是什么

python - 如何从 Python 中的每个单词的右侧去除字符?

Python pandas 替换

python-2.7 - Python OpenCV:Python 2.7与Python 3.5之间的VideoCapture差异