Chapter 3

Python and Minecraft magic!

Exercise 01: Hello Minecraft World

“Hello, World” is the most basic program we can build with any programming language. Let’s see what we can do now to make it more interesting with Python and Minecraft.

What you’ll learn

  • how to get the position of the player in Minecraft
  • how to import modules
  • how to create variables
  • how to optimise your code

The Minecraft API lets us have access to more than just being able to post to the game chat. We can also get the position of our player in the game world, create and destroy blocks, and even detect the kinds of blocks around us.

Pastebin Code

Exercise 02: Minecraft Whispers

Troll friends on your server by posting random messages to the in-game chat from an external text file.

Learning objectives:

  • make connections to external data sources
  • use a random number generator module
  • create a custom function

This is a simple program that takes advantage of Python’s ability to read a text file outside of your main program. Here’s a list of all the things we want our new program to do:

  • generate a random number
  • pause at fixed or random times
  • read from an external data source (in this case, another text file)
  • send data to Minecraft

Pastebin Code

Exercise 03: Force Field

Create a wall of glass around your Minecraft creations by simply running your Python program.

What you’ll learn:

  • how to construct large Minecraft objects by coding
  • how to request user input from the python console to change your program
  • how to use an if statement

Nothing’s worse than when you’ve spent hours building something amazing in Minecraft, only for someone to come along and grief your creation. With this program, you’ll be able to throw up a ‘force field’ made of glass all around your base at the touch of a button. For this exercise, we’ll need the exact coordinates for the perimeter of our house, so we’ll start with a program that can determine our location in Minecraft.

Pastebin Code

Exercise 04: Killer Drop

In this last exercise, we’ll create a mini game in Minecraft with Python.

What you’ll learn

  • how to create your own mini game in Minecraft
  • more about the random function
  • nesting functions
  • creating variable lists
  • creating different kinds of loops

Pastebin Code