Variables and 3D Shapes

In the last class we used functions and arguments to build circles, spheres and hollow spheres of different radii. Lets go ahead and build some 3D solid shapes as well. Before we can do so we need to learn about Variables and about solids shapes called Polyhedrons

Variables

A variable is a box or container to hold a value. You can put a different value in a box everytime.

  • Number values like 1, 0, -10 are called integers
  • Decimal values like 3.2, 1003.4, -4.5 are called floats
  • Word values like "Steve", "Hi Brooks School" are called strings

Variables have names. Programs can use variables names instead of raw integers and strings which is very handy. E.g., in the program below, the variable with the name positionX has the integer value 10 and the variable with name message has the value "Hi Brooks School"

positionX = 10
message = "Hi Brooks School"

In the next few tasks, you will practice using variables.

Start Minecraft and run the program cell below before you begin your tasks.


In [ ]:
import sys
sys.path.append('/home/pi/minecraft-programming')
import mcpi.block as block
import time
import drawings

Task 1

  • Set the value of the variable named userName to "Steve" and run the function in the task 1 program cell
  • Now set the value of the variable named userName to "Brooks" and run the function in the task 1 program cell again

In [ ]:
# Task 1 program
userName="blah"
mc.postToChat(userName)

Task 2

  • Run the function below in the task 2 program cell after defining the two variables below
    • Define a new variable named radius and set its value to 5
    • Define a new variable named blockToUse and set its value to block.WOOD.id
drawings.drawMyCircle(radius, blockToUse)
  • Now set the value of the variable named radius to 8 and the value of the variable named blockToUse to block.GLASS.id and run the same function in the task 2 program cell again

Well done!


In [ ]:
# Task 2 program

drawings.drawMyCircle(radius, blockToUse)

3D Shapes and Polyhedrons

The picture above shows the solid shapes studied by the mathematicians Plato and Euclid.

We will use the function drawings.drawSolid to build these solids. This function takes the arguments

  • shape - name of the shape to build, which is one of "TETRAHEDRON", "OCTAHEDRON" or "CUBE"
  • length - length of the side of the shape to build e.g., 5
  • blockId - id of the block to build with e.g., block.STONE.id
drawings.drawSolid(shape,length,blockId)
# e.g., drawings.drawSolid("TETRAHEDRON",5,block.STONE.id)

Task 3

Use the building function drawings.drawSolid to build a cube with length 5 and using the block block.GLASS.id


In [ ]:
# Task 3 program

Task 4

  • Run the function below in the task 2 program cell after defining the three variables below
    • Define a new variable named shape and set its value to "CUBE"
    • Define a new variable named length and set its value to 5
    • Define a new variable named blockToUse and set its value to block.GLASS.id
drawings.drawSolid(shape, length, blockToUse)
  • Now set the value of the variable named shape to "TERAHEDRON" and the value of the variable named blockToUse to block.STONE.id and run the same function in the task 2 program cell again

Well done!


In [ ]:
# Task 4 program

drawings.drawSolid(shape, length, blockToUse)