Meeting 2019-12-17

Testing software installation

After last week's class we stayed late and got everything set up on the 12 Surface Pro computers we were using in class. So for now on we can focus on programming and Python!

There are three objectives for today's class:

    1. Test out the Python -> Minecraft connection (and have fun) by running some demo programs

    2. Run the Python IDLE editor and create a "hello world" program

    3. Modify some demo programs with IDLE to change how they behave

Testing Python Scripts in Minecraft

To make sure everything is working and get a feel for what can be done using the Python API we will run some of the demo programs that got installed with the mcpipy package.

    1. Run the Minecraft Launcher, make sure the Forge 1.12.2 installation is selected, and click "PLAY". Once Minecraft is at the starting screen, check in the lower left of the startup screen that there are 5 mods installed (including the RaspberryJamMod).

    2. Set Minecraft to be windowed (not full screen) by pressing Options->Video Settings->Fullscreen->OFF. Resize the MC window to 2/3rds of your screen area to make room for the Python editor.

    3. Click on Singleplayer mode and "Create New World" to create a new test world. Name it "Python World" (or something more creative), select Game mode: Creative, and press "More World Options...". Press "World Type:" to get "Superflat" and optionally Customize your flat world. Leave "Allow Cheats: ON" and press "Create New World" at the bottom.

    4. Once in your new world, enter /py donut to test Python script running - it should make a huge glass torus filled with water! If it says "[ERR] Python not found" you need to reinstall Python and check the Add to PATH box.

Now we can try some of the other demos. Enter "/py<space>" and then press the <tab> key. Minecraft will display a list of the Python demo routines you can run:

Try some of them out. You may not see the results right away, so be patient and look around after you run a command to see the results. To interrupt a program that's running you can enter the "/py" command again without a program name.

Enter a "Hello World" Program using IDLE

Now let's try writing our own simple program to say "Hello World" on the Minecraft console - it's a programming tradition that shows us we are ready to program.

    1. Press ESC in Minecraft to open the Game Menu and release the mouse cursor.

    2. Open IDLE from Python using the Recently Added group of the Windows Start Menu (or scroll down to P and find Python 3.8).

    3. Select File->New File to open the editor window, and enter the following program (type it or copy'n'paste):

      1. from mine import *

      2. mc = Minecraft()

      3. mc.postToChat("Hello Minecraft World from Lowell")

    4. Save the program as in the Python scripts folder. To get there, enter %appdata%\.minecraft\mcpipy in the top path line of the "Save As" window that opens (by the up arrow and the yellow folder icon):

    1. Go back to Minecraft and press ESC again to return to the game. Enter "/py HelloLowell" and look for the message in the console.

    2. We will discuss what each lines in the program does in future classes.

Modify Demo Programs using IDLE

Now that your IDLE editor is in the folder with all the demo Python files, we can explore what a Python+Minecraft program looks like and play around with some of the settings - this will help you practice with the IDLE text editor. We will modify the program to create smaller donuts and change the material.

    1. Go back to the IDLE editor window, run File->Open, and select ""

    2. Find the "draw_donut(" lines at the end of the program. The first one makes the glass donut, the second creates the water filling.

    3. Find the number 18 (fouth entry in the list of arguments) and change it to 10 on both draw_donut lines.

    4. Run File->Save As and save the modified program with a new name - maybe add your initials to the end like ""

    5. In Minecraft, run "/py donutTMS" to try out the program with the new donut size

    6. Now back at IDLE, change the first block type from GLASS to DIRT and the second type from WATER_STATIONARY to LAVA.

    7. Save again and try running "/py donutTMS" in Minecraft again - break the dirt and watch out!

Here's how your code should look:

Even if you don't understand the Python language yet, you can find and change elements of the programs to see how they work, and this is a useful way of learning about the Python/Minecraft environment.

Now try editing the "" program to change the size of the maze and the block used!