Python Coding for Android Minecraft PE
by arpruss in Circuits > Software
76980 Views, 35 Favorites, 0 Comments
Python Coding for Android Minecraft PE
The Raspberry PI version of Minecraft lets you script in Python, making fractal designs, drawings, turtle graphics, simple games, etc. The same thing can be done on a desktop version with my Raspberry Jam Mod (here is my Instructable on how to use it), as well as with a server-side mod. There is a great book by Whale and O'Hanlon on Python scripting for Minecraft, and O'Hanlon's site has a lot of information.
I now made a mod that lets you do all this with the Android Pocket Edition of Minecraft. The code runs more slowly than the desktop edition, but you can still draw knots, fractals, L-system based plants, have fun with turtle graphics, etc. Source code is here and an installer is on Google Play.
In this Instructable I will explain how to install my mod and get started with Python coding for Minecraft on Pocket Edition on a phone or tablet. (A Bluetooth keyboard is recommended, but not necessary.)
You will need an Android 2.3 or higher device (but devices from the 2.3 era may be too slow for the more complex scripts) a copy of Minecraft PE, and some free software.
Prerequisites
Install the following apps, available on Google Play (they're also all up in the Amazon Appstore):
- Minecraft PE (the only thing you need to actually pay for)
- BlockLauncher (free) or BlockLauncher Pro (paid)
- QPython
- Raspberry Jam Mod PE
Install Raspberry Jam Mod
- Run Raspberry Jam Mod.
- If it finds that the prerequisites are all installed, it will give you a big "Install!" button. Beside it, you can also choose what to do with existing QPython scripts.
- Tap on "Install".
- You will get BlockLauncher's script import screen. Tap on "Import".
- Turn your device to landscape (sometimes BlockLauncher may crash in portrait) and start BlockLauncher (I recommend from now on using BlockLauncher to start Minecraft).
- Tap on the wrench button (middle of top of screen).
- Tap on "Manage ModPE Scripts".
- Tap on "raspberryjampe.js" and then on "Enable".
- Press back, and everything is installed.
Run a Sample Script in QPython
- Create a world in Minecraft (making sure you start Minecraft with BlockLauncher): I recommend a Creative world, with "Flat" in advanced options.
- Press your device's Home button and run QPython.
- Swipe the QPython main screen to the left.
- Tap on "Programs".
- Choose a script, I'll suppose "borromean.py".
- You can now choose "Open" to view it, and then the play button to run it, or "Run" to run it directly. Either way, you will get a text console screen when the script is running.
- Switch back to Minecraft (on my phone that's a long-tap on home, and on my tablet that's the multi-app button).
- The script will now be running. Look up to see what it's doing. You may need to back away to see better.
Run a Sample Script From Inside Minecraft
You can also run a script right from within Minecraft (started within BlockLauncher). Just tap on the chat button in the upper right corner, and once you get the keyboard (you may need to tap on the keyboard icon), type
/py scriptname
and press next or enter. (Omit the ".py" extension.) This will launch the script inside QPython (you can press the back button several times to switch back to Minecraft to watch it run; make sure you allow the console to continue running in the background). The script must be found in your internal storage's com.hipipal.qpyplus/scripts folder. You can also include commandline arguments (e.g., to draw an icosahedron, do /py polyhedron 14 and to do a dodecahedron do /py polyhedron 14 faces).
In addition to the /py (or /python if you want to type more) command, my Raspberry Jam Mod PE includes commands:
/time set x
to set the time, and
/tp x y z
to teleport, where (x,y,z) are coordinates (with 0,0,0 being the spawn point). You can also do relative coordinates by using ~. E.g., /tp ~ ~10 ~ moves 10 blocks up. (And if ~ is hard to type on your keybaord, you can use any letter in its place!)
Create a Simple Script
Make sure Minecraft is running (via BlockLauncher) with a world loaded.
In QPython's main screen, swipe to the right and choose "Editor". Press the big "+" button in the upper right corner and choose "Blank file".
To save yourself typing, tap on the Snippets icon (the second-last icon on the bottom line). Choose "Minecraft turtle".
You will get some boilerplate code.
Before the last postToChat line, type:
for i in range(5): t.go(20) t.right(144)
Then tap on the disk icon to save the script as sample.py, making sure to put it in the scripts folder. Then press the play button. When you switch to Minecraft, you should see a five-sided star made of gold bricks.
To learn how to make various more complicated scripts, see my big Python coding for Minecraft Instructable (one note: t.turtle(x) will only work for x=None or x=PLAYER). (Though beware that some of the scripts there may be too complex for a mobile device. For instance, the Menger sponge needs one less level.)
Interactive Python Console
You can also run Python code interactively by launching the console.py or turtleconsole.py script (with QPython or via /py console or /py turtleconsole) and typing Python commands into chat, ending with 'quit'. The console.py script initializes an mc global variable which is an instance of Minecraft() and the turtleconsole.py script initializes t which is an instance of Turtle().
One trick: the chat doesn't let you enter an empty line, which in a Python interactive session is needed to get out of an indented block. To register an empty line, just enter a single space on a line.
f