Minecraft and Python — text-based coding!

For my first foray into text-based coding, I recently moved to coding Minecraft with Python. Or “Mython”, as I affectionately call it. I’ve tried other things, like Small Basic and GameMaker (GML, their text language) and, while students can learn from any platform, I felt Minecraft offered more bang for the buck.

The engagement factor is off the charts for Minecraft. I’d be crazy not to try to tap into that. So I launched a year-long exploration of various coding environments for Minecraft(you can see my now somewhat-dated presentation from CSTA 2015 here). My conclusion was that Python offered the best entry-point, and the amount of support available tipped the scales for me. Here’s why:

It’s fairly easy to install and available on Mac, PC, Linux, and Raspberry Pi. For the first three, you’ll need Minecraft, Python, and an implementation of a Bukkit server so that each student can run her own instance of Minecraft. She will also need a Minecraft license (which does cost $27US, but many kids have one already). The beauty of Mython on the Raspberry Pi is twofold — no need to purchase a Minecraft license, since Mojang donated an early version of Minecraft to the Raspberry Pi Foundation. And the Bukkit server is not needed. So there are two big steps you can skip, if you’ve got a set of Raspberry Pi’s (see my chapter on RPi for many reasons why you should consider this).

So why Minecraft?

Engagement level is super high.

Most kids have some familiarity with the game.

The ability to “mod” Minecraft is enticing (though this isn’t modding in the strict sense of the word).

Support is good. I highly recommend getting a copy of the Mython “bible”: Adventures in Minecraft by David Whaley and Martin O’Hanlon. These two gents from Great Britain literally wrote the book on coding Minecraft with Python, and it’s all laid out in an attractive, logical, and clear way, with lots of cool challenges to extend your learning. The authors host an awesome website (www.stuffaboutcode.com) which has a forum for those thorny questions. They are more than willing to help out, and I’ve found these two to be always helpful. In addition, they are on Twitter and have always responded quickly, kindly, and accurately to my questions. And version 2 of Adventures in Minecraft now has support for the Microbit board!

Check out the appendix of the book for more resources.

Here’s how I “teach” Mython. As you might gather from my pedagogy, there’s a reason I put “teaching” in quotes. I stole the idea of the “hackpack” from Chris Penn in Great Britain, as well as some of his excellent code ideas and challenges. I print out “hackpacks” for each student. These include anywhere from 4-7 programs on a particular topic (the first covers basics and building, the second covers triggers, etc.). The code is liberally commented with explanations of what the code does, how it works, analogues to Scratch, gotchas, and whatever I think will help the students understand the code.

And yes, I give them a printed paper copy of the code. That’s by design. I know that copy/paste works great for getting code, and I use it all the time. However, I believe when a student is learning a new language, the muscle memory that she uses by actually hitting the keys and looking at the screen is invaluable in making it stick. It also gives the student a fabulous opportunity to become personally acquainted with error messages! I tell them that error messages are their friends. They don’t believe me, of course, but it’s true, because Python is training them to speak in a way that it understands!

That, of course, is the big difference when we take the training wheels off! It’s now possible to make syntax mistakes! Spelling! Punctuation! Capitalization! Indents! All of it matters. And Python doesn’t know “what you meant”; it only knows “what you said”. Sorry! But welcome to the wonderful world of code.

The good news is that students want to make their code work, and they’re willing to keep at it, fixing errors, until it works. Aha, the magic of Minecraft! Much preferable to doing coding exercises that sort lists or do math or any of the other introductory exercises I’ve seen. I have to confess that my reaction to all of those is a great big “meh”. Who cares? I already know what 8 + 7 equals. Doing it in Python doesn’t make it much more palatable. But if I can figure out how to teleport myself 80 blocks up in the air and 40 blocks to the east, well, that’s cool! And incidentally it’s also teaching a 3-dimensional coordinate system, but shhh…. stealth teaching, remember?

So they get their hackpack, and they type in the code, and it works! Fiero! The dance of joy! But they don’t actually earn any credit for that. I tell them, “Congratulations, that means you can type.” To earn points, they must code what I call a “reverse”. That is, they have to take that code and do something different with it. For instance, one of the hackpacks has code that updates my position every second and displays it in the chat window with the relevant x, y, and z position. The “reverse” is to display it every 4 seconds. Hmm… how to do that? Well, where in the code do you say to display it every second? We look at the loop (while True:) and find the code that says to get my position and post it to the chat. Do you see anything that says to do it every second? Oh… time.sleep(1)! The light goes on, and they swap out the 1 for a 4! Easy peasy! But they’ve learned about a loop, they’ve learned how to get and update my position continually, they’ve learned how to print that to the user (Hello World!), and they’ve learned about delays as well (and that you have to import the time library in Python for it to work).

And I didn’t have to stand at the front of the class and say, “Today, class, we are going to learn about… now follow me as I type on the screen and type that exactly, all together now…” Yawn. Students do it at their own speed and find their own “aha moments”! And they get to see it actually happen in Minecraft, on their screen, caused by the code that they just typed in! Woohoo!

So you can see why I’m excited about using Minecraft for teaching coding!

Another carryover, I’m hoping, is that when they go from my class in 8th grade to our 9th grade CS class, where they will code their own original games using the Pygame library, that at least some of this will be burned into their brains. At the very least, they’ll remember they have to be careful with what they type, since they learned firsthand how picky “the Python” can be!

So if I had a standards-based curriculum, what CS standards and concepts have I “covered”? By the time we’ve finished the unit, we can check off: conditionals, events, triggers, 3D coordinate systems, for loops, variables… in Python, they’ve learned about importing libraries, proper indents, colons and why they’re essential, and how to read Python error messages. But again, shh…. let’s not spoil the fun.

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *