public override void Run()
{
// A big thanks to Johan Vinet, pixel artist and animator, for the Mario animation! :-)
// http://johanvinet.tumblr.com/
// Twitter handle: @johanvinet
var imageUri = new Uri("ms-appx:///Assets/MiniMario.png");
// Get the pixels of the animation frames.
Color[,] pixels = PixelSupport.GetPixels(imageUri).Result;
// Create a sprite map from the pixels.
var spriteMap = new SpriteMap(pixels);
// Keep track of the animation frame...
int animationIndex = 0;
Sprite sprite = spriteMap.GetSprite(animationIndex);
// ...and when it's time to update it.
TimeSpan frameDuration = TimeSpan.FromMilliseconds(70);
DateTime nextAnimationUpdateTime = DateTime.Now.Add(frameDuration);
// Keep track of the location and orientation of the sprite.
int spriteX = 0;
int spriteY = 0;
DisplayDirection direction = DisplayDirection.Deg0;
bool flipHorizontal = false;
bool flipVertical = false;
while (true)
{
bool redrawSprite = false;
//Is it time to update the animation ?
if (DateTime.Now >= nextAnimationUpdateTime)
{
// Yes. The next time to update is:
nextAnimationUpdateTime = DateTime.Now.Add(frameDuration);
// Needs to redraw the sprite.
redrawSprite = true;
// Select the next sprite index.
animationIndex++;
if (animationIndex >= 6)
{
animationIndex = 0;
}
// Pick out the sprite.
sprite = spriteMap.GetSprite(animationIndex);
}
if (SenseHat.Joystick.Update()) // Has any of the buttons on the joystick changed?
{
UpdatePosition(ref spriteX, ref spriteY); // Move the sprite.
UpdateDrawingDirection(ref direction, ref flipHorizontal, ref flipVertical); // Re-orientate the sprite.
// Needs to redraw the sprite.
redrawSprite = true;
}
if (redrawSprite)
{
SenseHat.Display.Clear(); // Clear the screen.
// Draw the sprite.
sprite.Draw(SenseHat.Display, spriteX, spriteY, true, direction, flipHorizontal, flipVertical);
SenseHat.Display.Update(); // Update the physical display.
}
// Take a short nap.
Sleep(TimeSpan.FromMilliseconds(2));
}
}