Python: langton ant problems with first pygame call

I'm trying to make a graphical representation of the Langton ant algorithm, but I'm stuck.

here is a description of Langton ant

What should be the output:

Animation of first 200 steps of Langton's ant

My code:

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"

      

When you start ant goes to the top left corner and then the program stops. I really don't understand why this doesn't work. Are there any hints on how to fix this?

+3


source to share


1 answer


You need to draw first and then update the coordinates.

Instead, your code first updates the coordinates and then draws (meaning that the next move will not depend on the color you moved).

The difference is important because, when done correctly, the next step instead depends on the color that ant is placed in and the direction from ant; in your version, except for the first move, the board color is never used, because you draw a square and then create if

in your content (the color you just used).

You need to change your code from

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

      

replacing two lines to get



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

      

in all places you update the coordinates.

Another mistake is that the screen must be filled with white (255,255,255)

at the start of the simulation (in the current code it is used instead (0, 0, 0)

).

With these changes, the simulation works just like in the video.

Note that you can use a different approach that scales much better for more complex cases (more than two colors). The idea is to keep the array directions

and index for the current one direction

; so to turn left or right you just need to increase or decrease the value direction

(mod 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"

      

+1


source







All Articles