In this project, we will create a sprite of a person (this is actually the same sprite used by Minecraft for their main character, Steve), and we will draw and animate that sprite on the LED Matrix.

Your breadboard should still have the circuit we wired in the project. As a reminder, here is what it should look like:

In this project, we will create a sprite, and we will animate that sprite. In order to animate the sprite, we will create multiple versions of the sprite, and we will display the different versions of the sprite on our LED Matrix, one at a time. If we rotate through the sprite images reasonably quickly, the sprites will appear as a continuous animation. In this case, the animation will be of a person running.

Here are the four sprites we will rotate through to create our animation:

If it's not completely obvious what the animation will look like based on those four sprite designs, just wait and see...

For our code, we will use the Game Loop format. First, we'll do all of our initialization and define our sprites, then in a loop, we will cycle through our sprites, erasing/drawing/displaying each sprite in the sequence.

Here is the code, which hopefully you'll find pretty straightforward at this point:

from rstem.led_matrix import FrameBuffer, Sprite import time fb = FrameBuffer() sprites = [ Sprite(""" 00000000 0000FF00 000FF000 000FFF00 00FFFF00 000FFF00 0FF00F00 0F0000FF """), Sprite(""" 00000000 0000FF00 0000F000 00FFFF00 0F0FF0F0 000FFF00 0FF00F00 00000FF0 """), Sprite(""" 00000000 0000FF00 000FF000 000FF000 000FF000 000FF000 00FFF000 000FF000 """), Sprite(""" 0000FF00 000FF000 000FFF00 00FFFF00 000FFF00 00F00F00 00F0F000 00F00000 """), ] which_sprite = 0 while True: # Get user input # Update the game world which_sprite = which_sprite + 1 if which_sprite > 3: which_sprite = 0 # Draw graphics on the screen fb.erase() fb.draw(sprites[which_sprite]) fb.show() # Wait for some period of time time.sleep(0.1)

Give it a try. You should see your animated sprite displayed on your LED Matrix.

1. Can you add a button that, when pressed, makes the running man jump? That is, displays him shifted up one or two points above the bottom of the display.

2. Can you add a second button that, when pressed, makes the running man go faster?

3. Can you add a third button that, when pressed, makes the running man flash on and off while still running?

4. Can you add a fourth button that, when pressed, makes the running man change direction?

5. Challenging: Can you modify the first button so that it makes the running man jump gradually? That is, as he continues to run, slowly display him shifted up one point, then two points, then three points, then two points, then one point, then back to ground?