Posts Tagged ‘PixelRobots’

XNA Pixel Robots library

Wednesday, January 20th, 2010

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


He has invented a way of generating simple, random robot-like or spaceship-like sprites. He has implemented his code in Java, and you can try out an applet there that will generate many different versions of the millions and millions of possible variations.
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.

Before you go any further I suggest you go and read about PixelRobots and PixelSpaceships, so you understand what the basic functionality of the API is.
You don't have to understand the internals of my API to use it, 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:

using System.Drawing;
using PixelRobots;

namespace XNAFAN.Net
{
    class Sample
    {
        public void Main()
        {
            //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);
spritesheet.Save(@"C:\spritesheet.png");
spritesheet.Dispose();

Samples spaceships

And then if you need to convert the Bitmaps to Texture2D runtime, it can be done using Cristopher M. Park's excellent method:

//Converts a Bitmap to a Texture2D
//Code found here:
//http://christophermpark.blogspot.com/2008/10/converting-systemdrawingbitmap-to-xna.html
Texture2D BitmapToTexture2D(Bitmap bmp)
{
    Texture2D tx = null;
    using (MemoryStream s = new MemoryStream())
    {
        bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png);
        s.Seek(0, SeekOrigin.Begin); //must do this, or error is thrown in next line
        tx = Texture2D.FromFile(GraphicsDevice, s);
    }
    return tx;
}

Code
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.

TestingPixelRobotsInXNA

Screenshot from the running TestingPixelRobotsInXNA project

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)