【问题标题】:python: langton's ant pygame first step issuespython: langton's ant pygame 第一步问题
【发布时间】:2015-07-29 02:44:09
【问题描述】:

我正在尝试绘制 Langton 蚂蚁算法的图形表示,但我卡住了。

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

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

【问题讨论】:

    标签: python algorithm python-2.7 pygame


    【解决方案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"
    

    【讨论】:

    • @LiamGiannini:查看更新后的答案。还有另一个小错误...屏幕应该初始化为(255, 255, 255)
    • @LiamGiannini:您可能在修改问题中的代码时犯了一些其他错误。我在更新答案之前对其进行了测试并且可以正常工作,您可以检查从raksy.dyndns.org/ant.py下载我的修改版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多