block(x,y,z,m) block_m(x,y,z,material)

All about CodeCraft materials

Variable materials is a dictionary

As you can see the console result of the printed materials is in the form:

{'brick': 1, 'cobblestone': 2, 'diamond': 3,...'wool_yellow': 76}

materials is a dictionary of 76 key:value pairs.

All the keys are names of the block materials, they are paired with integer value from 1 to 76 representing 76 different blocks.

((?? not use?

print(materials.keys())
# all the keys(names of blocks) of materials will be printed in the console
print(materials.values()
# integers from 1 to 76 will be printed in the console

))

Two ways to build a diamond block

We can use the materials' value directly as before, or we can use dict[key] to access key value. For example: materials['diamond'] points to integer 3, see it in CodeCraft:

# build a diamond block using integer 3 indicating the material 'diamond'

game.set_block( Position(0,2,-10), 3 )

# build a diamond block on top of the previous one , using materials['diamond'] to access diamond material

game.set_block( Position(0,4,-10, materials['diamond'] )

From the above example you can see, using materials['diamond'] or integer 3 will actually do the same thing: accessing the material 'diamond'.

CodeCraft makes it possible for us to visualize the dictionary key and value's relationship.

New function to build a block

Previously we always use integer indicating block material, such as in function to build a block:

block(x,y,z,m)

But sometimes it's convenient and easy to just use the block material's name directly.

Based on block(x,y,z,m), I will define a new function blockM(x,y,z,material) to accept a string name as the material input.

# previous block(x,y,z,m), m is an integer (1-76)

def block(x,y,z,m):
    game.set_block(Position(x,y,z), m)

# new blockM(x,y,z,material), material is a string value such as 'brick'

def blockM(x,y,z,material):
    game.set_block(Position(x,y,z), materials[material])

Demo these two functions:

# brick blocks:
block(0,3,-10, 1)             # at left
blockM(2,3,-10, 'brick')      # at right

# blue blocks:
block(0,5,-10, 62)              # at left
blockM(2,5, -10, 'wool_blue')   # at right

You see, both of the functions basically do the same thing, pick which one to use according to the situation.

blockM(x,y,z,material) is easy to recognize the building material

block(x,y,z,m) is good for iteration over material values

Iterate over materials

We can use for loop to iterate over the keys in materials dictionary, keys in materials are names of different material blocks.

i = 1
for k in materials:
    print(k)
    blockM(i, 10, -20, k)
    i += 1      # x location increase 1 for each key

Run the above code, the names of block materials are printed out in the console; You can see in the game world we create a line of blocks, at x locations from 1 to 76 there are all 76 kinds of blocks.

We can do the same thing with function block(x,y,z,m), use for loop to iterate over all the numbers indicating material values:

for i in range(len(materials)):
    block(i, 15, -20, i+1 )

This line of all blocks locates on top of the previous line.

What if we like to iterate only a potion of the material dictionary? Then material numbers are more easy to use than material name strings.

Example, Display some color blocks

It's easy to display some color blocks using iteration over material number values.

I like the last few colorful blocks in materials: 'wool_black'...'wool_yellow', (values from 61 to 76). Let's display them here:

y = 2
for i in range(61, 77):
    block(0, y, -20, i)
    y += 2

See the colorful blocks at y locations: 2,4,6...,34.

Display blocks

Display all blocks in CodeCraft world

Now it's time to go back to understand the lines of code to show all blocks in CodeCraft Part I code:

# to show all blocks (optional)
for i in range(len(materials)):
    game.set_block(Position(int(i / 4) * 2, (4 + (i % 4) * 2), -20), i + 1)

len(materials) returns the length of materials dictionary, 76. In materials, all the keys (name of block material) point to integer values from 1 to 76, so we use integer directly to represent all the block materials.

int(i/4)*2 group every 4 blocks in one column, for example, for i value: 0,1,2,3 in the first column at x=0; 4,5,6,7 in the second column at x=2...; y location of each block is (4+(i%4)*2), that is, for i=0,1,2,3, (i%4) returns the reminder, 0,1,2,3, so each block has y value at: 4,6,8,10; For i=4,5,6,7, each block also at y location: 4,6,8,10... For each block material, we use integer (i+1) directly (value from 1 to 76) instead of using materials[material] to point to the value. The same as in previous example, using integer 3 or materials['diamond'] all indicate the same 'diamond' block material.

Thus, we display all the blocks in 4* 19 array at x locations: 0,2,4,6...38; y locations: 4,6,8,10.

((copy of edited stuff in the "CodeCraft with Python" final book:

Two ways to iterate over materials

One: iterate over the material strings in block(x, y, z, material)

We can use for loop to iterate over the keys in materials dictionary. Keys in materials are string names of different material blocks.

x = 1
for k in materials:
    print(k)
    block(x, 10, -20, k)
    x += 1      # x location increase 1 for each key

Run the above code, the names of block materials are printed out in the console, such as 'box_black', 'box_blue' ...

In the game window you will see a line of blocks with all available materials.

Two: iterate over the number m in block_m(x, y, z, m)

We can do the same thing with function block_m(x, y, z, m), use for loop to iterate over all the numbers indicating material values:

for i in range(len(materials)):
    block_m(i, 15, -20, i + 1)

This line of all the blocks look exactly the same as the previous one below it. They look like a pair of chopsticks.

What if we want to iterate only a potion of the material dictionary? In that case material numbers are easier to use than material name strings.

Display a part of the color blocks

It's easy to display some color blocks using iteration over material number values.

I like some colorful blocks in materials: 'linen_black'...'linen_yellow', (values from 44 to 59). Let's display them here:

y = 2
for i in range(44, 60):
    block_m(0, y, -20, i)
    y += 2

See the linen series colorful blocks in the vertical line.

Display all blocks in an array

Now it's time to go back to understand the lines of code to show all blocks in CodeCraft Part I code:

# to show all blocks (optional)
for i in range(len(materials)):
    game.set_block(Position(int(i / 4) * 2, (4 + (i % 4) * 2), -20), i + 1)

len(materials) returns the length of materials dictionary (currently 88).

int(i/4)*2 group every 4 blocks in one column, for example, for i value: 0, 1, 2, 3 in the first column at x = 0; 4, 5, 6, 7 in the second column at x = 2; ...

y location of each block is 4 + (i % 4) * 2, that is, for i=0, 1, 2, 3, (i%4) returns the reminder, 0, 1, 2, 3, so each block has y value at: 4, 6, 8, 10; For i=4, 5, 6, 7, each block also at y location: 4, 6, 8, 10 ...

Thus, we display all the blocks in 4 * 22 array at x locations: 0, 2, 4, 6..., and y locations: 4, 6, 8, 10.

))

results matching ""

    No results matching ""