Here is a copy of the first part of every CodeCraft application

# Part I
from codecraft import Game, Position

game = Game()   
game.clear_console()  

materials = game.materials  

print('Hello, buzzCoder!') 
print(materials) 

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

# First block

game.set_block(Position(2,2,-10), materials['obsidian'])

Chapter 2 Build with for loops and range( ) function

2-01 Review For-loop and range():

For loop is to repeat a block of code a set number of times.

For loop format:

for var in sequence:    # For-loop header, end with ':'
    code block          # Standard Indention! 4 spaces!
    code block
...

range (start,stop,step) Function is a Python built-in function that will generate integers in list from the start number to stop number(not including) with given step. (start default is 0, step default 1, stop number can not be omitted.)

Use range() function in a for loop:

>>> for i in range(1,10,2):
>>>     print(i)
1
3
5
7
9

Note: If you need to know more about for loop and range() function, check reference chapters(R2, R4) or check out other book: Python Basic:**(link of Basic Python:) https://python-basic.buzzcoder.com/

2-02 Build a column in CodeCraft

If you build only one block at a time, to build a 3 blocks tall column you need 3 lines of:

game.set_block(Position(0,1,-10),materials['brick']
game.set_block(Position(0,2,-10),materials['brick']
game.set_block(Position(0,3,-10),materials['brick']

Happy typing? Would you build a column 50 blocks tall?

It's very simple to build a column with for loop and range() function.

# 2-02-1column
for j in range(10):
    game.set_block(Position(10,j,-10), materials['dirt'])

# this column is actually 10 blocks tall start at y location 0, but we can only see 9 blocks in height.

Here is a simple explanation:

range(10) will generate a list of integers from 0 to 9, (10 is not included)

j is each item in the list of [0,1,2,...,9]. for j in range(10) will iterate through the number list and each time do the code blocks under the for loop header line, that is, call the function set_block() of game object to lay a block at Position (10, j, -10).

After the loop is over, there will be 10 blocks total at locations: (10,0,-10), (10,1,-10), (10,2,-10),..., (10,9,-10), together they made up a column of 10 blocks high at horizontal location(x=10, z=-10).(We can only see 9 blocks above the ground).

To build a column starting from j=1 to j=10, we can use j in range(1,11)

Imagine how easy to build a column of 100 blocks tall. But I don't have time for that, let's keep going.

2-02-2 lineOfBlocks Next, I will put a line of single blocks, each is 2 blocks away from others:

# 2-02-2 lineOfBlocks
for i in range(0, 30, 4):
    game.set_block(Position(i, 1, -15), materials['planks_oak'])

See the power of loops a little bit?

2-03 We can use loops within loops.

First, imagine for a second, how many lines needed to build a wall without loops? Then you will be more willing to keep learning.

Review an example to use loop within loop:

for i in range(3):   # items in [0,1,2]
    for j in 'abc':   # letters in 'abc'
        print(i, j)
# run
0 a
0 b
0 c
1 a
1 b
1 c
2 a
2 b
2 c

Then let's see the power of for loop in CodeCraft:

I like to pair i for x locations, j for y, k for z.(Good habit that will make your life easier when you learn Geometry and Physics)

Brick wall: 5 times 10(w*h)

# 2-03-1A brickWall

for i in range(5):    # iterate x location from 0 to 4.
    for j in range(10):  # iterate y location from 0 to 9
        game.set_block(Position(i, j, -15), materials['brick'])

Brick wall: 5 times 10(w*h)

# 2-03-1B brickWall

for i in range(5):    # iterate x location from 0 to 4.
    for j in range(1,11):  # iterate y location from 1 to 10
        game.set_block(Position(i, j, -15), materials['brick'])

A line of columns

# 2-03-2 lineOfColumns

for i in range(0, 30, 4):
    for j in range(10):
        game.set_block(Position(i, j, -18), materials['planks_oak'])

A line of columns variation:

# 2-03-3
for i in range(5,36,5):
    for j in range(1,4):
        game.set_block(Position(i,j,-10), materials['dirt'])
        game.set_block(Position(i+2,j+2,-10), materials['cobblestone'])

A line of high-low columns:

# 2-03-4
for i in range(1,31,4):
    for j in range(4):
        game.set_block(Position(i,j,-10), materials['dirt'])

    for k in range(6):
        game.set_block(Position(i+2,k,-10), materials['cobblestone'])

The image: A checkerboard wall

#2-03-5 checkerboard

for i in range(1,11,2):
    for j in range(1,11,2):
        game.set_block(Position(i, j, -5), materials['brick'])
        game.set_block(Position(i+1, j, -5), materials['quartz'])
        game.set_block(Position(i, j+1, -5), materials['quartz'])
        game.set_block(Position(i+1, j+1, -5), materials['brick'])

A door structure

# 2-03-6 door structure

for i in range(1,6):
    for j in range(11):
        game.set_block(Position(1, j, -5), materials['brick'])
        game.set_block(Position(5, j, -5), materials['brick'])
        game.set_block(Position(i, 10, -5), materials['brick'])

Some extra funny looking tree for you to enjoy:

Tree-funny looking

# 2-03-8A funnyTree
for j in range(20):
    game.set_block(Position(0, j, -5), materials['wood'])
    game.set_block(Position(-1, j+10, -5), materials['leaves'])
    game.set_block(Position(1, j+10, -5), materials['leaves'])

Tree-

# 2-03-8B 2DTree
for j in range(20):

    game.set_block(Position(10, j, -5), materials['wood'])

    for l in range(10, 20, 2):
        game.set_block(Position(9, l, -5), materials['leaves'])
        game.set_block(Position(11, l+1, -5), materials['leaves'])

Tree-3D

# 2-03-8C 3DTree
for j in range(20):
    game.set_block(Position(15, j, -5), materials['wood'])

    for l in range(10, 20, 2):
        game.set_block(Position(14, l, -5), materials['leaves'])
        game.set_block(Position(16, l+1, -5), materials['leaves'])

        game.set_block(Position(15, l, -6), materials['leaves'])
        game.set_block(Position(15, l+1, -4), materials['leaves'])

Check later chapters for a better looking 3D-Tree using random() function to mimic real tree leaves that appears at random locations. And more, using time delay we can imitate a growing tree.

Stair structure


# 2-03-7 stairs
for i in range(10, 20):
    for k in range(-20, -10):   
        game.set_block(Position(i, i-9, k), materials['brick'])

Now you can see we are going somewhere!

results matching ""

    No results matching ""