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:
Test out the Python -> Minecraft connection (and have fun) by running some demo programs
Run the Python IDLE editor and create a "hello world" program
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.
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).
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.
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.
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.
Press ESC in Minecraft to open the Game Menu and release the mouse cursor.
Open IDLE from Python using the Recently Added group of the Windows Start Menu (or scroll down to P and find Python 3.8).
Select File->New File to open the editor window, and enter the following program (type it or copy'n'paste):
from mine import *
mc = Minecraft()
mc.postToChat("Hello Minecraft World from Lowell")
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):
Go back to Minecraft and press ESC again to return to the game. Enter "/py HelloLowell" and look for the message in the console.
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.
Go back to the IDLE editor window, run File->Open, and select "donut.py"
Find the "draw_donut(" lines at the end of the program. The first one makes the glass donut, the second creates the water filling.
Find the number 18 (fouth entry in the list of arguments) and change it to 10 on both draw_donut lines.
Run File->Save As and save the modified program with a new name - maybe add your initials to the end like "donutTMS.py"
In Minecraft, run "/py donutTMS" to try out the program with the new donut size
Now back at IDLE, change the first block type from GLASS to DIRT and the second type from WATER_STATIONARY to LAVA.
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 "maze.py" program to change the size of the maze and the block used!