custek123
Posts: 1
Joined: Tue Nov 13, 2018 10:13 pm

Coding With Minecraft PI

Tue Nov 13, 2018 11:00 pm

I recently purchased my Raspberry PI 3 B+ with the intention of learning how to code in Minecraft. I purchased learn to program with Minecraft by Criag Richardson. I have been attempting to run a bit of code that is meant to stack blocks. When I input the code as it is written in the book it does not construct a bock that is visible anywhere. Additionally, I have found samples of the code for the project online and run it as well with no success this way either. The code below is meant to construct a single melon block at the x, y, and z identified. Although as stated previously when I run this code it does not work. Alternatively, I have found a code that does work but I have not figured out how to modify the code to stack more then a single block. Although, as I have just finished typing this I have found a way to make this work to stack more then a single block by adding and additional (x, y+however many blocks I want, z, and the block ID. If anyone has additional ways of writing this code they would be appreciated.

Broken Code
from mcpi.minecraft import Minecraft
mc = Minecraft.create()
mc.setBlock(6, 5, 28, 103)

Working Code

from mcpi.minecraft import Minecraft
mc = Minecraft.create()
x, y, z, = mc.player.getPos()

mc.setBlock(x, y, z, 103)

User avatar
topguy
Posts: 4996
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Coding With Minecraft PI

Wed Nov 14, 2018 11:34 pm

I dont know how to program Minecraft but is seems logical to me that if you dont know where the position "6,5,28" is on the map, you can not be sure that the player/camera is facing that direction. The second code first initialize the x,y,z coordinated with the position of the player so then its very simple to place blocks relative to the player. You should print out the values x,y,z to see what they are... move your player and try again.

If you want to surround the player with 4 blocks for example.

mc.setBlock(x+1, y, z, 103)
mc.setBlock(x-1, y, z, 103)
mc.setBlock(x, y, z+1, 103)
mc.setBlock(x, y, z-1, 103)

If you want to make large stacks of blocks without adding a lot of lines of code, you should learn about "for" loops.

Return to “Beginners”