This week I am preparing resources for a teacher CPD showing teachers how to create a Christmas tree in Minecraft through code. At first this seemed to be a simple task, making a trunk and a triangle like structure to sit on top. But it didn’t take long to realise it isn’t quite so straightforward. The final structure is quite mathematical in its construction. To create the tree, it is necessary to understand the coordinates being worked with and which direction the tree will face. So first off, I decided to build it in 2D, this means I am only working with two coordinates, either x or z and y for upwards. I decided to work in the x and y direction, keeping z unchanged for the build.
The first job is to place the trunk near to where the player is stood therefore I need to get the player position and make adjustments to x and z to place the tree in front of the player. I decided to have a trunk two blocks high, this was a simple case of placing one block on top of the other using the new x and z calculated from the player's position and increasing y for each block to increase the height.
The next step was to add the foliage to the tree. Since a Christmas tree is traditionally triangular in shape it made sense to have narrowing rows of blocks. This is where the Maths comes into play. Each row had to be an odd number of blocks so that the trunk is in the centre. Each subsequent branch is then two blocks shorter as one is removed from each end to keep it symmetrical. Therefore, the left hand x coordinate at is increased by one (x+1) and at the rigt hand x coordinate is decreased by one (x-1). At the same time each row had to increase in height so we had to add 1 to y.
Once the beginning of the row and end of the row have the same x coordinate then you are at the top of the tree. The next step is to add the star. In this case we are using wool. In Minecraft wool comes in different colour blocks, therefore the easiest way is to have a list of the different wool colours and randomly select a colour from the list. This is then displayed on the tree in an infinite loop so that the colours change and the block twinkles.
The next stages are to add candles or lights and to make it 3D. I am also going to look at the efficiency of the code and introduce loops to create the tree.