# Coordinates Tutor in MakeCode for Minecraft

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.

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 }.

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.

### 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.

### 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.

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.

#### 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.

### 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.

## 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 , which is upper right in the Code Connection window just below the banner. This will open the window shown below.

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

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

Click the 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.

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

## 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.

## 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.

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.

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.

## 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.

## 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 , which is upper right in the Code Connection window just below the banner. This will open the window shown below.

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

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

Click the 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.