patterncsharpMinor
Declaring a large number of 2D arrays to be used as 2D graphic sprites
Viewed 0 times
arraysnumberuseddeclaringgraphiclargesprites
Problem
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:
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?
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?
Solution
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:
Or even:
The helper function would look something like this:
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
111
010", Color.Red, Color.Blue);The helper function would look something like this:
static T[,] FormatMatrix(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 = '0' && formatPart[j] <= '9')
result[i,j] = values[formatPart[j] - '0'];
else
throw new ArgumentException("format");
}
}
return result;
}Code Snippets
Color[,] frame1 = FormatMatrix("010 111 010", Color.Red, Color.Blue);Color[,] frame1 = FormatMatrix(@"
010
111
010", Color.Red, Color.Blue);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'];
else
throw new ArgumentException("format");
}
}
return result;
}Context
StackExchange Code Review Q#48211, answer score: 3
Revisions (0)
No revisions yet.