CodeCraft-Build your Virtual World

Chapter 1: First Block

First Block code

# part I
1 from codecraft import Game, Position
2
3 game = Game()
4 game.clear_console()
5 print('Hello, CodeCraft!')
6
7 materials = game.materials
8 print(materials)
9 
10 # part II build one block
11 p1 = Position(0,1,-10)
12 game.set_block(p1, materials['quartz'])

line 1 from codecraft import Game, Position

1-01 Module, import

Modules are Python's solution to manage code. We put related functions and variables in a file with a .py extension, this file is a module.

In order to use a module, we import that file into our current program:
import moduleName

Whenever you see import in a program, you know the programmer is bringing in code from elsewhere.

After we import a module, we can then access the attributes (functions and variables) in that module using dot notation:
moduleName.itemName

Another way to import something is to use
from moduleName import itemName

This way we can save some typing and access the item directly without dot notation.

Application
Application in CodeCraft, in line1 we can do:

from codecraft import Game, Position

Game and Position are Python classes, they are attributes of codecraft module.Line1 import Game and Position classes from codecraft module, then we can use these classes in the following code.

1-02 Basic on types(classes), objects

line3,4: game=Game(), game.clear_console()

(Please check Reference Class and Object for more detailed explanation)

A class creates a new type where objects are instances of the class.

A class is essentially a blueprint for creating an object of a particular kind.

If you feel difficult to understand all these new vocabularies, let's use an example:

Chair type/Chair class
suppose we have a type(or class) representing a chair:
Chair(color, legNumber)

Chair( ) type has two input parameters color, legNumber;
It also has a function say() that can print out message:'I'm a (color) chair with (legNumber) legs.'

Next line, we create a Chair object, chair1, providing two values for the parameters:

chair1 = Chair('yellow', 4)

Now we can access it's method(function belongs to object are called method) using dot notation:

chair1.say() # see the printed out message below:

I'm a yellow color chair with 4 legs.

More Chair objects:

chair2 = Chair('brown', 3)
chair3 = Chair('red', 6)
chair2.say()
# Run...result
I'm a brown color chair with 3 legs.

chair3.say()
# Run...result
I'm a red color chair with 6 legs.

When an object is created using class, the resulting object is called an instance of the class.
chair1, chair2, chair3 are instances of Chair class(type)

The same as Chair type(or class), in Python, we can create any types we like such as type Alien(eyeNumber, headShape), then by changing the initializing values, we can have:

8 eyed square head Alien object,
5 eyed triangle head Alien object...

They are all instances of Alien class (type).

Now back to CodeCraft

Game(), Position() are Python types, also called classes

...
3 game = Game()         # make a Game object, name it game
4 game.clear_console()  # access it's method to clear the console
5 print('Hello, CodeCraft!')    # test the console
...

In this part we mainly create game object as the instance of Game class, we access it's object method using dot notation:
game.clear_console()
then we test the console by printing out a message "Hello"

1-03 Dictionary

line 7 materials = game.materials

We access an attribute named materials of game object using the dot notation again(game.materials), then we assign it to a new variable in our program also named materials

materials, some words on Dictionary
(Please refer Chapter Dictionary for more information)

materials itself is a dictionary that has many elements(or key:value pairs).

Through line 8
8 print(materials);

We print out materials to see it's a dictionary including 76 key:value pairs enclosed in curly bracket { } representing the different block materials in CodeCraft world:

{"brick":1,"cobblestone":2,"diamond":3,"dirt":4,"grass":5, ... "wool_yellow":76}

We can access key's value by using dictionary[key]

Here in materials dictionary, we can refer to the value of 'brick' via materials['brick'], we can see it's value is integer 1.

1-04 Position(x,y,z) and 3D CodeCraft world coordinates

10 # part II build one block
11 p1 = Position(0,1,-10)
12 game.set_block(p1, materials['obsidian'])

Position(x,y,z) has 3 parameters: x,y,z. In CodeCraft world, x,z indicate the location on horizontal plane: x points to the right; z points out of the screen toward us; y means vertical location, pointing up.

Code in line 11, creates a new object of Position type(also called class).
p1 as Position(0,1,-10) object. it represents the point (0,1,-10) in the 3D CodeCraft world, which is at x origin 0, y at 1 block high just above the ground, and z at -10 (10 blocks away from the origin).

Finally time to set the first block
With all the above preparing work, time for the reward: are you ready to see your very first block in CodeCraft world?

12 game.set_block(p1, materials['obsidian'])

Click Run button at the top then click the picture screen to enter the CodeCraft world to see the Mod. Click the reload button to clear the picture screen each time to run a new Mod. Esc button is for shifting back to code window.

In line 12, we call the function set_block() of game object, it needs two input values: a Position object and value for materials['obsidian']. This line set an obsidian block at location: (0,1,-10).

Next is your turn, after line 12, please make two other Position objects: p2, p3, at two other location and then call set_block() function on them with different materials.

Answer can be:

14 p2 = Position(4, 2, -10)
15 p3 = Position(8, 3, -10)
16
17 game.set_block(p2, materials['dirt'])
18 game.set_block(p3, materials['brick'])

1-04 Exercise:

Build some blocks :

Start from the beginning (import line)
create a new Game object
clear the console window
create new variable materials that has the value of game.materials

In the console window, show message 'Hello, CodeCraft!' for console testing
In the console, show all materials, you can see it's a dictionary

Next, create 3 new Position objects, b1,b2,b3 to represent 3 locations in the codecraft world: (0,1,-10), (3,1,-10), (6,1,-10)

Call set_block() function of game object on b1,b2,b3 seperately and some kind of material

Run your mod, Ta Da!!
See? you set 3 dirt blocks from left to right.

Reference code:

# Part I
...
# Part II
# 3 blocks
b1 = Position(0, 1, -10)
b2 = Position(3, 1, -10)
b3 = Position(6, 1, -10)

game.set_block(b1, materials['dirt'])
game.set_block(b2, materials['cobble_stone'])
game.set_block(b3, materials['obsidian'])

Commonly used code to build one block

Providing Position object directly as input without assigning it to a variable, like:

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

I also like to add a few lines to Part I to show all the different block materials in CodeCraft world so you can easily pick from them. You don't have to understand the code for now, we will learn every thing soon.

New Part I code, First Block:

# CodeCraft Part I 
from codecraft import Game, Position

game = Game()
game.clear_console()

materials = game.materials

print('Hello, CodeCraft!')
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'])

Here is the image of the 'PyFirstBlock', all block materials are at the background:

1-05 Blocks, More Blocks

Want to try more:

1.Make a column at horizontal location (10,-10) that is 3 blocks tall

2.Make a wall at different location that is 2_3 (width_height)

Column and Wall

# I will use Position objects in set_block without assigning them to variables from now on

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

#  1-05wall 
game.set_block(Position(12,1,-10), materials['dirt'])
game.set_block(Position(12,2,-10), materials['dirt'])
game.set_block(Position(12,3,-10), materials['dirt'])
game.set_block(Position(13,1,-10), materials['dirt'])
game.set_block(Position(13,2,-10), materials['dirt'])
game.set_block(Position(13,3,-10), materials['dirt'])

# 1-05 checkerboard
#Let's make a checkerboard lay on the ground

game.set_block(Position(6,0,-10), materials['dirt'])
game.set_block(Position(7,0,-10), materials['quartz'])
game.set_block(Position(8,0,-10), materials['dirt'])

game.set_block(Position(6,0,-11), materials['quartz'])
game.set_block(Position(7,0,-11), materials['dirt'])
game.set_block(Position(8,0,-11), materials['quartz'])

game.set_block(Position(6,0,-12), materials['dirt'])
game.set_block(Position(7,0,-12), materials['quartz'])
game.set_block(Position(8,0,-12), materials['dirt'])

Great patience on typing!
Do you have time to make a wall 100*200 or even bigger piece of checkerboard?

I will rewrite code to make a column:

# 1-05B forColumn
for y in range(10):
    game.set_block(Position(8,y,-10), materials['dirt'])

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

Next make a wall 5_10(w_h) in just a few lines:

# 1-05BforWall
for i in range(5):    # make a wall 5*10 (w*h)
    for j in range(1,11):
        game.set_block(Position(i, j, -15), materials['quartz'])

Ha! Would you like to learn about for loop?

Next topic
for loop, range( ) function

Common errors

  • Pay attention to parenthesis
  • Pay attention to upper/lower cases

  • z axis is pointing toward us out of the screen plane, use a negative number for z so we can see the Mod from a distance

  • use triple quotes to comment out codes you don't want to run

  • for loop indention (4 spaces), header line ending colon :

  • range(start,stop,step), for vertical axis y, y=1 is the first block above ground we can see, not y=0;

Notes

If it's difficult for you now to understand all the knowledge mentioned here about module, class, object, dictionary..., don't worry too much. Just try to get some ideas, pay attention to the structure of the code and use them for now. As the lessons go on, we will get to each concept in detail sooner or later. Back and forth is a good way to learn new things. We will start with one block, slowly you will learn to build a mountain. If keep going, you sure will climb the mountain of coding and enjoy the view at the top!

==The end of chapter

I'd like to put some pictures of the structures we can learn to build later just for you to enjoy:

Are you ready to keep learning?

results matching ""

    No results matching ""