Bars and Flats
More application of for
loop and functions
The two functions below will be used frequently in this book. Function block()
does the same thing as game.set_block()
, except that it accepts the coordinates and material name directly. column()
builds a post from (x, 1, z)
to (x, h, z)
, using h
number of blocks.
def block(x, y, z, m):
game.set_block(Position(x, y, z), materials[m])
def column(x, z, h, m):
for j in range(1, h + 1):
block(x, j, z, m)
Let's look at a few examples.
Example 1: 3 lines of cobblestone blocks along x, y and z directions.
for i in range(10):
block(i, 1, -20, 'cobblestone') # loop through x
block(0, i, -20, 'cobblestone') # loop through y
block(0, 1, i-20, 'cobblestone') # loop through z
Example 2: Disconnected blocks along x, xy and xyz directions.
# loop x
for i in range(1, 20, 2):
block(i, 10, -20, 'linen_yellow')
# loop xy
for i in range(1, 20, 2):
block(i, i, -20, 'linen_blue')
# loop x,y,z
for i in range(1, 20, 2):
block(i, i, -i, 'linen_red')
# add two y lines for reference
for j in range(1, 20, 2):
block(0, j, -1, 'obsidian')
for j in range(1, 20, 2):
block(19, j, -20, 'obsidian')
Example 3: Using nested loops we can make a two-dimensional array of blocks:
for i in range(1, 10, 2):
for k in range(-15, -5, 2):
block(i, 15, k, 'box_lime')
Here are all of the examples we just mentioned:
We can easily build multiple columns by putting column()
inside a for
loop:
Example 4: A line of columns along x axis, every 5 blocks
for i in range(0, 30, 5):
column(i, -21, 10, 'cobblestone')
Example 5: Columns with different height and materials
# columns with increasing height
for i in range(11, 20, 2):
column(i, -10, 5+i, 'linen_lime')
# columns with different materials
for i in range(1, 10, 2):
column(i,-10, 10, 'box_lime')
column(i+10, -10+i, 10, 'box_pink')
column(-i, -10+i, 10, 'box_purple')
Front view of example 5:
Side view of example 5: