Last active
August 29, 2015 14:19
-
-
Save trlewis/458fe82f6202fd169411 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'travisl' | |
# This is an implementation of Langton's ant, written in Python using SFML | |
# Wikipedia article: http://en.wikipedia.org/wiki/Langton%27s_ant | |
import sfml as sf | |
UP = 0 | |
RIGHT = 1 | |
DOWN = 2 | |
LEFT = 3 | |
# Window / tile sizes | |
SIZE = 4 | |
WIDTH = 300 | |
HEIGHT = 300 | |
class Langton: | |
def __init__(self): | |
self.win = sf.RenderWindow(sf.VideoMode(WIDTH, HEIGHT), "Langton's Ant") | |
self.win.display() | |
self.paused = False | |
self.finished = False | |
self.is_running = True | |
self.step = False | |
self.win.show() | |
self.ant_x = (WIDTH//SIZE)//2 | |
self.ant_y = (HEIGHT//SIZE)//2 | |
self.ant_dir = RIGHT | |
self.ant_rekt = sf.RectangleShape() | |
self.ant_rekt.position = ((SIZE * self.ant_x) + 1, (SIZE * self.ant_y) + 1) | |
self.ant_rekt.size = (SIZE/2, SIZE/2) | |
self.ant_rekt.fill_color = sf.Color.RED | |
# create grid | |
self.tiles = [] | |
for y in range(HEIGHT//SIZE): | |
row = [] | |
for x in range(WIDTH//SIZE): | |
rekt = sf.RectangleShape() | |
rekt.position = (SIZE * x, SIZE * y) | |
rekt.size = (SIZE, SIZE) | |
rekt.fill_color = sf.Color.WHITE | |
row.append(rekt) | |
self.tiles.append(row) | |
def run(self): | |
while self.is_running: | |
self.handle_events() | |
if not self.paused and not self.finished: | |
self.update() | |
elif self.step: | |
self.step = False | |
self.update() | |
self.draw() | |
self.win.close() | |
def update(self): | |
rekt = self.tiles[self.ant_y][self.ant_x] | |
is_white = rekt.fill_color == sf.Color.WHITE | |
rekt.fill_color = sf.Color.BLACK if is_white else sf.Color.WHITE | |
# turn right on white, left on black, then move forward one unit | |
if self.ant_dir == UP: | |
self.ant_dir = RIGHT if is_white else LEFT | |
elif self.ant_dir == RIGHT: | |
self.ant_dir = DOWN if is_white else UP | |
elif self.ant_dir == DOWN: | |
self.ant_dir = LEFT if is_white else RIGHT | |
elif self.ant_dir == LEFT: | |
self.ant_dir = UP if is_white else DOWN | |
# move ant | |
if self.ant_dir == UP: | |
self.ant_y -= 1 | |
elif self.ant_dir == RIGHT: | |
self.ant_x += 1 | |
elif self.ant_dir == DOWN: | |
self.ant_y += 1 | |
elif self.ant_dir == LEFT: | |
self.ant_x -= 1 | |
self.ant_rekt.position = ((SIZE * self.ant_x) + 1, (SIZE * self.ant_y) + 1) | |
if self.ant_x < 0 or self.ant_x >= WIDTH//SIZE or self.ant_y < 0 or self.ant_y >= HEIGHT//SIZE: | |
self.finished = True | |
def handle_events(self): | |
for event in self.win.events: | |
if type(event) is sf.CloseEvent: | |
self.is_running = False | |
if type(event) is sf.KeyEvent and event.pressed: | |
if event.code == sf.Keyboard.ESCAPE: | |
self.is_running = False | |
if event.code == sf.Keyboard.SPACE: | |
self.paused = not self.paused | |
if event.code == sf.Keyboard.RIGHT and self.paused: | |
self.step = True | |
def draw(self): | |
self.win.clear(sf.Color.WHITE) | |
# draw board | |
for y in range(HEIGHT//SIZE): | |
for x in range(WIDTH//SIZE): | |
tile = self.tiles[y][x] | |
self.win.draw(tile) | |
self.win.draw(self.ant_rekt) | |
self.win.display() | |
if __name__ == '__main__': | |
l = Langton() | |
l.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment