Posts Tagged ‘PixelRobots’

XNA Pixel Robots library

Wednesday, January 20th, 2010

A while ago I came across Dave Bollinger's PixelRobots and PixelSpaceships:

Unfortunately his website is gone now :-(, but his work has not been in vain, as this C# version lives on :).

His website is still available on the WayBackMachine, so you can go look at the algorithm for pixel robots and pixel spaceships. In case they ever disappear, here's a zipped PDF print.

Dave, if you're out there, I hope you don't object to me storing your lost treasure for the rest of the world to enjoy your creation :)

He has invented a way of generating simple, random robot-like or spaceship-like sprites. His code will generate many different versions of the millions and millions of possible variations of robots and spaceships. I really liked the idea, and thought that it would be very nice to have an XNA implementation for anyone who needed generic spaceships or robots in a game. So I created an XNA version from his description.

You don't have to understand the internals of neither Dave's code or my API to use this code, as everything is wrapped up in simple methods. But all the helpermethods and variables are available for use if you want to create something more advanced.

It can be as simple as this:

//create two bitmaps scaled by 5 with different colors
Bitmap spaceship = PixelMaskGenerator.GetCompletedRandomSpaceshipImage(5, Color.CornflowerBlue);
Bitmap robot = PixelMaskGenerator.GetCompletedRandomRobotImage(5, Color.LightGreen);

The above code would generate the following two images:

If you'd rather generate SpriteSheets and then use them as Content files instead of creating the spaceships runtime there's also support for that. The spritesheet below was created with the following code:

//create spritesheet
//scaled 3 times, 10 rows and 10 columns
//using spaceships in CornFlowerBlue
Bitmap spritesheet =
PixelMaskGenerator.GenerateRandomSample(3, 10, PixelMaskType.SpaceShip, Color.CornFlowerBlue);

Samples spaceships

And then if you need to convert the Bitmaps to Texture2D runtime, it can be done in XNA 4.0 with Florian Block's code:

//Converts a Bitmap to a Texture2D
//Code found here:
//<a href=""></a>

private Texture2D BitmapToTexture2D(GraphicsDevice dev, System.Drawing.Bitmap bmp)
    Texture2D customTexture = new Texture2D(this.GraphicsDevice, bmp.Width, bmp.Height);
    BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

    // calculate the byte size: for PixelFormat.Format32bppArgb (standard for GDI bitmaps) it's the hight * stride
    int bufferSize = data.Height * data.Stride; // stride already incorporates 4 bytes per pixel

    // create buffer
    byte[] bytes = new byte[bufferSize];

    // copy bitmap data into buffer
    Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);

    // copy our buffer to the texture

    // unlock the bitmap data

    return customTexture;

The Download code is available here.
The solution includes three projects:

  • PixelRobots is the main project is the project which contains all the code needed to generate SpaceShips, Robots and SpinedRobots (robots that are ensured a cohesive spine) for your game.
  • ConsoleTester is a consoleproject which goes through step-by-step what is done behind the curtains. It saves the generated images to disk and displays them in an HTML page.
  • TestingPixelRobotsInXNA is a little XNA demo which lets you generate spaceships with the left mousebutton and robots with the right mousebutton.


I made a short video presenting the API in use in TestingPixelRobotsInXNA:

Hope it is of use to somebody - it was fun making :)
If you use it for something I'd love to see for what.

More links
Want your own PixelRobot Tee?
Want the code in PHP for your website?
Want to see the PixelRobot idea used in a windowsgame? (non-XNA)