City in Minecraft created entirely out of code.
What are functions?
Functions are a convenient way to split your code into useful re-usable blocks. Functions can make your code easier to read (useful when debugging code) and make your code more efficient.
To find out more about functions in MakeCode, check out this previous lesson: Minecraft Education Edition: Functions
To find out more about functions in MakeCode, check out this previous lesson: Minecraft Education Edition: Functions
Part 1 - Building the basic structure
1. Launch the Microsoft 'MakeCode' editor and create a new project.
2. Copy the code exactly as it appears below (alternatively, you can download the code sample below).
2. Copy the code exactly as it appears below (alternatively, you can download the code sample below).
3. Open Minecraft and click on 't' top open the chat window.
4. Type in 'skyscraper' in the command line (followed by the Enter key) to run your code.
4. Type in 'skyscraper' in the command line (followed by the Enter key) to run your code.
Resources:
minecraft-skyscraper-1.mkcd |
Part 2 - Refining your code
1. Go into the java script view (Under the hood view) by clicking the JAVASCRIPT button at the top of the editor.
2. Click on line 14 and type in the following:
3. Select lines 5 to 11. Cut the highlighted lines of code (A) and paste them (B) onto line 16.
4. Go to line 5 and type in the following:
buildfloor()
|
Your code should look like this:
5. Open Minecraft and click on 't' top open the chat window.
6. Type in 'print' in the command line (followed by the Enter key) to run your code.
Challenge:
6. Type in 'print' in the command line (followed by the Enter key) to run your code.
Challenge:
- Change the size of the skyscraper by modifying the height and width. The current dimensions are: height = 10 and width = 5.
- Create new variables to store the height and width. Generate a random number for the height and width to create a different size skyscraper each time you run the code.
- Comment your code to explain how the code works.
Resources:
minecraft-skyscraper-2.mkcd |
Part 3 - Creating new floors
Now that we have made the code to create a floor a new function, we can do something really cool.
Let's modify our new function to allow it to pass variables so that each time we call our function we can define which blocks to use.
1. Click on line 5 and modify the code as follows:
Let's modify our new function to allow it to pass variables so that each time we call our function we can define which blocks to use.
1. Click on line 5 and modify the code as follows:
buildfloor(blocks.block(Block.Stone))
|
2. Click on line 9 and add the following code inside the two closed brackets?:
blocktype: number
|
3. Click on line 13 and modify the code as follows:
builder.line(blocktype)
|
4. Next, let's create a variable to store our blocktype. Insert a new line at line 13. Type in the following code:
item = blocktype
|
5. Finally, insert a new line after line 6 and type in the following code:
buildfloor(blocks.block(Block.Glass))
|
Your finished code should look like this:
5. Make sure your code looks exactly as shown above.
Open Minecraft and click on 't' top open the chat window.
6. Type in 'print' in the command line (followed by the Enter key) to run your code.
Open Minecraft and click on 't' top open the chat window.
6. Type in 'print' in the command line (followed by the Enter key) to run your code.
Challenge:
- Add new floors or use different blocks (such as colored glass or TNT).
- Modify your code so that it creates a city full of skyscrapers (each a different size).
Resources:
minecraft-skyscraper-3.mkcd |
minecraft-city-grid-final.mkcd |
You may also be interested in:
This work is not affiliated in any way with Mojang AB. Minecraft is a trademark of Mojang AB. The Minecraft Name, the Minecraft Brand and the Minecraft Assets are all property of Mojang AB or their respectful owner. This work adheres to the terms set out by Mojang AB at https://www.minecraft.net/terms