Pro Topic: 'air' block

You may have noticed that there's no function that lets you delete a block from a space in the game world. We can fix that by creating a new 'air' material, which can replace an existing block and act like the delete button.

Since the 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 with value 0. Let's name it 'air'.

# add new key:value pair to materials dictionary

materials['air'] = 0 
print(materials)

console result:

{'box_black': 1, 'box_blue': 2,.... 'tnt': 88, 'air': 0}

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

Test the 'air' material

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

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

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

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

See the picture at the end of the lesson.

New function: dig(x, y, z)

Many of you have wanted to be able to click and dig holes in the game world. While we cannot click yet (this will be covered later when we talk about events and callback functions), we can now define a function to "dig" holes in the ground or an existing structure:

def dig(x, y, z):
    block(x, y, z, 'air')

Let's call dig() at some locations on a wall. Check out the picture at the bottom for reference.

Holes in the wall

# build a blue wall using columns
for i in range(5, 10):
    column(i, -10, 10, 'linen_blue')

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

results matching ""

    No results matching ""