Pro Topic

'air' block

Since variable materials is a Python dictionary, we can add a new key:value pair using value assignment:

dict[key]=value.

We can insert a material to represent empty block(means it's clear, invisible, it's value in CodeCraft is 0), let's name it 'air'.

# add new key:value pair to materials dictionary
materials['air'] = 0 
print(materials)

console result:

{'brick': 1, 'cobblestone': 2,.... 'wool_yellow': 76, 'air': 0}

Since air block indicating a invisible block, we can test it by making an existing block disappear. First we build a brick column, then in the middle of the column, build another air block.

By the way, let's try the new function blockM(x,y,z,material)

test the 'air' material

# put up a brick column
for j in range(10):
    blockM(0, j, -10, 'brick')

# make the middle block disappear
blockM(0, 5, -10, 'air')

# build a cobblestone column, and make every other block disappear
column(3,-10, 10, 2)

for j in range(1,10,2):
    blockM(3, j, -10, 'air')

See the picture at the end of the lesson.

Function dig(x,y,z)

Many of you have been wishing to be able to 'click' and 'dig' holes in the game world, while we can not do 'click' yet (wait for the advance topic: events and callback function), we can now for sure to define a function to 'dig'.

dig(x,y,z)

def dig(x,y,z):
    game.set_block(Position(x,y,z), materials['air'])

Check out the picture:

A hole in the wall

# wall (use column to build a blue wall)
for i in range(5, 10):
    column(i,-10, 10, 62)

# make a few holes in the middle
dig(5,5,-10)
dig(6,8,-10)
dig(8,5,-10)
dig(9,9,-10)

===end

((delete below??

I can make a function of making a column disappear, let's call it vanishColumn, later when we learn how to link events with functions, we can make a magic key on the keyboard(or mouse click) that can make columns disappear.

# vanishColumn
def vanishColumn(x,y,z,h):
  for j in range(y,y+h+1):
    game.set_block(Position(x,j,z), materials['air']


# bigHole make a big hole(w*h) at (x,y,z)
def bigHole(x,y,z,w,h):
for i in range(x,x+w+1):
for j in range(y,y+h+1):
game.set_block(Position(i,j,z), materials['air'])
bigHole(3,4,-10,5,2)

# 3D hole
def hole(x,y,z,w,h,d):
for i in range(x,x+w+1):
for j in range(y,y+h+1):
for k in range(z, z+d+1):
game.set_block(Position(i, j, k), materials['air'])

results matching ""

    No results matching ""