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.

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.

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.

Handy Compass in MakeCode for Minecraft

 

I like knowing where I am. Handy Compass is a utility hat I use often in other programs. In a flat world, Handy Compass places a gold block level with the grass under the player. It then  prints the sign and coordinate name (e.g., +X, -Z) as well as the direction (e.g., E, N) in each of the four directions. An example of Handy Compass output is shown below.

Handy Compass Field 2

I use this compass  instead of the example compasses shown on the MakeCode for Minecraft website because Handy Compass is designed to be background to another program, not the feature.

How Handy Compass Works

Handy Compass is offered as a Chat Command, not a function,  so that it can be run asynchronously with the program it supports (e.g. The Art of Random Walks).

Handy Compass operates with the following steps:

  1. Says the program is running.
  2. Tests the position variable origin for whether or not it has been set before the function was called. If so, it is left as was, but if it was null (not set), it is set to the positon of the player’s feet.
  3. Says the coordinates of the origin.
  4. Places a gold block at origin, but one block down so that it is level with the ground.
  5. Calls a function for each of the four directions.
  6. Says that it is done.

handy compass code

Each of the four directions has an assonated function. Each function calculates X and Z for where to print the direction information. All printing is at vertical positon 20, which was chosen by trial and error. In each print block the direction of print is chosen so that if the player is facing the corresponding direction, the print reads from left to right. 

East

The function for East sets X to positive 50, meaning 50 blocks to the East from origin. The function prints towards the South  starting at Z = negative 20 blocks from origin, which was chosen by trial and error.

South

The function for South  sets X to positive  50, meaning 50 blocks to the South from origin. The function prints towards the West  starting at Z = positive 20 blocks from origin, which is chosen by trial and error.

West

The function for West sets X to negative 50, meaning 50 blocks to the West  from origin. The function prints towards the North starting at Z = positive 20 blocks from origin, which is chosen by trial and error.

North

The function for North  sets Z  to negative 50, meaning 50 blocks to the North from origin. The function prints towards the East starting at X = negative 20 blocks from origin, which was  chosen by trial and error.

Handy Compass Example Uses

See the Categories index for the blog for example uses of Handy Compass. A particularly nice example is The Art of Random Walks because Handy Compass run asynchronously, that is, the compass is created during the random walk.

Get the Code

Handy Compass code is shared on Code Connection at this URL

https://makecode.com/_Ti5fXbTs27qM

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.

Point East in MakeCode for Minecraft

In the real-world, the custom is that North is our primary reference direction. Once we know which direction is North, our mental map falls in to place: when we face North, we know that right is East, behind is South and left is West. So to find ourselves, all we have to do is find North. Finding North has progressed from using the stars through the compass to today’s GPS system.

In Minecraft, East is like real-world North.  if you think of yourself as facing East, South is to the right, North behind and West is left. So, all we need to do is find East to know where we are. Another reason to find East, is that relative to the player’s position, East is positive X and South is positive Z, making the Eastsouth quadrant the area of  (positive-X, positive-Z) coordinates from where the player stands. Using (positive-X, positive-Z) coordinates often makes moving and building easier to visualize.

Finding East

So which direction is East? An easy way is to plant a sunflower.

When looking at the back of the sunflower, the player is facing East, South is to the right, West is behind and North is to the left. Of course, when facing the sunflower, the player is facing West. Why do Minecraft sunflowers always face East? Imponderable. But, When I am building, I don’t want sunflowers scattered throughout my world. But, when I  am building, I don’t want sunflowers scattered throughout my world.east pointer

To point North, I use something more subtle that doesn’t get in the way of my structure. In some cases, I use a couple of blocks placed under the player’s feed to point East. I use the gold  block to make the “origin,” my usual name for the initial positon of the player. The blue block (lapis lazuli) is east of origin. I use these blocks as anchors for testing my mental map against the actions of my program. For example, when using the Builder to build a fence, I like to start at origin, move east, turn right, and so on, back to  origin.

East pointer with sunflowerBut, is the blue block pointing East from origin? Test it with a sunflower. The blue block is pointing the same direction as the sunflower, so East it is.

 

 

 

 

The Code

The East pointer code is implemented as a function because I use it in many different programs. Such routines are called “utilities” and are part of every programmers bag of tricks.

Point East code only

The function starts with a say block, which notifies the user that the function is running. Next, iIf the variable origin has not been set, it is set to the X and Z of the player world position with Y where the  player’s feet are (Y = 4)  in a flat world. For debugging purposes, a say block prints origin coordinates to the Command window. The gold block is placed under the original positon of the player’s feed with the blue block one block to the east. Point north chat

A run command launches the function debugging. The function is designed so that origin can be a global variable that is set before the function is called. In this case the once origin has been set, the player can be an another location and can move while the function executes.

This function is used frequently in programs documented in this blog, for example, Builder Fence.

Get the Code

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

Move Me: Better Ways to Move the Player in MakeCode for Minecraft

Move Me is a handy gadget  that I use when debugging my code. When on the hunt for programming bugs, a clean world area is needed every time the program is tested. To date, I have thought of five ways in which this can be done:

  1. The most obvious is to use the keyboard or buttons to fly the player far away from the trashed area to a clean area. My fingers get tired, I get lost, and flying is so boring.
  2. A better way than flying is using “/tp @s ~200 ~0 ~0” in the command line to move relative to the player 200 east, 0 up, and 0 south — or however far and in which ever direction you like. Typing this makes me crazy because I must look at the keyboard for the symbols I rarely use. And, of course, I make mistakes. When using this method, I type the commands I use often into a text document, which I keep open,  and copy/paste to the command line. Efficient, but I don’t like having still yet another open file, and going back and forth to it is distracting.
  3. Make a program that with one easy-to-remember chat command moves the player to a clean positon. Move Me is such a program and  is described in this post.
  4. Make a program that restores the used world area to it’s pristine condition. Flat Fixer is described in another post.
  5. Create a new world. Way too time consuming, but I use it when I want a really, really clean world.

When developing a new program, especially when leaning a new language as I have been doing with MakeCode, I usually try the program many, many, many times until it works correctly and I have settled on the programs features. Move Me is my favorite way to get to a clean world area.

Move Me

With just one chat command using Move Me, the player is moved from the mess on the left  to the pristine field  on the right so far away from the mess that it can’t be seen.

Move Me CodeMove Me has two parameters (arguments): E and S, which specify the distance to move east and south, respectively. Of course, negative E is west and negative S is north, so the program allows movement in all four directions. If both parameters are 0, there would be no movement. This case is used to trigger the default values of 200 E and 0  S. If a parameter is omitted, its value is 0. Typing “run” in the command line with no values for E and S is equivalent to E=0 and S=0, which  triggers the defaults, and is the same as typing “run 200 0.”

Notice that the teleport block specifies a relative positon. It causes the player to be moved E blocks east and S blocks south of where the player started. The up coordinate moves 0 blocks, meaning the player stays in the same vertical position.

This program has one big advantage over manually flying to a new location. In manual flying it is easy to accidently return to an old used location. Using Move Me and moving the same direction every time, the player never accidently returns to an old trashed area. In most cases, I just use “run” with no parameters. The default always move east, so does not return to any prior position.

Move Me takes some of the pain out of debugging.

Get the Code

This code is sometimes susceptible to the Chat Command arguments bug described the our prior post.

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

 

Lucy in the Sky with Diamonds in MakeCode for Minecraft

Lucy in the Sky with Diamonds was a popular 1960s Beatles tune.  Before you ask, yes, I am old enough to have been there. The screenshot below is from a little MakeCode chat command using a print block with text “Lucy” and diamonds to do the printing. 

lucy

lucy code 2There are a few things we can learn from this code about blocks, positions and directions.

Which blocks (e.g., diamonds, grass) can be used to write in the sky?. Change diamonds to sunflowers. What happens? Lava? Water?

The position  position  means 50 blocks east, 5 blocks high and 0 blocks south relative to the player; that is, from where the player is standing.

The phrase along gives the direction to print. I think of the player as facing east, so printing along South means printing left to right. What happens if the along phrase is changed to each of the following:

  •  along north ?
  • along west ?
  • along east ?

Keeping Score in the Sky

field scoreIn Grandma’s Chickens, I used a similar  function to keep score of how many chickens the player zapped.

run

The “run” chat command  is for testing the function printScore

The first say block signals that  the program is actually running. The last say block indicates when it is complete.  Why are these needed? Sometimes when “run” is typed into the command line, the program does not start. This may be because  start button , the start button, was not pressed. Other times are more mysterious, but I have found that when repeated attempts to start a program fail, restarting Minecraft and MakeCode solves the problem once again proving the old adage “When in doubt, reboot.”

In the chat command “run,” the original player world position is saved in the variable “center.”  This allows the player to move while the program is running. This is desirable because the view of the what’s happening in Minecraft is often better if the player is allowed to move to a different vantage point rather than being required to stay in one place because all positions are relative to the player and change as the player moves.

The repeat loop in “run” calls the printScore function 5 times to test randomly selected values for score..

printScore Func

The function printScore performs as its name indicates. It first converts the number variable score to text, which is required by the print block. The say block outputs the score to the log window.

In the print block, by joining txtScore with trailing  spaces, the digits of any longer number that had been previously printed to the same location are erased.

Pay particular attention to the manner in which position is specified by the at phrase. The position is not specified as relative to the current player world positon but, rather, as relative to the original player world position, which was saved in the variable  “center.” Notice that printScore coord is a world position, not relative. The print position is center X plus 40, center Y plus 5, center Z minus 5. The value of these coordinates were determined by trying various numbers to find a visually appealing  positon.

Demo of App and Code

Get the Code

The code for Print Score and Lucy is shared at https://makecode.com/_HpX7bY2ehM3H . It can be downloaded and run in MakeCode for Minecraft.

 

 

 

 

Go to Ground in MakeCode for Minecraft

MakeCode for Minecraft coders sometimes assume that the player will be on the ground when the program starts. There is no harm in this except that some programs do weird things if the user forgets to move the player to the ground before running it. Users may find the weirdness difficult to understand. A wise coder would teleport the player to the ground as a first step in a program.

In a flat world with no complex structures, the player is teleported to the ground with code block below.

Go to Ground 1a

We can test this statement by wrapping it in a Chat command.

Go to Ground 1

Try flying the Player to various locations in the sky, then run the chat command to verify that the player is indeed teleported to the ground with no change in the X and Z coordinates.

A Reusable Go-to-Ground Function

Although only a single block is required to teleport the player to the ground, the block is so wide that often it cannot be read without scrolling to the right. Further, recoding the block for many programs can get old, Recoding can also introduce bugs because some little mistake is made in the recoding. I have, for example, mistakenly coded the Z coordinate as X, since X is the default in the “position get value of” block. As a remedy to all these issues, I created a little function that can be copied  to any program where it is needed.

Go to Ground 3

To test the function, I created the chat command “goToGround” shown below.

Go to Ground 2

Get the Code

The code for Go to Ground is shared at https://makecode.com/_8HhHxbTxMiu3. It can be downloaded and run in MakeCode for Minecraft.