Meeting 2020-01-14

Welcome back from Winter Break!

After a long break, let's make sure everyone can write and edit Minecraft Python programs before we move on to programming our own. If you already did the Hello World program you can skip to modifying the demo programs - there are lots of them and it is fun to explore what happens with simple changes.

Set up PC desktop for programming

Most people play Minecraft in full screen mode, but to make it easy to program we will run it in a window next to our Python editor. From Minecraft press ESC to get to the Game Menu, then press Options->Video Settings->Fullscreen to turn Fullscreen:OFF. Then resize your MC window to fill the right 2/3rs of your screen.

Open IDLE (the Python editor) so you have a text editor window on the top left side and the Python Shell window at the bottom left. Here is an example of my home desktop:

The Lowell computers will have smaller screens but you get the idea.

Prevent Minecraft Pausing

By default Minecraft pauses execution when you switch to another window, but it would be nice to be able to edit files while Minecraft is running. We can change this behavior by editing the options.txt file in the main Minecraft directory. Open a Windows Explorer window (yellow folder icon) and click in the field that shows the current path. Enter %appdata% and navigate to %appdata%\Roaming\.minecraft:

Then double-click on the "options.txt" file and open it in Notepad (or your favorite text editor). Search for pauseOnLostFocus and change ":true" to ":false", then save the file. This will allow you to use Alt-Tab to switch between the windows on your screen without pausing Minecraft. You may need to restart Minecraft to see the change.

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 HelloLowell.py 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 donut.py program to create smaller donuts and change the material.

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

    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 "donutTMS.py"

    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.

Here is a list of Minecraft block names from the file %appdata%\Roaming\.minecraft\mcpipy\mcpi\block.py.

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

Try editing snowflake.py to change the degree of the snowflake shape by editing the bottom line - change the 4 to a 3 or 5 (5 is pretty large!)