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.

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.