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.
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.
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!)