Happy Day! MakeCode for Minecraft on Windows 10 Is Back!

No doubt about it. I’ve tested on 3 computers: Code Connection maintains a connection. MakeCode for Minecraft on Windows 10 is working perfectly.

I ran the test program from yesterday’s post Missing Monsters in MakeCode for Minecraft Education Edition and, sure enough, all monsters are present in MakeCode for Minecraft on Windows 10.

Get the Code

Monsters code is shared on Code Connection at this URL

https://makecode.com/_0MAW16htVifj

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

Missing Monsters in MakeCode for Minecraft Education Edition

 

This program, which we’ll dub “Missing Monsters,” is designed to test that monsters spawn as they should in MakeCode for Minecraft Education Edition. As of this writing, only Shulker, Vindicator and Evoker spawn correctly. When trying to spawn a ghost, something pink appears briefly; however, no ghost is spawned. All the other monsters simply do not spawn. When the post Monster Showtime in MakeCode for Minecraft was published, all monsters spawned as expected in MakeCode for Minecraft on Windows 10.

Using the Missing Monsters Program

Missing Monsters offers the following 3 on-chat commands:

  • run
  • go
  • ghosts

Run

The run command is used first. It will cause “ready to go” to be printed in the sky. The player should face so that “ready to go” can be seen.

Go

The go command is used after the player is positioned  so that the sky printing is visible. After the go command, the player need not be moved — hands off.  Each monster will be separately spawned with its name written in the sky. To allow for viewing time, there is a brief wait after each monster. Each monster’s name will be printed in the sky. If the monster is spawned correctly, it will appear a few blocks in front of the player. Be patient — the correct ones are at the end of the monster list. Vex is the last monster.

Ghosts

The ghosts command can be used after go has completed. This command will generate 10 ghosts, one at a time, with a brief wait after each one.

Ghosts

The pink thing above flashes on the screen when a ghost should be spawning. But, if you blink, you will not see it. I captured this figure by clipping a frame from a video. This thing looks kind of like a ghost, but pink? Really? And, ghosts may float away, but, as far as I know, they don’t flash briefly and disappear.

The Code

The code is show below. It is a simplification of the code in  Monster Showtime in MakeCode for Minecraft.

code all

The Code Assumes a Flat World

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Get the Code

Missing Monster  code is shared on Code Connection at this URL

https://makecode.com/_YLiKDc5E0HrY

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

25 MakeCode Programs Tested for Compatibility with both Minecraft for Windows 10 and Minecraft Education Edition

The Shared MakeCode for Minecraft on Windows 10 and Minecraft Education Edition  page now shows,  for each of our published programs, the compatibility test results as well as the link to the post describing the code and the link to the code as shown in the graphic below.

post

The first listed program has not been tested in Minecraft for Windows 10 because for the past 3 weeks I and others have not been able to connect or maintain a connection  to the Minecraft for Windows 10 server. We are sad, but waiting hopefully.

Only one program failed a test. Monster Showtime in MakeCode for Minecraft  did not operate correctly in Minecraft Education Edition. Only three of the monsters would spawn. The MakeCode for Minecraft Education Edition monster dialog is shown below.

monster dialog

Of the 24 monsters shown in the dialog only Shulker, Vindicator and Evoker would spawn.

You can verify this test by running Monster Showtime in MakeCode for Minecraft; however, this afternoon I will release a simpler test: Missing Monsters in MakeCode for Minecraft Education Edition.

Grandma’s Game of Life in MakeCode for Minecraft

Grandma’s  Game of Life (Grandma’s GOL) is the implementation of Conway’s Game of Life described in this post.  Conway’s GOL is named  after the British mathematician John Horton Conway who created it in 1970 after being inspired by John von Neumann‘s work on hardware organisms. John von Neumann defined “life” as being or organism that could  reproduce itself and simulate a Turning Machine, Conway’s GOL grew to be the best known and most widely implemented cellular automata. Cellular automata are studied in computer science, physics, biology, biochemistry, economics, mathematics, philosophy and generative sciences.

In the video above, the pattern of cells is called a Glider, which is a famous pattern because it actually appears to moves on the board as the rules are applied from generation to generation. The Glider is the smallest of the spaceship patterns. When game rules are applied to the pattern of cells on the left below  in the figure below, the pattern on the right is generated. Read on to understand how this works.

Conway’s Game of Life is a simple idea from which marvelously complex “living” patterns can emerge. Although, invented with serious intent, Conway’s GOL, has become a pastime, if not an addiction, for many. After Conway’s GOL was described in the October 1970 issue of Scientific American in Martin Gardner’s Mathematical Games column, computer scientists and mathematicians the world over became devotees, I among them. Many of us rushed to program Conway’s GOL in whatever programming language we had access to at the time. I remember printing reams of paper, in the absence of a CRT,  from a FORTRAN program so that I could watch “living” patterns, which might emerge, move across the board, stabilize, oscillate and die. There was a rush to discover patterns with certain characteristics. Conway’s Game of Life has lost none of its popularityr. Today, there are databases of GOL patterns catalogued by their various characteristics.

In this game there are no players. The game is played on a board such as those shown above. Each square on the board is called a cell. Each cell can be alive (blue) or dead (white) , which is also referred to as on and off. The game has rules, which are applied to a board configuration to create the next generation. The rules are applied again and again to create successive patterns.

Each cell has eight neighbors, which includes the four diagonal cells as well as the cells above, below, left and right. A cell at the edge of the board has neighbors on the opposite side of the board. This makes the board wrap on both up/down and right/left sides so that all cells have eight neighbors. The wrap-around board is implemented here; however, another common, simpler implementation is to have cells at the edges have only 5 neighbors because thy have no wrap-around neighbors.

The rules for Conway’s Game of Life are simple. For a live cell to remain alive in the next generation it must have exactly 2 or 3 neighbors. In other words, if a live cell as fewer than 2 neighbors (underpopulation) or more than 3 neighbors (overpopulation) it is dead in the next generation. If a dead cell has exactly 3 live neighbors, it will be alive in the next generation. These rules are so simple that they can easily, albeit tediously, be applied by hand. You do it — I’ll use my computer.

To start a game, choose a board size and  enter a pattern of living cells. Apply the rules to generation after generation. With some patterns, all the cells will die within a few generation. With other patterns, the living cell configurations may oscillate, repeat after several generations or repeat while moving on the board.  There are even patterns that never change no matter how many time the rules are applied.

Wikipedia and LifeWiki provides good overviews and history of Conway’s Game of Life as well as examples of patterns from the more than 1,000 that have been studied. LifeWiki presents more than 1,000 patterns with running simulations of many.

Grandma’s Game of Life has 24 patterns built-in that you can explore by just typing the pattern name as a command. For example, pulsar is another well-known GOL pattern. It does not move, but repeats after 3 generations as you can see in the video below.

How to Operate Grandma’s Game of Life

Program Commands

The two tables below list the commands available in Grandma’s Game of Life.

on-chat command Action
pattern title There is a command for each pattern in the pattern list. (see next table)
pattern num1 Runs pattern number num1 from the pattern list
patterns num1 Runs all patterns starting with pattern 0 from the pattern list. num1 is the number of cycles (generations) each pattern can run If the stop command will cause the command to terminate with the current pattern.
new Creates a new board with each side containing the number of blocks specified by the current size. This command can be used to create a new board for entering player specified on/off cells.
size num1 Sets the number of blocks per side of the board to num1, default 10.
random num1 Creates a new board then initializes num1 percent of the total cells to be alive (on). For example, if num1=50, half of the cells would be alive.
edit Enables the player to edit the current board by changed the on/off value of cells. When the player has finished editing the board, the enter command is required.
enter Completes editing by entering the on/off values for the edited board.
go After a pattern has been entered, the go command starts the generation cycles. The program will cycle through generations until the limit is reached, there is no change in the board or the player stops or pauses execution.
1 Performs one cycle and pauses. Repeated 1 commands enable the player to study how a new generation is derived from the preceding by applying the GOL rules.
pause Pauses after the current generation is completed. The game can be restarted with either a go or 1 command. During a pause, the board can be edited.
stop Stops after the current generation is completed. After a stop, the game cannot be restarted.
limit num1 Changes the cycle (generation) limit to num1. If no parameter, defaults to 3000.
wait num1 Changes the number of seconds for the pause between each generation. If no parameter, defaults to 0.
out Says the position coordinates of each on cell in the current board. The command is invalid if the game is running.
on-chat command on-chat command on-chat command on-chat command
blinker toad block glider
clock pond pre-pulsar_2 beacon
pre-pulsar L spaceship pent mazing
O spaceship figure 8 H spaceship blocker
M spaceship bi-clock tumbler kok
queen bee pinwheel weekender pulsar

The video below shows 20 generations each of the above patterns.

The Code that Grew and Grew

Halloween is an appropriate time for feature creep. This code grew to be the largest MakeCode program that I have written. I’m not proud of it. I succumbed to my usual problem — the creepiest of feature creep. Feature creep is a common problem in software development. After the program is defined, someone thinks of “just one more feature.” Then, just one more feature. And so on. Some programs are cursed with such bad cases of feature creep that they are never released Fortunately, I am releasing Grandma’s Game of Life today no matter how big it is. But, I am planning a smaller version for release soon. Trouble is — just which features am I willing to give up?

An implementation of  Conway’s Game of Life needs to do the following:

  • Generate the board.
  • Enter  patterns on the board.
  • Apply the rules from one generation to the next, making needed changes to the cells.

Two challenges proved particularly difficult;

  1. Including a pattern library in one of the standard formats. I chose Run-Length Encoding, (RLE).
  2. Fast application of the GOL rules to generate a new generation.

Pattern Library

I wanted to use Run Length Encoding (RLE) standard format for the pattern library. This would enable me to copy/past patterns from LifeWiki. In the code below, the entries in the list RLE_patterns list  where obtained that way. The patterns are arranged in order of complexity with simpler patterns first.

RLE patterns

Each RLE pattern specifies the layout of on/off cells that constitute the pattern .The task was to translate the RLE pattern into Minecraft positions of on/off cells on the GOL board. This is a job for a parser. I’ve been writing parsers for more years than I like to admit. This one was challenging because MakeCode has very limited string (text) functionality.

Before I could parse anything, I had to write a string library with functions such as trim, in-string, string-between and split. I will describe this string library in another post; however, the code is available with this program. I considered using JavaScript, which  provides a bit more string functionality, but not enough to justify the ugliness it would introduce.

Fast Rule Application

GOL simulators (like this one) can be very slow unless some cleverness is employed. Most of the slowness is caused by the fact that to apply the rules, the number of living neighbors for each cell must be counted. In this implementation, updating the blocks for a new generation was also slow. The algorithms I explored included the following:

  • Using the Minecraft blocks to store the on/off information. If A block is blue, it is on. By cycling through the grid of blocks, I could test each neighbor to count it thus determining how many neighbors each cell had and whether it would be alive or dead in the next generation. Very, very slow. Testing Minecraft blocks in MakeCode is much too slow to use this method.
  • Next I tried using a 1-D array to simulate a 2-D array of Boolean values that represented the alive/dead state of each cell. But, again ALL cells would need to be cycled through to calculate neighbors, then assign values for the next generation, then place the blocks in the board to match the result. In this method, the program spent too much time testing array values that could never be alive in the next generation because they had no living neighbors. That made me think of the final method.
  • After a few refinements, I found the following method. I used two lists, not arrays; that is, the lists only contained cells on the board that could change in the next generation, not the entire 2-D array. The lists are:
    • GOL_on_cells_list — This list contains all the cells that are alive in the current generation. The companion list — GOL_on_cells_list_prior — is the list of all cells that were on in the prior generation.
    • GOL_changed_cells_list — This list contains the Minecraft world positions (relative to origin) for the cells that will change in the next generation.  The MakeCode  X and Z coordinates are the row and column, respectively, in the GOL board grid. The Y coordinate is 0 if a cell is the be turned off and 1 if it is to be turned on.

Code Overview

I will explain how this works in a later post, but for now it is left as a exercise for the student. The code is shown below in three sections. The first is pattern processing. The second is the main parts of the program. The third is the string library.

GOL code 1

GOL code 2

GOL string library

Flat World

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Smaller Code Coming Soon

Depending on the power of your computer, this code may be difficult to work with; however, it should run, albeit slowly.

Get the Code

Grandma’s Game of Life  code is shared on Code Connection at this URL

https://makecode.com/_LU1iDpdcEFvY

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

The Art of Random Cityscapes in MakeCode for Minecraft

 

This MakeCode for Minecraft program is called “Cityscape.” Zillions of cityscapes can be generated by customizing input parameters. A few cityscapes generated by t he program are shown in this blog post.
tall city

ice city.png

dark down

Idea

I had the idea for generating random cityscapes as I was developing The Art of Random Walks in MakeCode for Minecraft.  Essentially, the idea was that instead of placing blocks at each random step, the program would place a building built with a random block with random height, width and depth (depth here means how far back). The program would then move to another  location a random distance from the prior building. It would then place another building, and so on, until the specified number of buildings were placed. The random selections would all be within player specified limits. Optionally, the program would place “ground” of a player-specified block within a rectangle around the city.

city 200 bldg

Customizing Cityscape

Cityscape’s has many options. Such flexibility is marvelous for an experienced player, but can be confusing for a first timer. This is a challenge frequently faced  by software designers. In this case, the solution was to have a default for every option so that a player need only type “city” in the command window to generate a beautiful, probably unique, cityscape. Typing “city” again, generates another city that likely will appear very different from the first, and so on, until the player decides that something a little different would be nice — time to start exploring the options.

Cityscape’s options are specified as follows:

  1. As the values of the parameters in the functions “default_params” and “custom_params.”
  2. As the three arguments to the on-chat “city” command, which starts the program.
  3. As the blocks specified in the block arrays.

Default and Custom Parameter Functions.

Both the default and custom parameter values, which are set in their respective functions,  can be changed.  I suggest having the values in “default_params” be the ones you use most often and the params in “custom_params” be experimental. I also save sets of favorite values by copying them to a scratch area of the program so that I can drag them in and out of the parameter functions.

Cityscape parameters include the following (in the order shown in the param functions):

  • the number of buildings
  • the block array to be used for buildings
  • whether or not to do a compass
  • the ground block to be used in the rectangle surrounding the city
  • the block to be used under the ground block. This is useful if a glass block is used for the ground.
  • foundation block to be used under each building. This is replaced if a new ground is laid.
  • whether a random block is to be used for the ground
  • whether all buildings are to be cubes
  • lower and upper limits for building height
  • lower and upper limits for building width
  • lower and upper limits for building depth (how far back)
  • the maximum distance from one building to another

parms code.png

In the left cityscape below,  all dimensions are set to 1 with a move range of 3. This cityscape illustrates the random walk from the origin, which is indicated by the clear glass block above the lower right TNT block. The right cityscape below has a move range of 0, which means all buildings, which have varying dimensions,  are  built in the same place.

A Function to Verify Parameters

The function “verify_params” checks the parameters for correct values. If one is wrong, for example, a negative value for width, the program will say a message and stop. The last verification in the routine is to check whether the fill command volume limit of 32,768 will be exceeded for any building. This is done by calculating the largest possible volume,  which is calculated as the product of the three variables height limit, width limit  and depth limit.

verify code

color sym city

Blocks from Which Buildings Are Constructed

Building blocks are  specified by the variable “which_array” in the parameter routines above. The values can be 1, 2, 3 or 4. This value can be overridden by an  on-chat “city” command argument. The values 1, 2, 3 or 4 specify, in order, one of the following arrays:

  1. jewel_array,
  2. black_array,
  3. other_array and
  4. an array constructed from all three of the above arrays.

Each of these arrays can have any number of blocks and can contain any type of Minecraft block from which a building can be constructed. The names of the arrays cannot be changed unless they are also changed in the code that reference the arrays. The variable “this_array” is set to the array that is selected by the “which_array” parameter or argument. For building construction, blocks are randomly selected from “this_array.”

array code

on-chat “city” Command Arguments

The on-chat “city” command (see below)  has three arguments.

city args

If num1 is 0, default_params are used. If 1, custom_params are used. A valid num2 overrides the number of buildings provided in default or custom parameters. A valid num3 overrides the array of blocks to be used.

blue city

Using Cityscape

The on-chat “city” command is the main command. It generates a cityscape according to the previously set parameters and arrays, which may have been overridden by the command’s arguments (see above).

The on-chat “view” command moves through views of the city from 20 different vantage points, including 4 sides, 4 corners, over the origin and over center of rectangle. For each of these  2 different heights are used. The on-chat command “s” will stop movement at the current view. The argument num1 to view is the number of seconds to pause at each view, default 3.

The Code

The left third of Cityscape’s code has to do with the inputs that tailor the program to generate an near-endless variety of cityscapes. The middle third, which is launched with on-chat “city” command,  is the guts of the program and actually constructs the city building by building within the bounds of the input parameters. The right third of the program is primarily that launched by the “view” command, which assists the player in looking at the city from various vantage points.

All Code

on-chat Command “city” Starts the Program

The on-chat “city” command defines the main flow of the program in the following steps:

  1. Processes arguments to
    • choose between default and custom parameters,
    • optionally, override number of buildings and
    • optionally, override the array of blocks used in building construction.
  2. Initializes variables, sets origin and, optionally, launches an on-chat to draw a compass.
  3. Uses a repeat block for the following:
    • calls “do_one_building” function to construct a building and
    • calls “get_new_rposition” to pick random position for next building.
  4. Adjusts height of origin marker to be taller than highest building and moves player to above origin.
  5. Calls “build_ground” to create rectangle at ground level.

city code

Function to Do One Building

The “do_one_building” function does the following:

  1. picks random height, width and depth.
  2. calls function to pick block to be used.
  3. uses fill block to create hollow building.
  4. uses fill block to lay foundation of building.
  5. calls function to update minimum and maximum X and Z.

do one bldg code

Function to Get New Random Position

The position of the next building is selected as a random position between the current building’s position plus {minus the move range, 0, minus the move range} and the current building’s position plus {move range, 0, move range}. Simply put, the new building can be any direction up to a distance of  move range from the current position.

get new position code

Function to Pick the Block for Building Construction

The “pick_block” function chooses a random block from the “this_array” array. It also call the function “give_flint_if_needed” to give the player a flint-and-stone item if the block is TNT.

pick block code

Function to Give Player Flint and Steel Item If Block Is TNT

This function gives the player a flint-and-stone item if “this_block” is TNT and the item had not been previously given in this run of “city.” This function is called when a new building block is chosen and also when the foundation block is selected.

give flint

blown up

Functions to Initialize and Finalize Origin Marker and Player Position

Origin marker is a tower of clear glass blocks that is a bit higher than the tallest building. It marks the place of the first building. I use it to view the direction that the random sequence of buildings developed. Initially, the marker is at the maximum height that a building can be. Finally, it is adjusted to be just above the actual highest building. The on-chat “no” command erases the origin marker. This is useful for those situations where the origin marker detracts from the view of the city.

origin marker

Functions to Keep Track of Minimum and Maximum X and Z coordinates

Maximum and minimum X and Z coordinates are updated with each building so that, in the end, the ground can be placed under the city with the same distances from buildings on all sides. Most of the view points are on the perimeter of the rectangle surrounding the city.

min max XZ code

Function to Do Ground Rectangle

This function uses the minimum and maximum X and Z coordinates to define a rectangle. Within that rectangle, grass is replaced by a block, which is either a random block or a block supplied as a parameter in either default or custom parameters functions. An underground block is also placed below the ground. This is useful when the ground is transparent.

do ground code

on-chat “view” Command Helps the Player in Appreciating Her Handiwork

After a cityscape is generated, the player may want to look at it from various vantage points because the view can be very different from various directions. Moving around a city can be tedious; therefore, automation to the rescue. The on-chat command “view” moves the player through 20 positions, pausing to allow time to view each one. The number of seconds to pause is specified to num1, which defaults to 3 seconds. If a particular view is spectacular, an “s” on-chat command stops movement to the next view.

view code

Function to Calculate the Positions for Viewing a Cityscape

The ground plane positions from with views are provided are shown by the drawing below. Minimum and maximum X and Z are used to do the calculation in the function that initializes the view positions array.

drawing directions

calc view positions

Note that, as far as I know,  arrays of positions are an documented feature of MakeCode for Minecraft.

Other Code Used by Cityscape

Two other pieces of code are used by Cityscape:

  • compass and
  • a function to convert a number on-chat argument  to a Boolean array.

This version of compass is particularly fast and flexible. It will be described in my next block post.

compass

This handy little function was motivated by the fact I needed to use several Booleans arguments to an on-chat command. It will be documented in a soon-to-come blog post.

bits

flat world

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

up black city.png

Get the Code

Cityscape code is shared on Code Connection at this URL

https://makecode.com/_MXigqm5HTdev

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

gray city.png

red on black down

black on cyan city

down-pink.png

down green city.png

down city.png

This slideshow requires JavaScript.

Monster Showtime in MakeCode for Minecraft

 

The video above runs Monster Showtime from beginning to end. In the Code section below is a video that describes the MakeCode for Minecraft code that creates the show.

makecode-for-minecraft-monster-showtime-blaze.png

Watching the Show

The on-chat-command “run” starts the show. The players then needs to do three things:

  1. Move the player to look to the east. If you don’t know which direction east is, you can use a sunflower because it always faces east. Or, when the show starts, just scan around until you see the stage.
  2. If needed, press the F1 key so that the screen log does not show — All those teleport messages can be annoying.
  3. Sit back and watch the show — no hands needed.

makecode-for-minecraft-monster-showtime-zombie-villager.png

Monster Showtime, after a flashy introduction, cycles though all monster types. For each monster type, it spawns  three on the stage with the monster’s name displayed above. The program delays a bit before going on to the next monster type so that the player can contemplate the monster’s characteristics. About half way through the viewing of a monster, a skylight is opened in the stage roof so that the player can see some combustible monster’s catch fire.

makecode-for-minecraft-monster-showtime-skeleton-on-fire.png

Challenges Solved

I did a prototype of this program a few months ago, but the following challenges kept me from completing it until now:

  1. Arrays, especially text arrays, sometimes go crazy. For example, in an array of numbers, sometimes, for no apparent reason, MakeCode forgets that the variable is an array and thinks that it is just a single number. It gives a message that “a number[] cannot be assigned to a number” — like I didn’t know. Eventually, I found a workaround.
  2. The MakeCode print block is so slow that most people would give up waiting for monster names to print. Eventually, I found a way to dramatically speed up printing.

The Code

All the Monster Showtime code is shown in the screenshot  below. On the left are the initialization routines. Right of initialization is   “run,” which along with “do_one_monster” below it, defines the flow of the program. Moving right, are the routines  that flash the initial display of “MONSTER SHOWTIME” and build the stage. Next right  is the code that displays “THE END” and spawns a crowd of monsters to take a final bow. On the far right, are the fast print routines that make each monster’s type appear almost instantly above the stage.

MakeCode for Minecraft All Code

The On Start Block Initializes Variables

It is good practice to initialize variables that never change in the on-start block, as this code does below.

on start codeThe two arrays shown below must be the same length and the names in the same order as the corresponding monsters.

arrays code

Run Starts the Program and Defines the Main Loop

The on-chat command “run” does the following:

  1. Calls a function to teleport the player to a new location and set the variable “origin,” which is used as the reference position in all building and spawning.
  2. Checks to be sure that the monster array and monster name arrays are the same length; and, if not, displays an error message and exits. If OK, continues with next  step.
  3. Calls a function to flash “MONSTER” and “SHOWTIME” on the stage in oscillating colors. Another function displays a suggestion message to the player.
  4. Sets “print_block,” which is used by “print_fast,” to orange concrete, the color used to print the monster names.
  5. Loops through all monsters doing the following for each:
    • Teleport player to new location and set “origin.”
    • Set the global variable “idx_global” to the loop index. “idx_global” specifies the monster being processed.
    • Calls the function “do_one_monster.”
  6. After all monsters are processed, teleports player to new location and runs “end” function.

do one

flash monster code

error code

Do One Monster

For each monster, the “do_one_monster” function does the following:

  1. Places a sunflower near the player to verify that the stage is being built to the east of the player.
  2. Calls the function “make_stage” to do just that.
  3. Sets “print_text” to the name of this monster, which is selected from the “monster_names” array at index “idx_global.”
  4. Calculates “Z_coordinate” such that the monster name will be centered when displayed.
  5. Sets the print position for the name using “Z_coordinate” calculated above and  X and Y determined by trial-and-error.
  6. Calls “print_fast” to print the monsters name.
  7. Teleports the player to “origin” to move close to the stage for a good view of the monsters as they are spawned.
  8. Spawns three of this monster in a row on the stage. Of course, the monsters will not stay where they are spawned very long, but some longer than others.
  9. Pauses, then teleports back one block at a time using a loop. This makes the player slowly back up for a full view of the stage.
  10. Pauses, then builds a skylight in the stage roof so that sunlight shines on most of  the stage. Some monsters will catch on fire in sunlight.
  11. Pauses for more monster viewing, then exits.

do one

Build the Stage — Fast

The stage is built using several on-chat-commands, instead of functions, so that several  elements of the stage will be built at the same time. This makes stage construction much faster. Speed is desirable because a new stage is generated for each monster.

stage code

The function “make_stage_roof_window” is called about half way through displaying each monster to allow sunlight, which makes some monsters catch fire. The on-chat-command “stage_test” is a tool for building a single stage for debugging purposes.

The “stage_test” function is included to enable testing “make_stage” independent of the program.

Print Fast

“print_fast” is speedy because all characters of the text string are printed at the same time, not one by one sequentially as they are using the normal MakeCode print block. To accomplish this, each character position, say 0 to 20, in a string must have its own print_n on-chat command. This code takes a lot of room. The good news is that all the routines are exactly the same except for the name. For example, the code shown below will print up to 21 characters using “print_0,” “print_1,” and so on to “print_20.”

The function “print_fast” loops through each character of “print_text” with the loop index “print_index” indicating the character to be printed. It calls a different print_n function for each character. It also supplies an argument, which is the number of the character to be printed.

“print_fast” uses some cleverness to be sure that each “print_n” routine does not share variables that might be changed by another “print_n” running at the same time.

    1. “print_text” is a global variable that is never changed by a “print_n” routine.
    2. The argument “arg_print_index” is unique to each “print_n” because arguments are local to an on-chat. This argument selects the character from “print_text” that is to be printed by this print_n. For example, “print_2” prints the third character of “print_text,” which is “i”. “arg_print_index” is also used to calculate the position at which the character will be printed relative to “print_position,” a global variable that is not changed within the “print_n” routines.

print code

The End

At the end of the show, the monsters all come out to take a final bow.

makecode-for-minecraft-monster-showtime-the-end.png

The end function does the following:

  1. Prints “THE END” in the sky.
  2. Spawns all monsters in front of the player.

An on-chat command “end_test” enables testing the function without running the entire program.

End Code

Code Requires a Flat World

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

MakeCode for Minecraft Monster Showtime Vex

Get the Code

Monster Showtime code is shared on Code Connection at this URL

https://makecode.com/_h7bEY4TVifUK

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

Coordinates Game in MakeCode for Minecraft

 

begin game

Early in the game, most of the spaces on the grid are white. The green blocks indicate that the player got the coordinates of that position correct while the red blocks were incorrect. The center cyan block is the origin (center) of the grid, which is coordinate { 0  0 }.

finish green

At the end of a game, all blocks are green except those that were missed by the player. The percentage score is shown above the grid.

Coordinates Game is a companion to Coordinates Tutor, which is described in Coordinates Tutor in MakeCode for Minecraft. Coordinates Game enables a player to demonstrate their knowledge of coordinates.

How to Play

To play Coordinates Game, type “run” in the command window. The program will display one of the windows below. It will oscillate between the two windows reminding the player that X and Z correspond to east and south, respectively.

The game will then display { X Z } coordinates, such as {  1  -1 } on the left below. The player’s challenge is to break the white block in the grid corresponding to the displayed coordinates. If the player gets it right, a green block will replace the broken white block at the coordinate {  1  -1 }. If the player gets it wrong, a red block will replace the broken one. The program then displays the next set of coordinates for the player until all positions  in the grid have been used. At this time, the score is displayed as a percentage of correct answers (see above).

The game loop, simply put is:

  • the program displays a position as a set of coordinates,
  • the player breaks the white glass block at that position in the grid,
  • the program replaces the broken white block with either green or red blocks corresponding to right or wrong, respectively, and
  • the program displays the next position coordinates, and so on until all positions have been used.

Code Design

The big-picture design of the program is depicted below. The program first initializes, which includes building the grid and compass as well as creating a list of all positions on the grid. At first, all these positions are available to be displayed to a player. As each position is used, it is removed from the list. When there are no positions remaining, the game is over.

drawing

In operation,  after the program posts position coordinates,  nothing happens until the player breaks a block, which generates an on-broken event. The position of the broken block is compared against the displayed position using a sneaky method that I will describe later. If correct, the broken block and the block under it are replaced by green ones. If wrong, a red block is placed at the correct position with a red block under it, which indicates that the player got that one wrong. The broken block is repaired by replacing the white glass.

The Code

The first version of Coordinates Game code was completed in July. I didn’t like it because on rare occasions weird things occurred that I didn’t understand. For example,

  1. Sometimes. towards the end of the game the on-white-glass event stopped firing and would not restart. No matter how much glass I broke, no event. I put many say-blocks in the program so that I was absolutely sure there was no explainable reason for this to occur. I still don’t know why this happened and I could not reproduce the problem outside my program.
  2. My original program did repairs asynchronously in a forever loop. On rare occasions, this interfered with game play. I eventually understood how this rare thing happened. To fix it,  I had to give up asynchronous repairs. This turned out wonderfully, as the resulting code is totally reliable and much faster. How great is that?

I changed and changed and changed the design as well as then code to make it more understandable and reliable. I did thought experiments, had nightmares and awakened with new ideas that sometimes worked and sometimes didn’t. I trashed large sections of the code and started over again and again. My 6-weeks on-and-off effort was rewarded. Now the program has no anomalies, at least none that I have found after playing the game many times.

The figure below shows all the code for the Coordinates Game. Starting on the left is the initialization code. Towards the middle is the code that posts coordinates. Moving right is the on-broken events that triggers processing of a player’s answer. To the right is the code that makes repairs.

all code with labels

run on-chat command

This on-chat command is how the player starts the program. It does the following:

  1. Moves the player to a new, pristine location and sets the origin variable.
  2. Calculates the opposing corners of the grid as { -5 -5 } to { +5 +5 } that will be used for building the grid.
  3. Sets the print position where the coordinates and score sill be displayed.
  4. Launches the on-chat command brackets to print the brackets within which coordinates will be displayed. Because it launches an on-chat command instead of using a function, the program continues while the brackets are being drawn.
  5. Launches the on-chat command compass to draw a simple compass while the program continues to run.
  6. Calls the function canvas to build a grid.
  7. Initializes the counts of right and wrong answer to zero.
  8. Calls a function to flash between { X Z } and { E S } to remind the player that X is East and Z is South.
  9. Calls a function to initialize the available-positions-list.
  10. Launches an on-chat command to post the first position coordinates. Because of using the on-chat command instead of a function, after launching the on-chat, the program continues and exits.
  11. At this point nothing happens until the player breaks a block, hopefully, corresponding to the correct answer.

run code

Display  New Position Coordinates

This on-chat command run asynchronously and performs the following functions:

  1. Calls a function to get a random grid position from those that have not been used; that is, is available.
  2. If all position have been used, the on-chat command end is launched to post the score and end the game.
  3. If an unused position was found, the answer position is calculated as origin, which is  the position at the center of the grid, plus the available position, which is a position of the form { X 0 Z }, where X and Z are each a  value between -5 and +5. This is more fully explained in the section following this one. The variables answer_X and answer_Z are the X and Z coordinates or the answer position, respectively.
  4. Prepares the coordinates for printing in the correct location by putting a space in front positive coordinates. The space occupies the space that is occupied by a minus sign in negative coordinates.
  5. Increments nPrints, which is used to select the color of block used to print the next coordinates. The color oscillates so that there is no confusion during the time when one coordinate is being replaced with a new one.
  6. Launches the two on-chat commands that prints the two coordinates at the same time. If this were done with a single print, it would be irritatingly slow.

postNewCoordinate

Available-Positions List

The list of available positions is key for keeping track of which grid positions have not yet been used. There were other more convoluted ways of doing this, but using a position list — an undocumented feature in MakeCode for Minecraft —  is neat. Usually, a coder should not use undocumented features of a language, but at 76 years of age, I can do anything I want. Since in JavaScript, lists of positions is allowed, I trust the makers of MakeCode will not eliminate the feature.

The function has an outer loop corresponding to the X coordinate of a position and an inner loop corresponding to Z. Each position { X 0 Z } is added to the list. { X 0 Z } is a world position; that is, each coordinate is just a number that can be added to origin to get a world position of a block on the grid. Each position added to the list is of the form { X 0 Z }, where X and Z are each a  value between -5 and +5.

init array code

The get available position function does the following:

  1. Checks the length of the available positions list. If the length is 0, meaning there are no positions available, it sets the Boolean variable availableFound to false and exists; otherwise, it continues at the next step.
  2. Picks a random position from the list as the variable availablePosition and removes it from the list.
  3. Sets availableFound to true and exits.

get available code

Process on-broken events

When a player breaks a white or cyan block on the grid, events are triggered that call the breakCommon function, which does the following:

  1. If the variable answerPosition has not yet been set, the function repairs the broken block and exits.
  2. If the block at the answer position is air, the answer is correct; otherwise not. I think this is sneaky or perhaps I mean clever, but I wouldn’t want to brag.
  3. For correct answers, the block at the answer position as well as the one below it are replaced with green blocks.
  4. For incorrect answers, the block at the answer position as well as the one under it, are replaced with red blocks. But before that, the block above the answer position flashes green a few times to indicate where the correct answer is. The wrong white block broken by the user, is left as air until repairs are done in the last step of this function.
  5. Before the on-chat command is launched to post coordinates of a new position on the grid, the function pauses one second to give the player a chance to view the current position coordinates at the same time as the corresponding block turns green or red.
  6. Finally, a function is called to do repairs, which replaces the air block, which was  left by a wrong answer, with a white block.

break code

Repairs

The do_repairs function runs every time a white or cyan block is broken; that is, an answer is given to the displayed position question. Repairs are needed when the player breaks a block that is not the correct answer. The first replace block  in the function fixes that. Also, on rare occasions, the flashing green block above the correct answer is left as a green block, not air as it should have been. The second replace assures that all blocks above the grid are air. The last step is to set the origin block, which might have been changed by mistake or broken as a correct answer to grid position { 0 0 }.

do repairs

Besides wrong answers, players make other mistakes when breaking things; for example, a player might break a grass block next to the grid. Or, a player might just be plain ornery, breaking things to see what happens. The remaining repair routines run in response to events when something is broken by a player that should not have been either by mistake or orneriness. The following should not be broken:

  • green blocks
  • red blocks
  • axis market blocks (white carpet)
  • grass blocks

When these blocks break, the associated on-broken event makes the needed repairs. How cool is that?

fix red and green

axes markers

repair grass

Code Requires a Flat World

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Get the Code

Coordinates Game code is shared on Code Connection at this URL

https://makecode.com/_8vKbsj7diit1

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

Negative Random Number Tester in MakeCode for Minecraft

 

main

I have made several uses of negative random numbers in my MakeCode for Minecraft programs, for example, in Coordinates Tutor in MakeCode for Minecraft I used random numbers between -5 and +5 to generates X and Z coordinates on a grid. With each use,  I wondered whether the number of negative numbers generated were, in fact, equal to the number of positive numbers generated. Thus, this exploration.

By experimenting with this program, I observed two things. First, in cases where I generated fewer random numbers, say 10, the number of positive numbers and negative numbers were often quite different. For example, in one case of only 10 generated numbers, 60% were positive and 30% were negative — not close. However, in the case of a million random numbers, 47% were positive  and 48% were negative — close. But, remember that in both cases, because the numbers are random, different runs of the program may give different results.

Three runs of the program generating 10 random numbers each gave the results shown below. The case on the left was perfect with exactly the same negative as positive numbers. The other two cases were not so close. Generating only 10 numbers is likely to give results like on the right because the number of runs (the sample size  n) is small and, thus,  one can expect large variation in the cases.

Three runs of the program with But, remember that in both cases, because the numbers are random, different runs of the program may give different results. random numbers gave the results shown below. The case on the left yielded exactly the same negative as positive numbers. The other two cases were not exact; but, they were close. Because the sample size n 100,000  is relative large, the variation in the runs is not great. But, beware, a single run might have very different results.

While the two cases on the right (above) total to 100%, the case on the left totals to 101%. This is caused by

  1. division that may not yield a whole number and
  2. by rounding.

This is illustrated below, which is say-block output from the program. These results show more significant digits than the billboard output.. Notice on the right that while before rounding the total is 100% but after rounding the calculated percentages is 101%.

Don’t worry if the reasons for such situations are not intuitive. Entire books are written on the subject. I remember taking and later teaching courses called “Machine Arithmetic” and  “Numerical Analysis,” which tediously explored such situation.  In your programming journey, just know that such situations exists and devise code to give the precision needed.

The Plan

The plan for Negative Random Number Tester, or just “Tester,” is to generate some number n random numbers between negative and positive limits of equal absolute value; for example from -10 to +10 or from -1000 to +1000 or from -1 to +1. The exact value of the limits should not matter; however, I did not test that.

The number of random numbers generated n is an argument to the run on chat command that starts the program. It can have values from 10 to 1,000,000.

The guts of the program is the loop that generates the random numbers counting individually the number of zero, positive and negative numbers.

After all the numbers have been generated and counted, the n and the percentages of each of zero, positive and negative random numbers are printed on a billboard, as shown above.

The Code

The run on-chat command is on the top left. The right half of the screen are those routines that convert counts to percentages and print them on the billboard.

code all

The run on chat command performs the following:

  1. Calls a function to move the player, set the origin and set the print position on the billboard.
  2. Checks the argument n for the correct range of 10 to 1,000,000. If n is out of range it set it to the default value 100,000.
  3. Builds the billboard on which the results will be printed.
  4. Initializes the count variables for zero, positive and negatives values of generated random numbers.
  5. Loops n times generating random numbers and counting the resulting values that are zero, positive or negative.
  6. After exiting the loop, says the three count values and calls a function to convert and print them on the billboard.

run code

Rather than print results in the sky, they are printing on a billboard..
whiteboard code

The routine in charge of printing the percentages first calls the functions that convert each of the percentages to text. It then runs the three chat commands to do the actual printing. It also says two totals, one for the total of the three percentages before rounding and one for after rounding.

print pcts code

There are three individual function for the conversions for no other reasons than to organize the code. The routines doe the following:

  1. calculates count as percentage of total,
  2. rounds the result,
  3. calls the format_pct routine right align the number in a 3-digit string, and
  4. adds the appropriate label and the % sign.

The gray block indicates that rounding is done in JavaScript.

convert pct to text

The format_pct routine takes as input a text variable representation of an integer percentage; for example, “1” or “13” or “100”. Since an integer percentage may have 1, 2 or 3 digits, it makes the forces the string to be length  3 by adding spaces on the left of the digits. When the numbers so modified are printed in a column, as they are on the billboard, they will be right aligned.

pad code

The print routines are individual chat commands, not functions, so that they will print simultaneously, which greatly speeds up the print. Notice that when the program is running the three numbers print at the same time. Each of the print routines have a different Y-axis value, which specifies the up-down position on the billboard..

print pos

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Get the Code

Negative Random Number Tester code is shared on Code Connection at this URL

https://makecode.com/_EfC4c9WCUcuR

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

Faster FISH Print in MakeCode for Minecraft Aquarium

 

FISH

This post describes a change to Aquarium in MakeCode for Minecraft that was suggested by a YouTube viewer..

The challenge in the design of the aquarium was to  print  “FISH” on the front of the aquarium by using the Minecraft Print block. The spaces within each character are to be filled with cyan glass blocks, not air (the Minecraft default). Filling with cyan glass blocks  makes the print appear to be embedded in the wall.

The Code

The helpful viewer suggested that I use the Minecraft Replace block instead of the much slower and more verbose nested loops, which test each position in each character for air and replace it with glass.

LOOPS

A loop within a loop that tests each position in a 5×5 character for air. If an air block  is found, it is replaced with a cyan glass block.

replace block code

A single Replace block replaces any air blocks in a 5×5 character with a cyan glass block.

The screenshot below shows the entire Aquarium code with the new print routines.

code all.png

Aquarium code with new print routines.

One print_n routine is required for each character in the text to be printed to enable the four prints to run at the same time rather than each character waiting for the preceding character to print. This was described in the original post Aquarium in MakeCode for Minecraft. The four print routines for “FISH” are shown below. The only differences in the four routines are:

  1. The character to print and
  2. The position in the wall.

The character to print is selected from the string print_text at the position print_num1, which is an argument to the routine. For example, print_2 is passed the argument print_num1 = 2, which selects the string “S” from the string “FISH”.

The position in the wall at which to print is print_num1 position from the array print_pos. For example, for print_num1 = 2, the position chosen from the array is the position for “S”.

prints.png

The four new print routines.

These routines would be a great deal simpler if arguments to an on chat command could be of type string or of type position instead of just a number. This not being so, dictated that the routines use the number argument print_num1 to select the string to print and the position at which to print.

one print

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Faster, Smaller and Easier to Understand

What’s not to like? The Minecraft Replace block does exactly what the nested loops (above) did. The Replace is much faster, as demonstrated in the video (above) . Further, the Replace is less verbose; that is, is fewer lines of code. The Replace is also much easier to understand.

Why Didn’t I Think of That? Wasn’t It Obvious?

It was obvious to my viewer. But,  I am only a few months into learning MakeCode for Minecraft. I have not used every block in the MakeCode language. One can argue that that was a mistake. I, as usual, was so impatient to start creating programs that I did not take the time  to methodically learn every MakeCode block before I started. I prefer Just-in-Time learning, which I am sorry to say that along with one of my life’s themes “not knowing what I don’t know” results in projects that can be improved. Don’t expect me to change. I like improving my projects and I love learning  whenever it is done. So bring it on — every constructive criticism  is welcome.

Get the Code

Aquarium with the improvements described here is shared on Code Connection at this URL

https://makecode.com/_YALWi6RbUXe2

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

Aquarium in MakeCode for Minecraft

 

main

Aquarium Front. Pool of dolphins on left. Watch them frolic in the  fountain. Salmon in the right pool. Slime seem to be attracted to the salmon.

glass view from door

The main fish tank as seen from the entry.  All kinds of Minecraft fish are included with a bit of kelp and coral, also.

at glass

A look through the glass of the main fish tank.

inside top view

A view from the top of the main fish tank. The green things are kelp.

inside tank

From inside the main fish tank.

from inside

Aquarium interior

slime in pool 2

Slime seems to be attracted to the salmon tank.

The Design

When viewing the final aquarium from above, one can see the concentric squares of the main building, the steps up to the aquarium, the fish tank and the glass square at the center of the tank. The porch on the aquarium features a left pool with a fountain and a right pool.

building from above

I started the design of the aquarium with the drawing below. Note that the concentric squares of the main building, as well as the porch with pools, correspond to structures in the final aquarium. Each square is defined by its lower-left corner and the number of blocks in a side of  square.

plan

Because the lower-left corner position of each structure is used as the reference position, each structure is built to the east and south of the corner; therefore, all coordinates X and Z positive.

Space and Time Challenges

I encountered several challenges in coding this program. I wanted to type run and a few seconds later view my aquarium.

I wanted to print  “FISH” on the building in a manner that the characters are embedded in the outer blocks, not in front of the blocks. Printing “FISH” with one print block is unbearably slow. Further, the code to embed the characters at least doubles the print time. I don’t have the patience for it.

print embedded

I solved the slow-print problem by printing each of the four characters in “FISH” asynchronously. The code is described later in this post. This approach required greatly increasing the number of code blocks  making the program so large that if I ever viewed it in JavaScript, MakeCode would not convert it back to blocks, so I gave up JavaScript.

The program was still too slow for me. Again, I used asynchronous code to nearly halve the time by running the code that builds the porch at the same time as the main building is built. This choice only increased the code by one on-chat block.

The Code

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

All the code for Aquarium is shown in the graphic below. Upper left t is the main on-chat command “run.” Below it is the function that sets the dimensions, such as, the size of each square, that define the structures. Below that is the function that calculates the corners as shown in the drawing above. From left to right, is the code for the outer building, the tank and fish, the porch with pools and, lastly, the fast print. Notice how much code is required for the fast print, which caused the program to be fast but larger than I would like.

ALL CODE LABELED

This program could have required much more code to build such a complex structure. The clever, if I do say so myself, use of 25 fill blocks enabled it to be amazingly compact. The use of the fill-block hollow option was particularly useful.

run on-chat command

in this on-chat command block, which starts the program, notice the use of the two run-chat-command blocks instead of functions. Had functions been used, the function would have to complete before the following block would be executed. By implementing it as shown, the code for porch and print_fish runs asynchronously with the functions that follow. You can think of the run-chat-command blocks as launching independent programs then immediately to run the code that follows.

run code

Beware of Common Variables in Asynchronous Code

When using asynchronous code, the variables used in two code sections that run at the same time, must not share variables except for those that are read only.

Set Dimensions and Define Corner Positions of Structures

define dims

define corners

Build Outer Building and Foundation

outer building code

do outer door

do foundation code

Build Tank and Populate with Fish

do tank code

do tank interior code

AQUA THINGS CODE

Build Porch

pool code
porch entry code

Build Two Porch Pools and Populate with Dolphins and Salmon

TWO POOLS CODE

pool code

Fast Print “FISH”

The key to print speed is to print each character with a separate print on-chat command.  In the case of “FISH,” which has four  characters, the on-chat commands are named print_0, print_1, print_2 and print_3. The only difference in code for the four commands is the name. All the interior code is identical in the four functions. The code following the print block fills in the air spaces left by the character within the area where the character was printed. A for-index block would have been neater; however, the index in such a loop cannot be an element of an array. Of course, I tried it.

print fish one letter

Before you ask, of course, the four functions can be collapsed to one; but, in that case, the commands will not run asynchronously and no time is saved.

print fish several letters

The code below is the main code for the fast print. It launches each of the four print commands after initializing the appropriate element of the three arrays print_pos, print_i and print_j. Each on-chat command using a different element of the three arrays. This was necessary to avoid having the asynchronous routines from changing the same variables. This could have been done by  naming the variables differently for each on-chat command; however, that would have  made the interior codes different.

print fish main

I plan a future post that will further explore fast printing.

Get the Code

Aquarium code is shared on Code Connection at this URL

https://makecode.com/_0kDWR4UsX9KP

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.