I'm creating a Space Invaders game, and graphics sprites in it are defined as 2D arrays of colors.

It seems like it's going to be cumbersome declaring these arrays the way I currently am:

//This creates an inverting red/blue cross animation
public Sprite myFirstAnimation()
    Sprite frame1 = new Sprite(new Color[,]
                    {{Color.Red, Color.Blue, Color.Red}, 
                        {Color.Blue, Color.Blue, Color.Blue},
                        {Color.Red, Color.Blue, Color.Red}});

    Sprite frame2 = new Sprite(new Color[,]
                    {{Color.Blue, Color.Red, Color.Blue}, 
                        {Color.Red, Color.Red, Color.Red},
                        {Color.Blue, Color.Red, Color.Blue}});

    AnimatedSprite myAnimatedSprite = new AnimatedSprite(new Sprite[] { frame1, frame2 });
    return myAnimatedSprite; 

Can you point me the the direction for a nicer way of doing this, as to make it more maintainable and readable, and less arduous?

  • 1
    \$\begingroup\$ ... It seems like, using a bit of code that will convert bitmaps into my 2d array is what I'm going to want. \$\endgroup\$
    – dwjohnston
    Apr 26, 2014 at 5:21
  • \$\begingroup\$ .... or just using the System.Drawing.Image.Bitmap class... \$\endgroup\$
    – dwjohnston
    Apr 26, 2014 at 8:15
  • \$\begingroup\$ Those sound like great ideas - go and implement them! \$\endgroup\$
    – Uri Agassi
    Apr 26, 2014 at 8:31

1 Answer 1


You're trying to store data in code. While that is not bad idea by itself, you shouldn't overuse it and I think that's what you're doing here.

If your data is bitmaps, store them in some bitmap format (BMP or maybe PNG) and then load them into your program from that. As an additional advantage, you will be able to edit your images using normal image editing software (like Paint), instead of having to edit your code.

If you want to keep having your bitmaps in code, you want to decrease the verbosity and you're willing to lose some type-safety, you could encode your bitmaps as strings and then transform them into 2D array using a helper function.

It could look something like:

Color[,] frame1 = FormatMatrix("010 111 010", Color.Red, Color.Blue);

Or even:

Color[,] frame1 = FormatMatrix(@"
010", Color.Red, Color.Blue);

The helper function would look something like this:

static T[,] FormatMatrix<T>(string format, params T[] values)
    var formatParts = format.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);

    T[,] result = new T[formatParts.Length, formatParts[0].Length];

    for (int i = 0; i < formatParts.Length; i++)
        var formatPart = formatParts[i];

        for (int j = 0; j < formatPart.Length; j++)
            if (formatPart[j] >= '0' && formatPart[j] <= '9')
                result[i,j] = values[formatPart[j] - '0'];
                throw new ArgumentException("format");

    return result;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.