Site icon We Code MakeCode

Code Part 3 — Build the Field for Riding the Rail in MakeCode for Minecraft

 

The Code that Builds the Field for Riding the Rail

The code in the not-grayed-out area near the middle of the screenshot below builds the field (e.g., fence, rail and control buttons). 

All the on-chat commands that start the program (described in Part 2), end by calling two functions:

The function init_beginning

The function init_beginning performs the following actions:

The Functions moveMe and setCenter

The function MoveMe, which is called by init_beginning (see prior section), moves the player 200 blocks east and to the ground. The new MakeCode ground block could have been used to do this. MoveMe calls setCenter, which sets the variable “center” to the player position. Riding the Rail code does not depend on the position of the player except for initially setting the variable “center.” After center is set, the player can move anywhere for a better view of the action.

The on-chat command “moveMe” is provided in case a player wants to move after a game. After a particular run of Riding the Rail, the MOBs can be noisy (I am especially distracted by the disgusting noise that Shulkers make) so I move to a quite place.

Initialize Sides and Corner Positions

The function init_sides_and_corners, which is called by init_beginning, sets a number of variables that are used in fence and rail building. Notice that rail_corner and fence_corner both refer to a northwest corner, which when the player is facing east, is down and to the left of center so both adjustments from center are negative to get to a northwest corner. This function calls init_rail_corner_lists, which will be described below.

On-chat command to build he Fence

The on-chat command “fence” is launched by init_beginning to build the fence. This is an on-chat command, instead of a function, so that it will run at the same time (asynchronously) as the rail is build. This speeds up the program. fence  can be run asynchronously without fear of interference with other functions because it changes no variables and uses no variables that might be changed elsewhere.

As shown in the drawing below, the four sides of the fence are built starting at the northwest corner and continuing in the following order:

  1. northwest corner to northeast corner
  2. northeast corner to southeast corner
  3. southeast corner to southwest corner
  4. southwest corner to northwest corner

The fill blocks in the graphic below are in this same order.

Notice that in each fill block, the middle coordinate Y varies from 0 to 2, which makes the fence 3 blocks high.

Function to Lay the Rail with Redstone Under It

The function lay_redstone_and_rail is called by init_beginning. It sets the variables used by the function builder_square and calls it twice: once the lay redstone under the rail and once to lay the powered rail.

Function to Build a Square with Missing Corners

The function bulder_square does build a square; however, the corner blocks are missing. The corners will be laid later because Minecraft requires that the straight sides be placed first so that it will know when to make a rail curve. I tried using  fill to lay the rail segments; however, this often laid the rails in parallel rather than connecting them. Frustrating.

Function to Lay Rail Corners

The function lay_corner_blocks is called by init_beginning. This is a separate function so that it can also be called from a on-chat command corners. The on-chat command may be useful for repairing rails that have some square corners.

This function lays unpowered rail blocks because Minecraft will not turn powered rails into corners.  No matter — when using power, momentum carries the minecarts swiftly around corners, sometimes too swiftly with heavy MOBs (they fly right off the rail when trying to make the turn).

This function uses two four-item lists of position (see below)s:

By using these lists, a for_loop from 0 to 3 can be used to loop through the four corners.

In the loop, the variable “where” is set to the corner position where the rail will be. The variable “under_where” is set to the corner position under the rail where the cobblestone will be.

The fill statement using air is employed to remove a undesirable square corner rail before placing another  in hopes that the new rail will curve. Through trial and error I found that the brief pause after replacing the old rail with air, then placing the new one makes corners works more often. But, unfortunately, not always.

Function to Set List of Rail Corner Positions

The two corners lists (below) are initialized by the function init_rail_corners_lists.

The two empty items in the  rail_corners list are there so that the code will be vertical, not horizontal. The empty items have no effect on the size of the list or the code accessing the list. Empty items can also appear inside the list with no problems — empties seem to be ignored by MakeCode.

Control Buttons

The Spawn Position is the rail position east of the corner (left in this picture). When a minecart is placed on the rail, the next MOB in the spawn  list is spawned at the Spawn Position.

Four blocks are placed at the same level as the grass. I call these control buttons because when one of these is broken, it causes some action to take place. The broken block is replaced with either the same block or another block depending on the particular block.

Define Spawn Position and Place Control Blocks

The function shown below first defines the Spawn Position, then places the four control blocks. The Spawn Position is calculated as one block east of rail_corner, which was set in a previously defined function. The control blocks are set relative to the Spawn Position. Notice that, from SpawnPosition, the east/west  coordinate X  is incremented by 1 for each blocks will be side by side.  The Y coordinate is -1 so that the blocks will be level with the grass. The Z coordinate is the same for each block so that the blocks will be in a row.

Code Requires a Flat World

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

Get the Code

Riding the Rail code is shared on Code Connection at this URL

https://makecode.com/_bPdMMvgg5Mjw

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.

Exit mobile version