The Minecraft API on the Raspberry Pi doesn't have a direct way to determine which direction Steve is facing (this is just a limitation of the Minecraft API on this platform). Unfortunately, this makes it essentially impossible to create code that allows you to modify the Minecraft world relative to what you are seeing in front of you on the screen. For example, without being able to programmatically tell what direction you're facing, you can't write a program that says to do something directly in front of (or next to, or behind) your character.
For that reason, we've included several APIs in the Ready Set STEM API that will allow us to get the direction Steve is facing, and will therefore allow us to implement additional features that we couldn't get with the Minecraft API alone. Note that to use the functions below, we are doing some behind-the-scenes trickery with Minecraft, and because of that, you will to see the screen briefly go blank (up to a half second) when you call these functions — that's the result of what we have to do to get these APIs to work.
get_heading()
The get_heading()
function returns the angle you are pointing
relative to the Z axis:
When we run this program, we'll see the screen go black briefly, about
once a second. This is because we're continually calling the
get_heading()
function (in a real application, you likely won't
be calling the function very often, so this won't be a major issue). In the
Program Output window, we will see the angle printed each time through the
loop.
Try turning Steve around to face in different directions and watch the angle change.
get_direction()
The get_direction()
function returns provides two angles -
the first angle is your heading, similar to the angle provided by
get_heading()
. The second angle is the angle Steve is looking in
the Y axis (up or down). This function uses a different method to determine
the angle, which is not as precise as get_heading(). So, if you want a more
precise heading (relative to just the Z axis), use the
get_heading()
function: