r/developer • • Feb 29 '20

Programming in Python and transforming the Minecraft world your way

Why Python?

It is a powerful and great language for any beginner programmer, being able to create systems, learn the basics of programming and still have fun compiling to modify the Minecraft world in this case;)

Why Minecraft?

Because it is considered one of the most fun games of our time and stimulates creativity with the modification of its world and possibilities.

What will we need?

5 little programs will be installed to start the game.

First install Minecraft on your machine (default windows… all next hahahah)

Then install phyton, how do you know if it is installed?

Next step ... JAVA, download and install it on your PC.

Verifying that java was installed correctly with the command “java -version”

Finally, with the files from the “Minecraft Python API & Spigot Minecraft Server” step downloaded, extract to a folder of your choice.

With the folder open, double-click to install the Minecraft Python API, a black screen will open, click on any key and as soon as it closes it will be installed.

We have completed our installation steps!

Are we going to run the server and create a new game to start this adventure?

Starting server and creating a game

In the folder that extracted the API, click on "Start_Server"

The cmd black screen will appear starting the server, in this style:

Now with the server on, open Minecraft and follow these steps:

Click on “Multiplayer”> “Add server”

In the name of the server I put Minecraft Python, it is to let us know that that server is ours and what we are going to work with.

In the "Server Address" place "localhost", as it is the address of your own machine.

Created server, let's go in and explore a little bit of this world

Putting hands in the dough

Before anything to manipulate we need to extract the ZIP “minecraftPythonAPI” and copy the folder “mcpi” and place it where we will save our codes.

In this case above, I put the “mcpi” folder inside a folder called “SetupMC” which is where I will save my game manipulation codes.

Open IDLE now (which is where we will look for or create our code in the folder)

Go to File> Save as> Find your folder and save your “.py file”

Not forgetting that your .py file must be in the same folder where you placed the “mcpi” folder.

Open your “file.py”

Before writing anything here, let's take a RUN MODULE (f5) to open the IDLE
and write what we need there and save it in this file if necessary

When started it will have a message very similar to this one.
And the ">>>" are used to write and execute something, test 1 + 1 and hit enter and you will see the calculation "2".

But let's get down to business and make something happen in the Minecraft world.

We will teleport to some locations using the following code.

As you can see in the game, I am in the “XYZ” position at X = 117,103, Y = 50.00000 and Z = 36.855.

Let's create a code to get me out of this place and teleport me for a few seconds.

Code to copy;)

from mcpi.minecraft import Minecraft
mc = Minecraft.create()
mc.player.setPos(120,120,0)

As you can see in the game, I am in the “XYZ” position at X = 120,000, Y = 68.00000 and Z = 0.000.

Explaining the code in parts ...

Here I import the Minecraft library, remember that folder we played in the same place as our initial code? so… that's where the magic happens and that's why we need to import this library so that we can use what we need to, teleport, assemble blocks, “summon” animals and food.

from mcpi.minecraft import Minecraft

right after this code and pressing the enter key in our “Shell”, we have this second piece of code.

this is where we set that we want to use the “Create” method or create and declare a variable with it.

but what is variable? "A variable is an object (a position, often located in memory) capable of retaining and representing a value or expression" in simple words, we are saying that "Minecraft.crate ()" is now called "mc"

mc = Minecraft.create()

Again we will click on the enter key and we will go to the next block section.

This section is where we make our character's teleportation happen.

Here I take the variable “mc” which is our renamed “Minecraft.crate ()” and we say to him, take the “Player” and play it in the “120 in X”, “120 in Y” and “0 in Z ”.

mc.player.setPos(120,120,0)

But what are X, Y and Z? X is where our character will move forward, Z is where he will walk to the side and Y is whether he will jump.

X and Z make us walk horizontally and Y represents our height.

With this little introduction I hope you can teleport to different locations within the game.

Here is another code to create blocks

mc.setBlock(x,y,z,NUMERO DO BLOCO)

As we learned up there, you can now place the values ​​you need in x, y and z under a list of block codes.

  • 162 (Wood)

  • 38.3 (Flower)

  • 10 * (Lava)

  • 8 * (Water)

I hope you enjoyed this little tutorial on how to run the manipulation project with Python using Minecraft and manipulating the game.

Like and comment if you want more steps to master this game differently, create mini games and have fun manipulating the game;)

Remembering that the learning base used in this post came from a book in which the image will enter below.

Hugs and see you soon

Original post on my Medium: https://medium.com/@rodrigoka4/programando-em-python-e-transformando-o-mundo-de-minecraft-a-sua-maneira-3d94a275faec

my github: https://github.com/roddyka
Linkedin: https://www.linkedin.com/in/rodrigo-carvalho-antunes/

16 Upvotes

2 comments sorted by

2

u/mazummo Mar 30 '20

Hey, I was looking into getting this book. I saw some reviews claiming that the book is out of date. Can you confirm that it's still relevant?

1

u/roddyka Mar 31 '20

Yes of Course ;) I love this book. You realy will learn basic of the python and a little bit advanced using this code running Minecraft