Introduction

The MineCraft world can sometimes get a bit boring, so why not make it prettier by adding a beautiful rainbow?

The Challenge

The challenge is to create some code to build a rainbow in the MineCraft world.

Step 1 – a quick maths lesson

What we see as rainbows are actually arcs of a circle and sometimes you can actually see a full circle rainbow when flying in a plane. So if we want to create a rainbow we first need to work out how to create a circle in the MineCraft world.  We can do this simply by add a block of wool at each of the 360 degree points on the circumference of a circle.

But how do we find out where to put each of these points? Well we use some maths!

Here are the equations we need to calculate each X and Y.

PointX = OriginX + radius of circle * cos(angle * Pi / 180)

PointY= OriginY + radius of circle * sin(angle * Pi / 180 )

>>>import math
>>>math.sin(1)
0.8414709848078965
>>>math.pi
3.141592653589793

 

 

 

 

 

 

 

point

 

 

Step 2 – Calculating the x coordinate

Using the formula above we first need to calculate the x coordinate of the point.

>>>radius = 20 # the size of the circle (in blocks)
>>>originX = 30 #the x coordinate of the centre of the circle
>>>angle = 0 # the point we want to draw (out of  0—360 degrees)
>>>pointX = origin + radius * math.cos(angle *math.pi / 180)
>>>pointX
50.0

Step 3 – Calculate the y coordinate

Can you calculate the Y-coordinate by adapting the above code above and looking at step 1? Have a go!

Step 4 – Add a block of wool

originZ = 50
mc.setBlock(x,y,originZ,block.WOOL.id,colourID)

Step 5 – Repeat for all 360 degrees

Add all the code above into a for loop or while loop and repeat for every degree in the circle ( or  just 180 degrees if you like!)

    for angle in range(0,180):
        x = originX + radius * math.cos(angle * math.pi / 180)
        y = originY + radius * math.sin(angle * math.pi/ 180)
        mc.setBlock(x,y,originZ,block.WOOL.id,14)

 Step 6 – Repeat for all 7 colours of the rainbow

The code above is great but it just draws a circle, it doesn’t do all the colours of a rainbow. In order to do this we need to use a list of colours and to take it in turn to draw each colour of the rainbow, increasing the radius of the circle by 1 each time so that each colour doesn’t overwrite the previous colour.

colourIDs = [14,1,4,5,3,11,10]

for colourID in colourIDs:
    for angle in range(0,180):
        x = originX + radius * math.cos(angle * math.pi / 180)
        y = originY + radius * math.sin(angle * math.pi/ 180)
        mc.setBlock(x,y,originZ,block.WOOL.id,colourID)
    radius+=1

Extensions

  • Add a user interface to your code so that user can choose the size and position of the rainbow.
  • Create a rainbow road instead of a normal rainbow (see the YouTube video)
  • Adapt your code so that the rainbow follows the player around the MineCraft world.
  • Create a double rainbow (see Wikipedia for what one should look like!

Complete Solution

Don’t have time to work out your own solution? Here’s the code for the solution.

import minecraft as minecraft, block,time,random,math

mc = minecraft.Minecraft.create()
mc.player.setTilePos(0,20,50)
originX = 20
originY = 0
originZ = 50
radius = 30
colourIDs = [14,1,4,5,3,11,10]

for colourID in colourIDs:
    for angle in range(0,180):
        x = originX + radius * math.cos(angle * math.pi / 180)
        y = originY + radius * math.sin(angle * math.pi/ 180)
        mc.setBlock(x,y,originZ,block.WOOL.id,colourID)
    radius+=1

 

 

Recommended reading