python: langton's ant pygame first step issues
Question: I'm trying to make a graphic representation of the Langton's ant algorithm but
I'm stuck.

here's the [Langton's Ant video
description](https://www.youtube.com/watch?v=1X-gtr4pEBU)

That's what the output should be:

![Animation of first 200 steps of Langton's
ant](http://i.stack.imgur.com/oBjc6.gif)

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 it starts the ant goes in the upper left corner and then the program
stops. I really don't understand why it won't work.. how to fix it?


Answer: You need to first draw and then update the coordinates.

Your code instead first updates the coordinates and then draws (and this means
that the next move will not depend on the color you are moving to).

The difference is important because when done correctly instead next move
depends on the color the ant lands in and the direction the ant is coming
from; in your version except for the first move the color of the board is
never used because you're painting the square and then making an `if` on its
content (that's the color you just used).

You need to change the code from

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

swapping the two lines to get

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

**in all the places** you are updating the coordinates.

Another error is that the screen should be filled with white `(255,255,255)`
at the beginning of the simulation (current code uses `(0, 0, 0)` instead).

With these changes the simulation works like in the video.

Note that you could use a different approach that scales much better to more
complex cases (more than two colors). The idea is to keep an array of
`directions` and an index to the current `direction`; this way to turn left or
right you just need to increment or decrement the `direction` (modulo 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"
    

