This post was originally published on the Wolfram Blog and has been edited slightly for Tech-Based Teaching. Enjoy!

The standard Raspbian software on the Raspberry Pi comes with a basic implementation of Minecraft and a full implementation of the Wolfram Language. Combining the two provides a fun playground for learning coding.

If you are a gamer, you can use the richness of the Wolfram Language to programmatically generate all kinds of interesting structures in the game world, or to add new capabilities to the game. If you are a coder, then you can consider Minecraft just as a fun 3D rendering engine for the output of your code.

Installation

The first step is to make sure that you have all the right components. Make sure that you have the latest version of Raspbian and the Wolfram Language. You do this by connecting your Raspberry Pi to the network, opening the Terminal app and typing the following:

Now open Mathematica on the Pi, or another computer, and type:

… followed by Shift + Return to evaluate it. If all went well, we are ready to start.

Using the Link

The MinecraftLink library adds a small set of new commands to the Wolfram Language for connecting to a running Raspberry Pi Minecraft game.

Start by launching Minecraft on the Raspberry Pi, and then start a fresh game or open an existing one. You must have a Minecraft game open before you can connect to it from the Wolfram Language. In the Wolfram Language, load the library by evaluating the following:

This extends the Wolfram Language with the following new commands:

You can find documentation on these by evaluating MinecraftHelp[] after you have installed the link.

You can control a Minecraft game running on the Raspberry Pi from the Wolfram Language running on the same Raspberry Pi, or from any other computer that has a network connection to the Pi. If you’re connecting from a different computer, you must now tell the Wolfram Language the name or IP address of the Raspberry Pi where Minecraft is running…

You don’t need to do this if both programs are on the same machine, but if you need to reset the connection, you can use MinecraftConnect[] or MinecraftConnect[“localhost”]. If you get a “Cannot connect” message, then the problem is either that you have the wrong IP address or no network connection to the Pi, or you forgot to start a Minecraft game first.

Let’s test to see if that worked by evaluating the following code:

You should see the message appear briefly in the game chat area:

We need to find out where we are in the Minecraft world. Minecraft uses a simple

{x, y, z} coordinate system, where x and z are the horizontal directions (x is left/right if you have just started the game) and y is the vertical direction. If you have started a fresh game, you will be near to {0, 0, 0}. You can see the coordinates in the top-left corner of the screen, but to get them programmatically you can use:

We can teleport the character to a new location (in this case, up in the air) with:

If we have just started a game, then 10 blocks in front of us is {0, 10, 0}. But depending on how mountainous the terrain is, that block might be above or below ground. We can find the surface level with:

We can test that by looking at the block at that position. It should be Air.

And the block below it should be something solid:

Building

Now we can start building. We can place blocks of any type — for example, “Wood”:

We remove them by just overwriting the block with something else, such as “Air”:

But if you want a full undo, you must precede your changes with:

And then if you don’t like your changes, you can undo them with:

The list of the 156 available Minecraft block names is in the symbol $MinecraftBlockNames:

One reason to use the Wolfram Language for this is that it handles all kinds of interesting 2D and 3D objects, and I have set up the SetBlock command to handle these fairly automatically. For example, let’s paint a letter X in the sky in gold.

We can remove it again by replacing it with “Air”:

By default, rasterized content will be 12 blocks wide, so if you need more detail, you can increase that with an option. For example, here we have the Wolfie symbol (made by typing “\[WolframLanguageLogo]”) displayed at 50 blocks wide:

Anything you can create in the Wolfram Language can be made into blocks. Here is a plot of the function Sin[x]:

You can also control the orientation of rasterized images with an option Orientation.

If the content is a 3D geometry, then it will be rasterized in 3D:

There are lots of 3D geometry primitives, and they can be combined in many ways. Here are some cuboids, a pyramid and a sphere to make a house:

OK, I’m not much of an architect! We can look at our creation from the air by controlling the camera:

Reacting to Events

Finally, we can interact with blocks that you hit using the right mouse button while holding a sword. The left mouse button just places and smashes blocks, but the right mouse button creates events that wait for us to read and act on them. You can read these with:

This shows that since the game started, I have done two of these special hits, each time on the same block at {–1, 2, 2}, on face number 1 (the top of the block). I am player 1, but there could be multiple players.

I can fetch these pieces of information by position and name. For example, HitHistory[–1] is the last hit, and we extract its “Position” information and use that coordinate in MinecraftGetBlock to discover the type of block that was most recently hit:

And we can clear the data with:

As a simple example, let’s monitor this list every second and create an explosive “super hit.” I will define the explosion first. It is a function that takes a position and places a large sphere of air at that position:

Now I create a scheduled task to run every second, and apply that function to the hit history:

And now when I strike the ground in front of my house with my sword, using the right mouse button, a huge hole appears…

I can remove the monitoring task with:

There are a few more commands in the MinecraftLink package that you can read about in the documentation after you have installed the link.

As well as giving you a simple programming interface to Minecraft, similar to other languages, the Wolfram Language contains hundreds of high-level functions that let you develop much more exciting projects quickly; you might want to check out some of the 3D geometry, 3D image processing and built-in data sources as a starting point.


Join us again soon for a look at even more cool things you can do in Minecraft with a bit of code and curiosity.

Want to dig deeper into the code in this post? Download it as a Wolfram .notebook file.

About the blogger:

Jon McLoone

Jon McLoone is the Director of Technical Communication and Strategy and Director of Technical Services at Wolfram Research Europe Ltd. With over 25 years of experience working with Wolfram technologies, Jon has helped in directing software development, system design, technical marketing, corporate policy, business strategies and much more. Jon is the cofounder and Director of Development at computerbasedmath.org, where he leads the technical development team. He holds a degree in mathematics from Durham University and is a member of the Industrial Advisory Board for King’s College London. Find him on Twitter at @JonMcLoone.

Tech-Based Teaching: Computational Thinking in the Classroom

Collaborate and share experiences, tools, and ideas on implementing computational thinking in the classroom. By and for teachers, students, and learners.

Tech-Based Teaching Editor

Written by

Tech-Based Teaching is all about computational thinking, edtech, and the ways that tech enriches learning. Want to contribute? Reach out to edutech@wolfram.com.

Tech-Based Teaching: Computational Thinking in the Classroom

Collaborate and share experiences, tools, and ideas on implementing computational thinking in the classroom. By and for teachers, students, and learners.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade