In this project, we'll build on the sprite animations we did in the LED Matrix project by not only animating a running man on the LED Matrix, but also creating a synchronized animation of the running man in the Minecraft world. What we draw on the LED Matrix will be replicated in blocks in the Minecraft world.
This project will only require the LED Matrix display. Here is what your
breadboard should look like:
Our code for this project will be identical to our code in the project, with one addition — we'll create a function that will replicate the animation we're doing on the LED Matrix in the Minecraft world as well.
When you give this code a try, don't forget to look around to find the running man!
Here is what the full code for this project should look like. We'll dig further into the highlighted pieces of code below:
sprite_in_minecraft()
FunctionWe've created a function that will animate our character in the Minecraft world. Unlike the LED Matrix, where we can use a simple API to pass in an 8x8 design (a sprite) with a single function call, to create a design in the Minecraft world, we need to draw it block-by-block. For that reason, the bulk of our function is used to turn our 8x8 list of 0's and non-0's into blocks (for the non-0's) and air (for the 0's).
Let's discuss what our function does, line-by-line:
On Line 56, we define our function, which takes three arguments as inputs: our Minecraft object, the current 8x8 design that we want to draw and the location of our character (where we want to draw it).
On Line 57, we take our 8x8 design that we drew on our LED
Matrix and we rearrange it in an order that will allow us to draw it in the
Minecraft world. Specifically, we break up the design into the eight separate
lines (using the splitlines()
function) and then we
reverse the order of the lines from bottom to top (using the
reversed()
function), as we will draw starting from the ground
and moving up.
On Lines 58 and 59, we nest (put one inside the other) two
for
loops. This will allow us to run through each of the eight
horizontal characters and each of the eight rows and execute the code that
follows the nested loops.
On Lines 60 through 64, we have implemented the code that will be used to
create blocks at each of 64 (8x8) character positions. The code checks the character,
and if it's a 0 (which is equivalent to the LED being off in that position),
it sets the block type to AIR, which is the same as not drawing a block. If the
character is a non-0 (which is equivalent to the LED being on in that position),
it sets the block type to wood, so we'll see the block at that location. The final
line of that code block uses the setBlock()
function to draw the
appropriate block.
enumerate()
Function
In the two for
loops above, we use the
built-in enumerate
function. This function allows us to track the count
of how many times we've gone through the for
loop — we call
this the "index", it starts at 0 and it is stored in the first variable of
the loop.
Here's an example:
animals = ['dog', 'cat', 'bird']
for index, animal in (enumerate(animals)):
print(index, animal)
This code would output:
0 dog 1 cat 2 bird
We made the running man out of wood. Can you change the type of block that the running man is made of?
Can you make the running man out of stripes? That is, can you use different materials for the odd and even rows?
Can you make the running man out of randomly generated materials for each block?
We made the running man stand straight up in the Minecraft world (on the X and Y axes). Can you lay the running man down flat (on the X and Z axes)?