Controlling Our Character

As we mentioned earlier, the Ready Set STEM CREATOR Kit has an enhanced Minecraft API (mcpi) that allows us to control our Minecraft character (Steve) and manipulate the Minecraft world. Normally, we move Steve around the Minecraft world pressing keys on the keyboard (for example, he moves forward when you press the "W" key). One of the things the API allows us to do is to simulate keypresses — that is, our program can make Minecraft think that the "W" key is being pressed, and Steve will move forward exactly as if the key were actually pressed.

This functionality comes in handy when we want to control the movement of Steve when something happens — for example, when a button is pressed on the CREATOR Kit!

Below, we'll discuss the major actions that our character can perform in the game, and how we can remap those actions to things other than the standard keyboard presses.

Control API

All of the character control functions are accessed from the control module, which we can import with:

Once the control module is imported, we can control Steve's movements with the functions forward(), backward(), left() and right(). Calling those functions without arguments is just like pressing and holding the keyboard key that normally invokes this movement. The "key" is not released until you call the function with the argument release=True.

For example, to have Steve walk forward for 2 seconds and then stop, this is what the code would look like:

This is what our code snippet does:

Another way to implement this same functionality (having a key pressed for a specific amount of time) is by using the duration= argument inside the function. Similar to above, the following code will have Steve walk forward for 2 seconds and then stop:

This one line of code achieves the same functionality as the three lines of code above.

Finally, to stop all keys from being pressed, you can use control.stop(). For example, to walk both forward and right for two seconds, and then stop, our code might look like this:

In addition to controlling Steve's movement forward, backwards, left and right, we also have the ability to control his other actions.

One of the main things that Steve does in Minecraft is breaking (smashing) blocks. This is typically accomplished using the left mouse button. Using the same control module we've been using, we can have Steve smash whatever is in front of him using the following code:

Additionally, Steve has the ability to jump, which is typically accomplished using the spacebar on the keyboard, and can be implemented in our code like this:

Current Keyboard/Mouse Mappings

Because the control functions simply poll the keyboard and mouse presses, there are many additional control commands that we can implement in our code — in fact, there is one for every keypress supported by Minecraft.

KEY

FUNCTION

S

backward()

W

forward()

A

left()

D

right()

SPACE

jump()

SHIFT

crouch()

Left Mouse Click

smash()

Right Mouse Click

place()

Right Mouse Click

hit()

1..8 (choice)

item(choice)

ENTER

enter()

E

inventory()

For more information on the rstem.mcpi API, check out the API documentation linked from the RDE above.

Look Out (And Around)

While simulating the keyboard keystrokes are important for moving Steve around, to fully control Steve, we need to be able to simulate the mouse as well. Mouse movements make Steve look around, and you can simulate them with the control API function look().

To use look() you need to provide an amount that you want to move the mouse (and therefore Steve) left, right, up, and down.

For example, to turn Steve left a little bit, try:

And, to look left a lot:

You'll have to experiment to see what works best for you depending upon how you are trying to move Steve — 0 won't turn Steve at all, numbers close to 1 will turn him a little, and once you get somewhere between 100 and 1000, you'll end up turning him completely around.

Besides looking left, Steve can look up, down, and right as well. For example, to have Steve look far up, we can try:

Finally, we can combine the actions of looking in different directions at the same time:
Of course, looking in opposite directions (up and down or left and right) at the same time by the same amount won't do anything. For example, this does nothing:

Finally, negative values work, too! A negative value for left is the same as a positive value for right. And, the same is true for up and down. So, the following lines to exactly the same thing: