Raspberry Pi, Minecraft and Python
Today I'm talking about something that has existed for a long time and several sites have already spoken : the Minecraft edition on the Raspberry Pi. Now it's my turn to (finally) discover this game other than iOS edition !
To discover the possibilities of the API (because that's mostly what I wanted to test), I therefore used a light distribution (see here how to install very simply) to which I added an X Server. I so get the bare minimum. But if you already have a Raspberry Pi that works, you can skip the step to install Minecraft.
- Update of the system
apt-get update && apt-get upgrade -y
- Installation of packages for a minimal X Server
apt-get install -y xserver-xorg xinit xserver-xorg-video-fbdev lxde lxde-common libsdl1.2debian
- Download and install Minecraft
wget https://s3.amazonaws.com/assets.minecraft.net/pi/minecraft-pi-0.1.1.tar.gz
tar -zxvf minecraft-pi-0.1.1.tar.gz
cd mcpi
- Run Minecraft
./minecraft-pi
Si vous avez une erreur Failed to Add Service - Already in Use , make sure you have defined at least 64Mb of video memory.
- The files needed to use the API are in the directory api/python (sf you prefer Java, It is api/java).
Copy the files in a particular directorymkdir apiTest
cp -R api/python/mcpi/* apiTest/
- Go to the created directory
cd apiTest
- Create a file, for example test.py with the following content :
import minecraft mc = minecraft. Minecraft.create() mc.postToChat("Hello Pi Home Server !")
- The position of the player. You can easily get or set the position of the player in the universe via the methods player.getPos() and player.setPos(). You will be working in 3 dimensions. X and Z are the horizontal axes. Y the vertical one.
Here is an example for changing the position of the player and to drop high !# POS will contain x, y and z position of the player pos = mc.player.getPos() # another way to get player coordinates x, y, z = mc.player.getPos() # set player position. In the air and fall ... mc.player.setPos(x, y 100, z)
- Create a block with the method setBlock
An example to create a block of stone in front of the playermc.setBlock(x 2, y, z, block. STONE)
- Create blocks with the method setBlocks
An example to create a block of 3×3 Stone front of the playermc.setBlocks(x 1, y 1, z 1, x 4, y 4, z 4, block. STONE)
- Other blocks type than stone. All types are defined in the file block.py. Here is the list :
AIR = 0 STONE = 1 GRASS = 2 DIRT = 3 COBBLESTONE = 4 WOOD_PLANKS = 5 SAPLING = 6 BEDROCK = 7 WATER_FLOWING = 8 WATER = WATER_FLOWING WATER_STATIONARY = 9 LAVA_FLOWING = 10 LAVA = LAVA_FLOWING LAVA_STATIONARY = 11 SAND = 12 GRAVEL = 13 GOLD_ORE = 14 IRON_ORE = 15 COAL_ORE = 16 WOOD = 17 LEAVES = 18 GLASS = 20 LAPIS_LAZULI_ORE = 21 LAPIS_LAZULI_BLOCK = 22 SANDSTONE = 24 BED = 26 COBWEB = 30 GRASS_TALL = 31 WOOL = 35 FLOWER_YELLOW = 37 FLOWER_CYAN = 38 MUSHROOM_BROWN = 39 MUSHROOM_RED = 40 GOLD_BLOCK = 41 IRON_BLOCK = 42 STONE_SLAB_DOUBLE = 43 STONE_SLAB = 44 BRICK_BLOCK = 45 TNT = 46 BOOKSHELF = 47 MOSS_STONE = 48 OBSIDIAN = 49 TORCH = 50 FIRE = 51 STAIRS_WOOD = 53 CHEST = 54 DIAMOND_ORE = 56 DIAMOND_BLOCK = 57 CRAFTING_TABLE = 58 FARMLAND = 60 FURNACE_INACTIVE = 61 FURNACE_ACTIVE = 62 DOOR_WOOD = 64 LADDER = 65 STAIRS_COBBLESTONE = 67 DOOR_IRON = 71 REDSTONE_ORE = 73 SNOW = 78 ICE = 79 SNOW_BLOCK = 80 CACTUS = 81 CLAY = 82 SUGAR_CANE = 83 FENCE = 85 GLOWSTONE_BLOCK = 89 BEDROCK_INVISIBLE = 95 STONE_BRICK = 98 GLASS_PANE = 102 MELON = 103 FENCE_GATE = 107 GLOWING_OBSIDIAN = 246 NETHER_REACTOR_CORE = 247
Here is an example that will create a bloc and change its type (except water and lava to avoid floods ;-)) every seconds (I do not handle types that do not exist) :
# loop on all block types (except water and lava) for blockType in range(0, 300): if blockType < 8 Gold blockType > 11: mc.setBlock(x 2, y, z, blocktype) sleep(1)
- More fun examples ! You can use the power of programming to animate the blocs in the world. You can create an analog clock or digital (Bravo SleepyOz):
With this trio you can entertain you (or your children) and learn programming in General with Python, which is a relatively accessible language. Other languages are possible : Java, JavaScript with NodeJS, Ruby or SmallTalk.
As in the game, your imagination will be a limit.
If you have any images or original videos or incredible of what is done with the Minecraft Pi edition, do not hesitate to let us enjoy !
Sources
- https://www.raspberrypi.org/forums/viewtopic.php?t=33427
- https://www.raspberrypi.org/learning/getting-started-with-minecraft-pi/worksheet/
- http://www.stuffaboutcode.com/p/minecraft-api-reference.html