Friday, July 18, 2014

Minecraft and Python

The last week of school I brought in a Raspberry Pi with the Minecraft Pi version that comes with its own API to program with Python.  I'll talk about the school/learnin' we did and then explain how we did it.

If you are not familiar with Minecraft, it is a game and a creative world.  For the Pi version it is just creative mode in a small (-127 to 127 dimensions) world.

The link above is for the education version of Minecraft (a different version) but if you are going to take 5 minutes to learn about Minecraft, you will want to learn about this version unless you want to dive into the actual game.

Within the Minecraft world you build with 1x1x1 blocks to create anything you want.  Using Scratch, my fifth graders had learned about X/Y graphs with negative numbers.  Adding Z space to this was a whole lot more interesting and confusing (also, a bit above grade level...which is a sentiment I don't always agree with).

To introduce the concept I constructed some paper cubes and drew the graph on two large pieces of art paper, X & Z as the base with Y drawn on the second piece of paper, propped up perpendicular to the X, Z page.

At this point we jumped into some basic designs.  With the API that comes with Minecraft Pi you construct a cube or cuboid by setting the opposite corner points.  It does a lot more, but we focused on the geometry.
From the center of the world (X,Y,Z format) (0,0,0) to (5,5,5) will give you a cube that is 5 wide, 5 long and 5 deep and a solid block.


Don't use VNC, rather work with the Pi plugged into a monitor.
I've shot some pictures of the TV to show this:


(Using VNC I get only a black screen where Minecraft is so I couldn't take any screen shots except ones to show the error)
 This is from my laptop screen shot.


So from here on out, the picture and video quality is crummy, I shot an HDTV with my phone while I used the mini-keyboard so sorry for the shakyness.



Now thinking through a 3D design is an easy first step: Build a big block

<-- This one floats so the kids could see that it was a full cube, not just resting on the ground.

(I missed the picture of the floating block but you can see it briefly in the videos below)

Then it gets interesting.  Fill a big cube with a slightly smaller cube of type 'air'.  This gives you a hollow cube.  It just gets better from here; where will you place your windows and make sure that you are cutting out 'air' holes that fit with your initial cube.  Will you make a fence around everything by laying down four lines or a large-flat cube of fence which is then filled with a smaller-flat cube of air?

The kids took their designs home and planned out their models, they spent hours on this for a few nights (none of this was homework, it was originally just to explore X,Y,Z space and have fun).
They brought in their pages that looked like this:


16,0,16,30,5,30,4    <-- their plans were lists like this
(X  ,Y,Z,  X2, Y2, Z2, blocktype)   <-- this is what the numbers mean

the x2, y2, z2 represent the opposite corner of the cuboid.  The above block would be 14units wide, 4 units tall and 14 units deep/long and made of granite?(4)

I had typed in each line at first like this: mc.setBlocks(16,0,16,30,5,30,4) which will set a nice cube.  After showing the class how this worked I quickly switched to using the proper variables.  Most of the class was comfortable with variable data from Scratch and they were gaining confidence with using ideas like X+5 within the plans. Again, this is above grade level, but it was embraced by the students and they were able to do it.  Gaining confidence, they weren't quite comfortable with all aspects of variables representing numbers, but they will certainly be ready for these concepts when they pop up in math in middle school.

The first thing to do is to clear your world
 


...and if you are like me, you spend your lunch break adding your name to the clear function so it always pops up in your world.  


A 'clear' script is very very useful when you are throwing kids scripts at Minecraft one after another.  
The first two lines get everything rolling.  The fourth clears everything  by putting a 0 'air' block everywhere and the fifth line rebuilds the ground.  The comments on line seven show the letters.

Here is the script:
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

mc.setBlocks(-127, 0, -127, 127, 30, 127, 0)
mc.setBlocks(-127, 0, -127, 127, 1, 127, 2)

#j
mc.setBlocks(3,3,0,3,5,0,4)
mc.setBlock(4,3,0,4)
mc.setBlocks(5,3,0,5,10,0,4)
#e
mc.setBlocks(7,3,0,7,10,0,4)
mc.setBlocks(7,3,0,10,3,0,4)
mc.setBlocks(7,6,0,9,6,0,4)
mc.setBlocks(7,10,0,10,10,0,4)
#f
mc.setBlocks(12,3,0,12,10,0,4)
mc.setBlocks(12,6,0,14,6,0,4)
mc.setBlocks(12,10,0,16,10,0,4)

#f
mc.setBlocks(18,3,0,18,10,0,4)
mc.setBlocks(18,6,0,20,6,0,4)
mc.setBlocks(18,10,0,22,10,0,4)


I did very little correcting of the student's plans.  Rather, I entered in all of their plans (coordinates) and then let them walk around their creations and find where their bugs were.  Mostly the attempts at doors and windows were off at first and the kids had to renegotiate their X or Z positions to get everything in the right spot.

Here is one of the kids scripts that got a little more complete and interesting.
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.setBlocks(-80,-2,-80,120,55,120,0)
mc.setBlocks(-80,-2,-80,120,0,120,2)
x1 =0
y1 =0
z1 =0

x2 =105
y2 =2
z2 =105

bt = 85  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)

x1 =1
y1 =0
z1 =1

x2 =104
y2 =2
z2 =104

bt = 0  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)


x1 =2
y1 =0
z1 =2

x2 =25
y2 =10
z2 =25

bt = 57
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)

x1 =3
y1 =0
z1 =3

x2 =24
y2 =9
z2 =24

bt = 89  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)


x1 =4
y1 =0
z1 =4

x2 =23
y2 =8
z2 =23

bt = 0  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)


x1 =2
y1 =0
z1 =2

x2 =4
y2 =4
z2 =4

bt = 0  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)

mc.setBlock(7,4,3,50)
mc.setBlock(20,4,20,50)
mc.setBlock(19,1,19,89)

x1 =2
y1 =11
z1 =2

x2 =25
y2 =21
z2 =25

bt = 78  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)


x1 =30
y1 =-10
z1 =0

x2 =49
y2 =0
z2 =34

bt = 49  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)

x1 =31
y1 =-9
z1 =1

x2 =48
y2 =0
z2 =33

bt = 8  
mc.setBlocks(x1,y1,z1,x2,y2,z2,bt)

mc.setBlock(35,-1,2,53)
mc.setBlock(35,-2,3,53)
mc.setBlock(35,-3,4,53)
mc.setBlock(35,-4,5,53)
mc.setBlock(35,-5,6,53)

This was about everything we had time to do.  I did show them how functions and loops work just for fun and we built this:


Change one parameter and you get a whole new set of houses.



import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
import time
#clear land
mc.setBlocks(-120,0,-120,50,50,50,0)
#Build ground
mc.setBlocks(0,-1,0,50,-1,50,1)

def house(x,y,z,bt,rt):
#walls
mc.setBlocks(x,y,z,x+4,y+10,z+10, bt)
xr=x-1
yr=y+11
zr =z-1
xr2 = x+5
zr2 = z+1
#hollowroom
mc.setBlocks(x+1,y+1,z+1,x+3,y+9,z+9,0)
#door
mc.setBlocks(x+2,y,z,x+3,y+2,z,0)
#roof
c=0
while c<5:
mc.setBlocks(xr,yr,zr,xr2,yr,zr2, rt)
yr+=1
xr+=1
xr2-=1
zr+=1
zr2-=1
c+=1

xr=x-2
yr=y+5
zr =z+2
xr2 = x+5
zr2 = z+5
c=0
#while c<5:
# mc.setBlocks(xr,yr,zr,xr2,yr,zr2, 0)
# yr+=1
# xr+=1
# xr2-=1
# zr+=1
# zr2-=1
# c+=1
def rowhouses(x,y,z,bt,rt):
c=0
while c < 12:
house(x,y,z,bt,rt)
x+=10
time.sleep(5)
c+=1

rowhouses(0,0,0,4,2)

The sleep function allowed us to watch things happen in real time in the game.  It was much more powerful in explaining the concept than anything else I had tried.

Where should you get started?

1 comment:

  1. This is my second time to try comment so sorry if it comes up twice. Great work Jeff, I will use this to teach coordinates in the future, God willing!

    ReplyDelete