Schweinskopfsuelze on DeviantArthttps://www.deviantart.com/schweinskopfsuelze/art/Langton-s-ants-149437698Schweinskopfsuelze

Deviation Actions

Schweinskopfsuelze's avatar

Langton's ants

Published:
1.8K Views

Description

Langton what? -> [link]

Ages ago I've seen this on an old DOS machine and it was very fascinating. I've seen a Flash version of it and had to give it a try myself. Nothing special, just a tribute to the good old times.

13x13 ants

Enjoy.

Here's the Code for one worm, give them a bitmapdata as a home and they are happy =)

package
{
import flash.display.BitmapData;
import flash.events.Event;
import flash.display.Shape;
/**
* ...
* @author milchreis
*/
internal class Worm extends Shape
{
private var color:uint;
private var hole: BitmapData;
private var direction:uint;

public function Worm(p_bitmapdata: BitmapData, p_x:uint, p_y:uint, p_color:uint=0xffff00ff)
{
x = p_x;
y = p_y;
color = p_color;
hole = p_bitmapdata;
direction = Math.floor(Math.random()*4)+1;
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
// this is how the langton algorithm works, setting/unsetting pixels and changing direction.
if (hole.getPixel(x, y) == 0)
{
hole.setPixel(x, y, color);
direction += 1;
}
else
{
hole.setPixel(x, y, 0);
direction -= 1;
}

//making sure that we only have 4 directions and cycle through them
if (direction > 4)
{
direction = 1;
}
else if (direction < 1)
{
direction = 4;
}
//this moves the ant and projects the bitmapData on a torus, this way, no ant can be lost. =)
switch(direction)
{
case 1: y = (y == 0) ? hole.height-1 : (y - 1);
break;
case 2: x = (x == hole.width-1) ? 0 : (x + 1);
break;
case 3: y = (y == hole.height-1) ? 0 : (y + 1);
break;
case 4: x = (x == 0) ? hole.width-1 : (x - 1);
break;
}

}
}

}
Image size
600x600px 1.83 KB
© 2010 - 2024 Schweinskopfsuelze
Comments21
Join the community to add your comment. Already a deviant? Log In
wonderwhy-ER's avatar
Heh cellular automate huh :)
Would be cool if cells were say 4x4 or 8x8 pixel art of walls of different type. Like walls built by gnomes, humans, orcs, undead :D