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.

Coordinates Tutor in MakeCode for Minecraft

 

main screen

We recently developed a game to demonstrate a player’s knowledge of MakeCode Minecraft coordinates. Soon as The Girl started testing it, we realized that a more tutorial program was needed to assist in learning coordinates. Coordinates Tutor — this program — was born..

How to Play Coordinates Tutor

After the user types run in the command window, the Coordinates Tutor will move to a new, pristine  world location, print a compass in the sky for the player’s reference, place a grid of blocks with a cyan glass block at the origin, which is the variable name we give to the 0 0 point in the grid where the X and Z axes intersect.

After initialization, the program will display the left scene then the right to remind the player that in MakeCode Minecraft coding, a coordinate  X is east, Z is south and Y is up. We do not use Y here because it is always the same.

{  X  Y  Z  }.

The positive X axis is east. If one thinks of herself, the player, as facing east, south will be on her right, north on her left, and west behind. Of course, the values X and Z can be negative as well as positive. A negative X is west and a negative Z is north.

When the right scene — {  E   S  } —  is displayed, Coordinates Tutor is waiting for the user to break any block in the grid. Please, break just one block.

broken block

After a block is broken, question marks will flash during the varying time that it takes for the program to search the grid to find the broken block.

When the program finds the broken block, the coordinates of the block will be printed between the brackets. The  broken block will be replaced with a green one, except for the origin block, which will be restored to a cyan block. The green block shown below is at coordinates { – 2 –  2 }.

coor display

At this point, the user may break another block and the program repeats. Coordinates Tutor is patient allowing a user to break blocks until she has absolutely mastered Minecraft coordinates. Then it is time to play Coordinates Game, which works in reverse: that is, when a coordinate is displayed, the user must break the block at the corresponding position on the grid.

The MakeCode Code

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.

Coordinates Tutor Starts with the Run Command

The on-chat run command initializes the program. The steps are:

  1. Teleport the player to an unused part of the world.
  2. Set the variable origin, which will be used as the reference position for all actions as well as the center of the coordinates grid.
  3. Set a position in the sky to be used as left reference point for the coordinate print area.
  4. Run the on-chat command compass to print E, S, W and N in the distant sky. compass runs while the code after it continues because it is an on-chat, not a function.
  5. Run the on-chat command brackets to print squiggly brackets on the left and right sides of where the coordinates will be printed.
  6. Call the function canvas to draw the grid for the coordinates.
  7. Call the function flashXZES, which will display X Z then E S, inside the brackets. This reminds the  player that the left coordinate is X and the right Is Z, or, equivalently, E and S.

run code

The Action Is in the Broken Events

When the player breaks a white, green or cyan stained glass block, which defines the grid, an on-broken event occurs, which for all three events results in the doBrokenEvent being run.

The doBrokenEvent function does the following:

  1. Sets the Boolean variable air_found to false. This variable will be set to true in the function findAir when the broken block is found.
  2. Set nPrints to 0. This variable will be incremented by 1 in one of the print commands launched by the run-chat flash_q.
  3. Launch chat command flash_q, which flashes questions marks inside the coordinate brackets until air_found is set to true in the findAir function.
  4. Call function findAir, which returns only after the broken block is found at which time it runs the chat command postCoordinates.
  5. The chat command postCoordinates prints the coordinate that was found, which is the coordinate of the broken block. postCoordinates is run as a chat, not a function, so that the on-broken event can complete. Although not required, this is neater than having the possibility of a second on-broken event occurring before the prior one has completed.

on break code

Searching for Air

The findAir function, searches the grid starting at coordinate  { -5  -5 } and searching to { 5  5 } for a position in the grid that is air instead of the glass block that was broken. To do the test, it uses the function testTryPositoin to determine air or not. The variable air_found was set to false before the function findAir was called. If air is found, the function testTryPosition sets air_found to true.

find air code

The function testTryPosition tests the Minecraft position to determine whether or not an air block is at that position. If it is not air, it returns doing nothing as air_found is already false. If air is found, it sets air_found to true and sets the airPositoin as well as air_X and air_Z. Note that airPositon has origin added because the Minecraft position is need to place the green block. However, air_X and air_Z are relative to origin; that is they are the coordinates on the grid, e.g. { ,-5  2 } because these variables will be used in posting the coordinate at which air was found. A green glass block is placed at the position at which air was found.

test try position code

The MakeCode Bug that Wasn’t

While coding testTryPosition, I became convinced that there was a MakeCode bug in testing position variables for equality. After some time trying the understand and prove the bug, which included writing a program to test for the bug in a less complex situation, I realized, as I have so many times before, that the bug was in my understanding of position variables. I will not go into the detail here, but take my word for it — if  you want to test a calculated position variable against another, test that all individual coordinates {X Y Z} pairwise for equality instead.

Print the Broken Block Coordinate

postCoordinate is a simple on-chat command that, if the broken block was found, prints the coordinate between the brackets. If the broken block was not found, which should never happen,  it prints red question marks between the brackets. Most of the code is to adjust the text for whether or not X and Z are negative.

post coordinates code

Simultaneous, Oscillating Print Until … Something Happens

I couldn’t resist explaining how the question marks print at the same time and are reprinted in another color block until the broken block is found. To review this short vid show this working.

The key to the simultaneity is using on-chat commands instead of a function or just doing the print blocks inline.  The changing colors are enabled by the variable nPrnts, which is incremented each time through the print blocks. Because the print blocks are run more or less simultaneously, nPrints must be incremented in only one of the print blocks; otherwise, the colors would get out of sync. The print blocks are in a while loop that tests air_found, which when true,   the prints stop enabling the coordinate of the broken block to be printed in the same spaces.

print q flasher code

Get the Code

Coordinates Tutor  code is shared on Code Connection at this URL

https://makecode.com/_d7PAhvEH98iy

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.

Using Minecraft Blocks as Buttons: Block-by-Block Tutorial Video in MakeCode for Minecraft

 

main

Strategy

The strategy for using Minecraft blocks as buttons to control a program is:

  1. Place the buttons on the screen in a place convenient to the player.
  2. In the block on-broken event, use the code that would have been in a on-chat command to do a sequence of actions,
  3. Replace the broken block with another of the same kind.

MakeCode Code

We modified the existing program described in Handy Timer — Block-by-Block Tutorial Video in MakeCode for Minecraft , to illustrate using Minecraft blocks as buttons. Only the changed code is described here.  This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

The placeButtons Function

The placeButtons function does exactly what its name implies: it places each button that will be used in lieu of an on-chat command. I like to place the buttons in front of the player within easy reach so that the player need not move to break a block (click a button). In this case we placed red concrete to do the same thing as the stop on-chat command, yellow for the reset command and green for the go command.
Place button code

Call the placeButtons Function

Once the function is built, it must be called from every code that initializes a screen. Here it is called from on start and from moveMe. As explained in the next section, it is also called from each block’s on-broken event.

on start and moveme

Code the on-broken Events

Each block being used as a button must have an on-broken event in which the same code as in the corresponding on-chat command should be run. I like to move the code to a function, which is called from both the on-broken event and the on-chat command. The advantage of putting the code in a function is that if it is modified, it need be changed in only one place.

Each on-broken event must also call placeButtons. Of course, an on-broken event really must replace only the broken button, but the advantage of calling the placeButtons event is that if you want to futz with the location of the buttons, it need be changed only in one place — always a good thing.

all buttons code

Get the Code

Handy Timer with Blocks for Buttons code is shared on Code Connection at this URL

https://makecode.com/_iwCgCd5bsFxY

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.

How to Use Shared MakeCode on Microsoft Code Connection for Minecraft

 

Most of our wecodemakecode.com posts share a link to the Makecode programs that were described in the post. To use the code, follow these steps on a Windows 10 PC.

Skip to Step 5 — if you are an experienced Code Connection user. In this case, in another 30 second you will have opened your first shared program.

Skip to Step 4 — if you know how to open and link Code Connection to Minecraft.

Skip to Step 3 — if you have already installed Minecraft and Code Connection.

Step 1. Install Minecraft and Code Connection from the Microsoft Store

Step 2. Start Minecraft and Code Connection

code connection link to minecraft

Step 3. Link Code Connection to Minecraft

Copy the link from Code Connection by

Click the copy button  button, which copies the link to the Windows clipboard.

Start a game in Minecraft and choose a world.

Choose world.png

Most We Code MakeCode programs require a flat world such as Sandbox, which we described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Open or create a world and open a chat window. Paste the link copied from about into the chat window. I use the keyboard combination control-v to do the paste.

Chat window with link

Hit Enter on the keyboard or click chat arrow , which is to the right in the chat window. The message “Connection established to server: …” should be shown on the Minecraft screen. If instead, an error message is shown, a common reason is the Internet connection is not available or too slow.
connection established

After the connection is established, the Code Connection screen will show the  languages in which programs can be written to control Minecraft objects, for example, MakeCode or Scratch.

code connection language choices

Click the MakeCode button. The MakeCode window will then become that shown below on the left. I like to position my Code Connection window to the left and my Minecraft windows on the right.

side by side 2

I sometimes think of the Code Connection window as my MakeCode window because once I open or create a program, it is MakeCode I see, such as shown below after clicking the New Project button.

after new projects button.png

Step 4. Load a MakeCode Program

side by side 2

In the Code Connection screen shown above, there are five ways to access a MakeCode program:

  1. New Project, by clicking  new projects.
  2. By clicking one of buttons to the right of New Project, which, in my case, is one of the projects that I had previously developed, for example, flat fixer button. Of course, your Code Connection will show your prior projects.
  3. By clicking one of the buttons below New Project, which are Microsoft-developed example MakeCode programs, such as, flower rail button.
  4. By clicking the Import button  import button, which is on the upper right just below the Code Connection banner, two additional sources of programs are revealed.
    • Import a previously saved program from your computer.
    • Import a shared program from Code Connection using a URL that was supplied to you by the person sharing the program, for example, one a friend or teacher might have emailed you or one suppled in a We Code MakeCode blog post.

Import choices

Step 5. Import a Shared MakeCode Program

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

Now all that is needed is the URL of the specific project you are wanting to import. The URL must have been provide to you by the person sharing the program. For Example, In We Code MakeCode blog posts, we provide the needed URL in the “Get the Code” section at the end of the post. 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 is 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.

Methods of Using the Code URL that Mostly Don’t Work

We have tested several other methods of downloading the code using the URL. We have pasted it into browser or click the link. Typically, this will open a browser, e.g., Edge, Safari. Interestingly, Safari on iPad opened the code allowing a view of both the Block and JavaScript versions of the code. There was a run button, but it did exactly nothing. Edge on a high-end Surface Studio with very fast download Internet connection,  opened smaller programs but always hung up on larger ones. On those that opened, I have not yet figured out how to get the opened new Code-Connection window linked to Minecraft. Of course, I would prefer clicking a shared URL put the code in my usual Code Connection window, but I have not yet accomplished that.

I recommend you stick to the method described above of pasting the URL into the Code Connection import window.

Handy Timer — Block-by-Block Tutorial Video in MakeCode for Minecraft

 

field 70 seconds

Handy Timer

I needed a timer for the game Snowman Army, which I will describe  in a  future post. Games often need a timer. This post describes Handy Timer, which explores how a timer might work in a game.

Handy Timer Strategy

The guts of Handy Timer is a forever loop that increments a second counter each time a second passes. The second counter is displayed each time it changes; however, in a real game other game-appropriate actions could be done each time the counter is updated. . The default second interval is one second, but it can be changed to other intervals, e.g., 10 seconds, 60 seconds.

The Code

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

The code consists of the forever loop, the on-chat commands that control the timer and my ubiquitous moveMe chat command.

Forever Loop: The Guts of Handy Timer

Below is the forever loop guts of Handy Timer. The Boolean variable bRun is true when the timer is running and false when it is stopped. The pause block pauses the code for delta seconds. The variable delta is the timer interval. Actions are taking after delta seconds have passed but only if bRun is true; that is, the timer is running. The actions shown here are to increment the second timers by delta seconds and display the timer value.

forever code

on-chat commands Control the Timer

The following on-chat commands control the timer:

  • go — starts the timer by setting bRut to true
  • stop — stops the timer by setting bRun to false
  • reset — initializes the timer setting seconds to 0 and delta to 1 second. These same values are set when the program starts in the on-start block.
  • delta — changes the interval to the number specified in the on-chat command argument. The variable delta will be changed only when the second counter is 0; therefore, it can be changed when the program first starts or after a stop then reset commands have been executed.

go stop reset chat code

The reset actions are implemented as a function so they can be called from both the on-start block and the on-chat command reset.

on start and reset code

The on-chat command delta has one argument num1. If num1 is greater than 0, delta is set to num1; otherwise, delta is set to 1, the default. If num1 is not provided, it is 0 and the default is used.

delta code

moveMe chat Command

I love my moveMe command because it saves me all that manual moving from place to place in a Minecraft world just to find a clean place to try my program again and again and again, which is usually needed during development and testing. Notice that only X is changed each time moveMe is run; thus, the player is always moved 100 blocks to the East. The player is also placed on the ground because this is what is expected by the other sections of the program. For Handy Timer, a sunflower is placed 2 blocks east of the player as a direction guide. Sunflowers always face east. Since the timer will be printed to the East of the player, its good to know in which direction to look.
moveme basic

Is Handy Timer Accurate?

I used my phone timer to compare to Handy Timer for an estimate of accuracy. For a short time, e.g.,  5 seconds — not very accurate. For longer time periods, e.g., 30 seconds, then results are essentially the same, or at least as close as my eye balls can discern. The difference is the time it takes to perform the actions Handy Timer does at the end of each interval. With a say-block to display the time, it is more accurate than it is with a print block, which takes much longer to display the seconds.

For games, a timer similar to this would be useful for marking more-or-less regular intervals, but it will not replace a stop watch.

Handy Timer’s Future

In my next post, Using Minecraft Blocks as Buttons: Block-by-Block Tutorial Video in MakeCode for Minecraft  I will use Handy Timer to show how to use Minecraft Blocks as Buttons to control the timer. In the post after that, I will use it to explain When and How NOT to Use Player World Position in MakeCode for Minecraft.

We Will Do More Block-by-Block Tutorial Videos

The Girl argued that some of our videos be tutorials on coding MakeCode block by block so that a beginner can see exactly how and why each MakeCode block is used. This post presents our first such effort. Our prior videos have described a program by looking at the final code and describing section by section what it does and how it works. Admittedly, this may be overwhelming to a MakeCode beginner. We will do a bit of both in the future. Our tutorial coding videos will be listed under the category “Block by Block Tutorial Video.”

Get the Code

Handy Timer code is shared on Code Connection at this URL

https://makecode.com/_3C89FjDJaEbR

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.

More Grandma’s Chickens 2.0: A Makecode for Minecraft Game

 

Grandma’s Chickens is a game in which the player kills as many chickens as possible racing against the lightning strikes that may kill  chickens  and ocelots that may  eat them before the player can score a kill. Also, lightning might strike the fence making a hole through which chickens can escape.

A cowardly (or impatient) player will choose to use the sword, which is provided in the player inventory. In this case, chickens are killed with a single strike and the fence and ground will not be destroyed accidently. A more difficult version is to use the hand rather than the sword. In this case, it takes a few strikes to kill each chicken and it is frustratingly easy to accidently make holes in the fence or ground. During all this action, the score is updated in the sky. The game can be varied using the on-chat arguments for the size of the fence, the number of chickens and the number of ocelots.

Field GrandmasChickens

Strategy for the Design of the Grandma’s Chickens Game

The strategy for building Grandma’s Chickens was to spawn chickens and ocelots at random positions inside a wooden fence with the player at the center. The player can start killing chickens while they are being  spawned. The on-chicken-killed event keeps score and every few kills randomly spawns a lightning bolt at a random position inside and including the fence. If lightning strikes the fence, a hole is burned in the fence through which chickens can escape. When a chicken is killed, the score is updated by the on-chicken-killed event and printed in the sky.

The Code for Grandma’s Chickens

The key code is the on-chat command “run” and the on-chicken-killed event.

The steps for then on-chat command “run” and described in the following paragraphs.

Step 1: Set the global variable origin, which will be where the west-north corner of the fence is positioned.

Step 2: Store the arguments in global variables– nBLocksPerFenceSide, nChickens and nOcelots. Check the variables for allowed values, switching to defaults if needed. Say these values before and after the validity checks to be sure there is not a problem, like the one described in a MakeCode for Minecraft on Chat Command Arguments Problem (Bug?). Notice that if no arguments are given — “run” — the values are all zero and the defaults are used.

GrandmasChickes Code 1
Grandma’s Chickens Part 1 of 4

Step 3: Calculate fence parameters including the west-north corner (same as origin), the opposing east-south corner  and the center. If the argument for the number of blocks per side is even, increase it d by 1 so that it is odd. This enables an exact center block for the fence; that is, the center block has the exact same number of blocks on the left, right, in front and behind. Yes, I realize that this is being a bit too meticulous. It’s a programmer thing.

GrandmasChickes Code 2

Grandma’s Chickens Run —  Part 2 of 4

Step 4: Move the player to just outside the fence so she will not be in the way. Yes, I know that this is probably not necessary.

Step 5: Build the fence according to the previously calculated parameters.

Step 6: Call the function makeEastPointerFromOrigin, which helps the programmer visualize the fence construction.

Step 7: Give the player a sword to provide the option of striking chickens with a more powerful weapon than just the hand. Using a sword makes the game easier to play, but personally, I prefer the more difficult version using the hand.

Step 8: Initialize the score to zero and print it in the sky.

GrandmasChickes Code 3

Grandma’s Chickens Run —  Part 3 of 4

Step 9: Set the boundaries for spawning chickens and ocelots one block inside the fence. Spawn the number of chickens and ocelots specified by the global variables nChickens and nOcelots, which were set to the values passed in the on-chat arguments or to the default values if on-chat had no arguments or were invalid values.

GrandmasChickes Code 4

Grandma’s Chickens Run —  Part 4 of 4

Grandmas chickens on start On start initializes the values for the global variables. Here it was automatically generated by MakeCode as JavaScript statements. 

An essential part of the Grandma’s Chickens game is the on-animal-chicken-killed event, which  is run when a chicken is killed by the player. Each time the event is triggered, 1 is added to the score and the score is printed in the sky. A random number is picked from the sequence 0, 1, 2. If the random number is 1, a lightning -bolt projectile is spawned at or inside the fence. The number 1 is arbitrary and could have been any of the three number 0, 1 or 2. It simply enables a 1 in 3 chance of the lightning striking.  If the lightning hits the fence, it burns a hole in the fence through which chickens and ocelots can escape. The lightning also sets fire to the grass, which can be annoying when one is in a panic to kill chickens.

Grandmas Chickens kill
grandmas chickens giveswords

The function giveMeSwords first clears the player’s inventory and then gives the player a sword that can be used to kill chickens more efficiently than just the hand. A function was chosen to implement this code rather than coding it inline, so that other items could be easily added to the player inventory to experiment with the game without cluttering the main code. For example, I have considered providing fence blocks so that the player can repair the fence.

Don’t Worry About the Chickens

I know you might be concerned about all those chickens being killed. Don’t be. They were born to be eaten by the hungry folks in the Minecraft world. On the humble farm where I grew up, chickens were feast food. I can still remember the anticipation of a festive meal when my mother was in the yard plucking the chicken feathers from a fine bird that my father had just killed.

Of course, We Are Working on Grandma’s Chickens 3.0

We have lots of ideas for making Grandma’s Chickens even more interesting and fun. We hope you have some, too. We would very much like for you to build on our code.

Get the Code

Grandma’s Chickens 2.0  code is shared on Code Connection at this URL

 https://makecode.com/_RFUKHraM7Tbp

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.

Flat Fixer in MakeCode for Minecraft

 

Flat Fixer is a program that repairs an area of a world on which structures have been built and perhaps the grass and dirt have been destroyed.

The Flat Fixer code is explained more thoroughly in the video. The first code segment in Flat Fixer, checks the on-chat argument values to verify they are in the correct range. If not, they are set to default values. The variable origin is set to be the initial player world position, but just above were the grass would normally be in a flat world.

Flat Fixer Code 1

Flat Fixer 1 of 3

Air is repaired one Y value at a time in a for loop, one pass through the loop for each Y value. A loop is used instead of varying Y in a fill, because repairs may be needed to high in the sky, say 100 blocks, causing fill block limits to be exceeded.

Flat Fixer Code 2

Flat Fixer 2 of 3

Two layers of dirt are restored above the bedrock at Y = 1 and Y = 2. The grass is restored at Y = 3.

Flat Fixer Code 3

Flat Fixer 3 of 3

Flat Fixer is a companion program to Bang. Using the two programs, Bang can make a mess and Flat Fixer can clean it up again and again and …

Get the Code

The code is shared at  https://makecode.com/_LC4dzUPe4bYg where it can be downloaded and run in MakeCode for Minecraft.

Flat Fixer  code is shared on Code Connection at this URL

 https://makecode.com/_LC4dzUPe4bYg

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.

Bang! — Simulating an On-TNT-Exploded Event in MakeCode for Minecraft

 

Bang Code

The Bang program  places TNT in the ground of a flat world. It then places a  flint and steel item in the player inventory. The player is teleported to within a few blocks above the TNT so that the TNT can be ignited. 

I wanted the player to be way high above the TNT area so that the full effect of the explosion could be seen. After igniting the TNT, it is hard to manually fly the player rapidly enough and high enough to catch the full view. What I needed was a TNT-Exploded event that, when triggered, would teleport the player high enough to get the desired view. But after reading, searching and experimenting, I didn’t find a way to do this with MakeCode’s built-in events (e.g., on block destroyed).

My solution was to simulate a TNT-Exploded event. The key idea was that after the event is enabled, it repeatedly tests the block under origin. The block is originally grass,  then replaced by TNT, and  when the TNT explodes, it is destroyed and replaced by air. When the event is enabled and the test for air succeeds, the TNT has exploded and the player is teleported high in the sky.

Before running bang, be sure the player is flying; otherwise, she will fall all the way to  bedrock after the teleport. I would like to find a way to programmatically put the player in flying mode, but haven’t found it as of this writing.

The implementation of the TNT-Exploded event uses the global variable TNT_eventEnabled, which is false until the TNT is in place and the player is in position to ignite it. Once the event is enabled, the forever loop, which runs repeatedly, proceeds to test for air under origin. When air is found, the player is teleported sky high and the event is disabled so that it will run only once for each run of bang.

Event

Bang is a companion program to Flat Fixer. Using the two programs, Bang can make a mess and Flat Fixer can clean it up again and again and …

Get the Code

Bang! code is shared on Code Connection at this URL

https://makecode.com/_LC4dzUPe4bYg

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.

Builder Fence with Size and Block Arguments in MakeCode for Minecraft

 

I had wanted to build a fence from the first day I saw MakeCode for Minecraft. Why? To hold all those raining chickens, of course, Due to one of my most endearing flaws, I couldn’t just build a simple, straight-forward fence. I set out to discover the easies, most understandable and fastest way to build a fence using MakeCode  in the Minecraft world.

I explored fences using MakeCode fill, line, and place . I giggled as I watched that funny little agent struggle to build a fence. I compared and raced various fence algorithms against each other. All that exploration may be the subject of a future post, but this post is about the winner (so far) — a fence using the MakeCode builder.

Although I started with a simple fence with a fixed size and material, I wasn’t content with that. I wanted a “utility” fence that I could use in various programs. To that end, I added an argument (parameter) so that the fence could be any size, although it is limited to being square. I also added an argument to specify the material (e.g., wood, stone) from which the fence is constructed. Two fences are shown below.

fence field stone and wood

How Builder Fence Works

The on chat command that launches Builder Fence has two arguments. num1 is the number of blocks per side. num2 is the index of the block to be used to build the fence. Notice that nBlocksPerSide and fenceBlockIndex are global variables, which are set to the respective values of the arguments. The arguments num1 and num2 should not be used as global variable unless you want to make yourself really crazy. The global variables are required because in MakeCode blocks, functions cannot have arguments — too bad, but maybe someday …

The two global variables are not validated here because they are validated in the buildFence function. After the global variables are set, the function setOrigin is called before the function builderFence is called.

Builder Fence Code

The array fenceChoices contains blocks that can be chosen for the fence. The on chat  argument num2, which is stored in global variable fenceBlockIndex may have values 0, 1 or 2 specifying the wood, stone or sunflower fence, respectively. More choices can be added to this array, and doing so does not require any other change to the builderFence code.

Builder Fence Code 4

The function builderFence (below) does the magic. It expects two global variables to have been set prior to calling it: nBlocksPerSide and fenceBlockIndex. The function validateBuilderFenceParams is called to make sure the two global variables are within the allowed values, and, if not, are set to default values. builderFence then calls makeEastPointerFromOrigin and setOrigin, which sets origin if it has not been already set. After this initialization, the real fence-building action starts.

The main part of builderFence starts when the builder teleports to origin. By facing the builder East and doing right turns, the fence will be to the east-south  of origin (the positive X and Z quadrant). This knowledge may be important to a program that calls builderFence.

Builder Fence Code 2

The mark placed by the builder just before the repeat block, is be the starting place of a path that will be traced. During each of the four passes through the repat loop, the builder moves forward one block fewer than nBlocksPerSide then turns right. One  fewer blocks is required because the builder  is standing on one of the blocks before it  moves making the total blocks on each side be the correct number. Only after the builder has traveled the entire path (after the loop), the builder does the trace path with the block from the array fenceChoices designated by fenceBlockIndex

Future Fences

An obvious upgrade to the fence described here, is to enable the fence to have a different width and length rather than be square. Also, other fence material options would be good. I have also experimented with fences that are more than one block high.

Get the Code

Builder Fence  code is shared on Code Connection at this URL

https://makecode.com/_RHMLRD0KUXeC

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.

Remember Where You Started in MakeCode for Minecraft

In most of my programs, I need a variable that saves the starting position. For example, in Handy Compass I needed a reference point from which to print the four directions. I call such a reference point “origin.” Origin is the player world position except that it is always on the ground even if the player is flying or standing on a structure. Origin is on the ground where the player’s feet would be if the player standing on the ground. The X and Z coordinates of origin are the same as the player world position. The function setOrigin is designed for a flat world.

Sometimes I run  multiple programs at the same time, for example, The Art of Random Walks. In such a case, I  usually want origin to be the same for all programs and set only once.  This is accomplished in the following two steps:

  1. Make origin a global variable by declaring it outside all function and on chat blocks. Since origin is a position variable, it should be set to null, which means it has no value. The variable  origin cannot be set to zero because it is a position variable. A position variable consists of 3 values, one for each of the e 3 coordinates X, Y and Z.  In MakeCode blocks, global variables should be initialized in an on start block.
  2. In the setOrigin function, or any other program that might set the origin variable, first test to see if origin already has a value. Only if it is null (does not contain a position) should origin be (see code below).

Set Origin code

The on start block below sets the variable origin to null when the start button start button   is pressed. I program my on start block as is shown on the left below; however, MakeCode usually, eventually changes it to be as shown on the right. The two are equivalent, so I don’t get too excited.

The on chat command below is used to test the function. It calls setOrigin, then places a gold block under origin, which is be level with the grass in a flat world.

Set Origin Run Code

In the JavaScript below, note that origin is set outside the onChat and the function; therefore, it is a global variable and can be used by both the run on chat and the setOrigin function.  Also notice the use of the value “null.” I have not found such a value in the MakeCode blocks, so I switch to JavaScript to set it.

JavaScript

Get the Code

Remember Where You Started code is shared on Code Connection at this URL

https://makecode.com/_3wLH9K3Ashg1

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.