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.