Apple-Eating Snake in MakeCode Arcade

 

The apple-eating snake is a well-known game that has been implemented on many different platforms. Two years ago, we built it using Scratch 2.0. In  MakeCode Arcade, it was even easier to build. Screenshots from the Arcade simulator, which runs in a browser, are shown below.

Apple_eating Snake on iPhone Double

How to Play the Apple-Eating Snake Game

The game starts with a short snake and 1-3 apples randomly placed on the screen (left screenshot). The player moves the snake up, down, left or right with the pointers. Each time the snake’s head (green) intersects an apple, the score is upped by one and the snake’s body grows longer.. The game ends with the snake head either hits a wall or hits the snakes body. The right screenshot above shows the game at 37 points with a much longer snake.  Game nuisances include:

    1. The snake head cannot move backward because that would intersect the snake body. The snake head can be alongside the body, but cannot intersect it.
    2. In the Arcade simulator if the player touches the area between two pointers (e.g., the left and up), the snake moves diagonally. If done by accident, the snake head may hit the wall or the snake’s body unexpectedly and end the game.

In addition to the Arcade simulator, which runs in a browser, an Arcade game can also be run on several devices. In the photo below, the Meowbit is running the Apple-Eating Snake game. The last section of the post will explain how to download a MakeCode Arcade game to a Meowbit.

 

The Code

Game Initialization

The code below initializes the game by setting the background color, setting sprites of which the snake is composed to 8 pixels, calling functions to create the snake head and body, creating the apples and setting the score to 0.

on start code

The function creating the snake’s head creates a square green image to be used in creating the sprite. The last block requires the snake head to stay on the screen. If the head goes off the screen, the game will end.

creak snake head

Creating the snake’s body is a little more complicated because a snake has only one head but may have many links (sprites) in its body. The links in the snake are stored in a list that is an array of sprites of type snake_body_sprite. A function is called within a loop to build a snake of 4 sprites, each positioned below the preceding. Of course, the first body sprite is positioned just below the snake head.

The function make_body_sprite takes two arguments that are the x and y positions of the sprite to be created. Because the initial snake is to be positioned vertically, the x position is always the same as the snake head, while the y position varies.

create snake body

The function create_random_apples generates one or two apples keeping the total number less than or equal to 3. Each apple is placed at a random position on the screen.

create random apples

Move Snake with Up, Down, Left and Right Buttons

The buttons move the snake. Each button

  1. moves the snake head in the direction indicated by the button,
  2. places a body sprite where the head was and
  3. removes a sprite from the end of the snake.

Because steps 1 and 2 are the same for each of the four directions, the function below embodies the two steps. Rather than destroy one sprite and create another, the tail end sprite is moved to the prior head position. The moved sprite must be both physically moved on the screen and also moved in body sprite list  to reflect its new position.

move body sprite.PNG

The function for each direction, calls the function above, then moves the head in the appropriate direction. Each direction has two button events: one for when the button is pressed and released quickly and the other for when the button is held down. The same action is performed in each case. When the button is held down, the direction function (e.g., move_up) may be called many times.

pointer buttons

Score by Eating an Apple

The code below is triggered when the snake head overlaps with an apple. In this code segment, then score is incremented, the apple sprite is destroyed, the apple count is decremented, the snake is lengthened by one sprite and more apples are created.

The challenge in lengthening the snake was to place the new sprite at the end of the snake. But, which direction from the current last sprite? The answer depends on the positions of the last two sprites. The new end sprite extends the line between the prior two sprites. In the code below, the variables dx and dy are the change in x and y coordinates. This calculation is done instead of numerous if blocks that would go something like:

If the last block is below the next to the last block, place the new block below the last block. If the last block is left of the next to the last block, place the new block to the left of the last block. And so on.

score

End Game

The game ends when the snake head intersects the border (wall) or the snake body. The event below ends the game when the head intersects the body. The function create_snake_head specifies that the game ends when the head intersects the wall.

end game if hit wall

Download and Run Code

Run this game by clicking on the URL below.

https://makecode.com/_bdkguYHP4iEC

This will open a browser as shown below. Have fun playing the game.

snake in browser

To see the code, click the Show Code button on the screen above. It is  near the top left after the game title. This will open the browser window below.

code in browser

Using the browser window above, one can download and run the code in the normal MakeCode Arcade environment. In the browser window above Just click on the little icon to the left in the red border just below the game title. This will open the browser window below.

snake in makecode with sim

From the browser window above, the game can be saved to a hard drive by clicking the disk icon to the right of the game name (Apple Eating Snake).

The purple Download button is for downloading the game to another device, such as the Kittenbot Meowbit, which is shown in the screenshot below connected by USB to a laptop. This photo was taken just after the game Apple-Eating Snake was downloaded to the Meowbit.

Meowbit-connected-to-computer-USB.jpg

When the Meowbit is connected by USB to a computer running MakeCode Arcade in a browser window, pressing the Download button will display the option to save to the Meowbit, which appears as a hard drive (similar to a phone) in the Windows File Explorer window (left).

 

The  Meowbit display should be as shown on the right (ready to download). The ready to download display will be shown on the Meowbit when it is connected by USB to a computer and powered on. When the download is complete, the screen will show the game. At this point, the Meowbit can be powered off and disconnected from the computer. It can be connected to a battery, then when powered back on, the game will be displayed.

Amazing Maze Maker in MakeCode for Minecraft

Maze Gif

maze 20 30 51

2 default mazes

The mazes generated by Amazing Maze Maker can be used by Mazer, the Maze Walking Snake in MakeCode for Minecraft.

Using Amazing Maze Maker

Run Chat Command Generates a Single Maze

The run chat command with no arguments generates the default maze, which is  10 blocks x 10 blocks with minimum wall of 4 blocks.

The run chat command can have the following three arguments:

  • num1 — the number of blocks for the X direction (east/west). Minimum 10.
  • num2 — the number of blocks for the Z direction (north/south). Minimum 10. If Z is 0, it is set to num1 generating a square maze.
  • num3 — the minimum number of blocks in a wall. Must be 4 or greater. Default is 4. If needed, num1 or num2 are adjusted so that each side can be divided resulting in wo areas that are larger than num3 (the minimum).

Maze Colors Can be Changed in On Start Block

The blocks from which a maze is built can be changed in the on-start block shown below. If using the program Mazer to walk the maze, the 3 maze blocks must also be changed in Mazer to match the ones here.

on start code

Erase Chat Command Erases the Maze

If the player stands  on the ground left of the sunflower and a few blocks west so that the maze is in front, this command will erase a maze so that the area can be used to generate another. Instead of erasing, however, a player can just move to another part of the world to generate another maze.

The Algorithm

Amazing Maze Maker employs the recursive-division maze generation algorithm. This algorithm creates a maze with no loops; that is, a simply connected maze, which is also called a perfect maze.

The algorithm is called “recursive division” becuase it uses recursion to build the maze. Recursion in computer science is is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. In coding, this means that a function calls itself or calls a function that calls the first function.

The drawing below is the pseudocode for the recursive-division maze generation algorithm. Pseudocode is a notation resembling a simplified programming language, used in program design. This drawing illustrates recursion because the function Divide calls the function Divide_NS, which calls the function Divide. The arrows indicate calls. Notice that the function Divide returns (exits) to its caller when a wall is so small it can no longer be divided into two walls that are larger than the minimum.

Recursive Division Maze with border

Coding Strategy

Recursion using MakeCoode blocks is challenging becuase MakeCode block functions cannot have parameters. This is not the place to rail against this MakeCode shortcomings, but believe me, this particular one keeps me frustrated. Yes, I know that I could have used JavaScript,, but I prefer to stay with those colorful, readable, loveable blocks.

The pseudocode above shows that the recursive function Divide takes three arguments: a position, a X length and a Z length. These three parameters define a rectangle as shown in the drawing below. In each area of the maze the X and Z arguments become smaller with each recursive call to divide until eventallly they asre too small to further divide and the calls unwind. When one area of the maze has been completely divided, the algorithm moves to another area and divides it recursively. This results because the function Divide_NS and Divide_EW each call Divide twice — once for the leff or bottom area of the current area and once for the right or top area of the current area.

Recursive Division Maze 2 border

Now, what to do about the fact that MakeCode function blocks do not allow parameters? I used three stacks (corner stack, X stack and Z stack) instead of parameters. Before each call to Divide, corner, X and Z values (that would have been arguments) are pushed onto the stack. Just before divide exits, the same three values are popped off the stack.

A stack is an abstract data type that operates as a LIFO (last in first out) list. At a minimum a stack has push and pop operations defined. Push puts an item on top of the stack and pop takes the top item off the stack. An optional peek operation enables viewing the top item on the stack without modifying the stack.

A stack may be implemented in a variety of ways. For this program, the push operation is implemented by adding an item to the front of a MakeCode list. The pop stack operation is implemented by removing a value from the front of the list. A stack could have equally well been implemented using the end of a list instead of the beginning, in which case, push would put a value at the end of the list and pop would take it off of the end.

The Code

All the code is shown in the screenshot below.  In the screenshot to the left is the initialization code. The third column starts the maze. Columns 4 and 5 divide an area and create either a NS or an EW wall. Each section of the code will be explained in later sections of this post.

code all

Run Starts the Program

Run calls several functions to check argument values, initialize stacks and set anchors. These three functions are defined towards the end of this post. The function do_maze launches the algorithm.

run code

do_maze Calls Divide with Three Arguments Using Stacks

Notice that do_maze, pushes the required three arguments onto their respective stacks before calling divide.

do maze code

Divide Function Is Recursive

Depending on which wall (NS or EW) of the current area is longer, Divide calls either divide_along_NS_at_X or divide_along_EW_at_Z. If the longest wall is too short to divide into two at least  minimum length walls, divide exits, returning to the block after the block that called it.

divide code

Divide an Area and Make an Opening in the Dividing Wall

The two functions and their supporting routines are shown after this screenshot, which shows a potential problem in the placement of a wall. A wall dividing an area is placed randomly such that each resulting wall is longer than the minimum. The random position chosen might be such that one or both ends is at an opening in a previously placed wall. In the screensot below, the right horizontal wall blocks an opening in the vertical wall on its left. In the code to place a new wall (below), the functions get_new_wall_X and get_new_wall_Z search for an a wall that does not block an opening using the  functions try_wall_X and try_wall_Z.

problem maze

Notice that divide_along_NS_X below, calls divide twice — once for each area resulting from the wall it placed. The first thing that this function does is to get the three arguments from the three stacks. Conceptually, a peek operation is used to do this since the MakeCode list operation used dos not remove the value from the list. Careful examination of the code will reveal how the three  arguments are set up for each of the two calls to divide. At the end of the routine, he last function called pops each of the three stacks.

divide NS code

The functions below divide_along_EW_at_Z differs rom the one above by the direction that the wall is placed.

divide EW code

Build Wall Function

Both the above functions call the function build_wall to place the wall between the endpoints calculated by the calling function. It also places an opening in the wall at a random location between the endpoints.

build wall codce

Initialize Stacks and Pop Top Items

init stacks code

The function below pops (removes the top) values from the three stacks that are arguments to Divide and its subordinate functions.

stack remove code

Check Parameters of the Run Chat Command

The function below checks the arguments proved to the run chat command.

check params code

Set Anchors and Position Player

seet anchors code

Function to Do Outer Maze Border with Openings

outer border code

Erase Chat Command

erase code

Code Requires 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

Amazing Maze Maker code is shared on Code Connection at this URL:

https://makecode.com/_FiH5aD1Y6dru

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.

Mazer, the Maze Walking Snake in MakeCode for Minecraft

worm1

snake in maze 1

snake in maze 2

big Mazze Walked.png

Using Mazer

Generate a Maze

Obviously, Mazer needs a maze to walk. Our program Amazing Maze Maker  builds mazes.  Run it to generate one or more mazes before using Mazer.

If Necessary, Change Mazer Blocks to Match the Maze

The blocks from which the maze is constructed are the first three in the code below. They should match the settings in the maze generation program.  The last two blocks are he blocs from which the snake is constructed including the snake body block (black here) and the head block (red here).

init blocks code

Stand in Front of the Maze Facing East

To use Mazer, the player should stand a few blocks south (right) of the sunflower on he ground in front of the maze. Sunflowers always face east. The player can stand several blocks in front of the maze, but must be in front of it — not to the side.  Mazer first finds the opening near the sunflower, then moves to the opening and starts moving through the maze until it finds the exit, which is on the opposite side of the maze.

sunflower

Run Chat Command to Start the Program

Mazer’s run command has one parameter, the length of the snake, which defaults to 5 blocks. Longer snakes are fun to watch because in some mazes the snake’s tail may block an opening that the snake must go through. Mazer drops blocks from the end of its tail until it can get through the opening. Life often requires some sacrifice, and so it is with Mazer.

Erase the Snake and Its Trail to Start Over

The chat command “erase” is used to erase the snake and its trail so that the maze can be used again.

The Algorithm

Mazer implements the best know of the maze solving algorithms: the wall-following algorithm. Such an algorithm only works with simple mazes; that is, mazes that have no loops. A wall-following algorithm can be left or right. In a right-following maze algorithm, the actor is thought of as moving through the maze with their right hand always touching a wall. The actor will exit the maze at another opening or, if none, it will exit where it enterd. Mazer implements a right wall-following algorithm.

A snake in a maze presents in interesting challenge because it occupies more than one space. What if the snake’s tail is blocking an opening within the maze through which the snake must pass. The algorithm implemented here drops tail blocks from the snake’s end until the opening is no longer blocked. We explored other options; e.g., making all openings two bloicks wide.

The Code

Run — the Main Control of the Program

The “run” chat command does the following:

  1. Initialize b_stop to false. The chat_command “stop” can set this to true to cause the snake to stop its movement and the program to be exited.
  2. snake_len is set to the argument num1 or if it is greater than 5 or to 5 which is the defalt.  In other words, a snake must be at least 5 blocks long. I often set the length to 20 so that I can watch the tail drop off when the snake gets stuck.
  3. Calls a function to set the origin and move the player.
  4. Initialize constants used by the program.
  5. Make the snake, wchih is placed  where the player was initially standing with its head towards the wall of the maze.
  6. Search for the maze door on the wall the snake is facing.
  7. If the door is not found, exit the program. Otherwise, move the snake to the door. The snake’s head will be just inside the door so that it is not on the grass.
  8.  Enter a loop the will continue until the snake’s head is on grass, which means that it has found the exit door. This loop can be stoped if the player enters the chat command “stop.”
  9. For each pass through the loop, the following is done:
    • calls a function to get the next move, whcih depends on the snakes surroundings.
    • call a function to move the snake one block as specified by the get_next_move function.
    • Tests if snake’s head is on grass.
  10. When grass is found, the snake is moved completely out of the maze and the program ends — one more maze walked.

run code

Initialization

init code

Make the Snake

make snake code

Test If the Snake’s Head Is on Grass

test if grass code

Geet Next Move

get next move code

Left and Right Turns

turn code

Move the Snake

move snake code

Test Snake Surroundings

test for air code

Find the Door into the Maze

search for door

Search East to Find the Wall

find wall code

Search Left or Right to Find Door

search for dor in one direction code

Move the Snake to the Door

move snake to dor

Erase Snake and Trail

erase code

Code Requires 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

Mazer code is shared on Code Connection at this URL

https://makecode.com/_g7t6TVJJC0p6

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.

Snowman Army in MakeCode for Minecraft

cover war

Snowmen Are Fearless but Usually Harmless

A snowman army is awesome to watch in action. Snowmen are brave litle guys. They always attack no matter how outnumbered or how powerful the enemy. They encircle an enemy and mercilessly throw their snowballs. On the other hand, snowmen can be stupid, for example, when most of a snowman army chases a harmless little slime while a few of their comrades take on an army of witches. I made a program to enable experiments challenging a snowman army with a variety of  enemies, such as, creepers and wither skeletons.

Iron Golems Win Unless They are Way Outnumbered

Having tired of snowman army wars, which are rarely won by the snowmen, I introduced a my army option of  iron-golems. Iron golems wipe out nearly every hostile MOB unless they are way out numbered. In the screenshot below, 10 iron golems are taking on a mob of 400 zombie pigmen, which will win in spite of the iron golems frantic hammering. This experiment can be started with chat commands “ig” followed by “zp 20 20” or by the single script command “sc3.”

ig zp many 2

Summary of Our Experiments

The table below summarizes the results of our experiments. The left column identifies the monsters. The second column is an abbreviation of two-word monster types, which can be used as chat commands instead of the monster’s long name. The third and fourth columns indicate whether or not the monster is available in Minecraft Education Edition and Windows 10, respectively. The next columns show war results for snowman armies followed by a column with the results for iron-golem armies. The right column is notes about the enemy MOB.The grayed out lines indicate that wars with that monster are not very interesting for the reasons noted.

monsters 3

Using Snowman Army

My Army, the Enemy and Wars

In this Snowman Army program, a “war” consists of “my army” and the “enemy.” My army is either snowmen or iron golems. The enemy can be any monster available in the version of Minecraft you are using. To start a war, two commands are typed into the chat window — one  to define  my army and one to define  the enemy. The two armies are placed  on the battlefield by the program and the war starts. The two commands are described below.

Quick Start

As illustrated in the drawing below, type into the chat window:

  1. Pick your army — Enter  “sm” or “ig” for  snowmen or iron golems, respectively. Do not include the quotes.
  2. Pick your enemy — Enter  the name of a monster (e.g., “shulker” or witch).
  3. Watch the action.
  4. Go back to step 1 to start another war.

SM army QS 2

For example, the chat commands “sm” then “shulker” shown in  screenshot below, generates the war in the screenshot below it.

sm shulker default commands 2

sm-schulker-default.png

Each army has dimensions, which, if not specified as arguments, are set to the defaults. Because the commands “sm” and “shulker” had no arguments, default dimensions 5 rows of 2 columns each were used. the Minecraft screenshot above, notice the snowballs near the shulkers, which indicate that some of the first row of snowmen had already been wiped out when the screenshot was taken.

Size and Placement of Armies

The following arguments are available or my army chat commands “sm” and “ig”:

  • num1 — number of MOBs (snowmen or iron golems) to the east (columns)
  • num2 — number of MOBs (snowmen or iron golems) to the south (rows)
  • num3 — number of blocks between my army and the enemy and from the fence to an army

sm ig args drawing

The on-chat commands defining an enemy has only two arguments:

  • num1 — number of the enemy  to the east (columns)
  • num2 — number of the enemy to the south (rows)

enemy args drawing 5

Notice in the fist of these drawings that my army dimension are in MOBs East and MOBs South, but the enemy is in blocks East and blocks South. Using iron golems as a my army option, causes this problem. Unlike snowmen, iron golems are more than  three blocks wide and, in addition, iron golems  must be built with space in between because, if they are crowded, they will not come alive.

For a simplistic example, The screenshots below show that the  command “sm 1 1 1″ followed by the command ‘shulker 1 1″ generates a one snowman, one shulker armies that are 1 block apart and one block from a the fence.”

sm shulker 1 1 commands

sm shulker 1 1

The screenshot below uses the test mode of the program to generate default army sizes. Note that the iron golems have no pumpkins on their head thus  they have not yet come alive. I used this mode to test the layouts generated by the program. I used shulkers because they rarely move from their initial position.

test ig shulker default 2

Even Quicker Starts

Random Cases and All Cases

A “case” is a particular choice of my army and an enemy.  To run a cases or cases, using the chat window, type one of the following commands:

  • ” random” — randomly picks my army, the enemy and the size of each. If an argument is specified, for example,  random num1, runs num1 cases sequentially, pausing after each so that the player can watch the action.
  • “allcases” — runs all possible combinations for my army and the enemy with default army sizes.

Both random and allcases sequences of cases can be stopped with chat command “stop” or by breaking any block of the green fence.

Advanced Features

Changing Configuration Variables

Usually a player will never change configuration variables. However, the most likely configuration variable that might be changed by a player is b_Education_Editoin, which if true, causes the program to use only the monsters supported by Minecraft Education Edition. Other configuration variables are discussed in the blog post about the Snowman Army code.

configuration vars

Adding Additional Enemy Types

After a war is started, additional enemies of any kind can be added by typing its name in the chat window. If length and width are provided, they must be less than or equal to the first enemy. If no arguments are specified, the added army will be the same size and in the same position as first enemy.

Adding Snowmen, Iron Golems and Enemies at Random Positions

After a war is started, additional members can be added to the armies via the following chat commands:

  • “addSM num1” — adds num1 snowmen to the field at random positions.
  • “addIG num1” — adds num1 iron golems to the field at random positions.
  • “addEN num1” — adds num1 of the most recently specified enemy to the field at random positions.

Adding a Roof and Rain

Monsters that would otherwise burn up in the normal sunlight can live in a rainy environment. A roof  protects snowmen from melting in the rain. The following commands are provided:

  • “rain” and “noRain” –the current and later cases will either have rain or not have rain, respectively.
  • “roof” and “noRoof” — the current and later cases will either have or not have a roof.
  • “RR” and “noRR”– RR runs roof followed by rain. noRR runs norain followed by no roof.

Optional Fence

The following two chat commands are provided for experimenting with armies that can roam free versus those that are contained by a fence.

  • “fence” and “noFence” — The current and later cases will either have or not have a fence, respectively until another command changes the option.

pic 4

Run on-chat Command

The “run” command is provided just because a user my use it because so many program start with it. The “run” command executes the favs script; thus, providing several interesting cases for the player to view (see the section about scripts below).

Rerun on-chat Command

The “rerun” command runs the prior case using the last specified enemy. This enables a player to explore whether an identical case provides the same result.

Scripted Cases

In the context of MakeCode for Minecraft, a “script” can be defined as a sequence of chat commands that are specified in the script, instead of being individually typed into the chat window by the player. A script may also include pauses, which would normally be the time the player waits before typing in another chat command. The following scripts of interesting cases are built into Snowman Army:

  • sc1 — snowmen, shulkers and witches (see script and  screenshot below)
  • sc2 –iron golems, evokers, skeletons (requires roof and rain) (see second script and screenshot below)
  • sc3 — iron golems and many zombie pigmen
  • favs — will run scripts sc1, sc2 and sc3.

A player can make her own scripts using the included ones as patterns.

script sc1

sc1

script sc2

ig evoker skeleton 2

Player Interface Summary

In summary, a player interacts with the program Snowman Army in the following ways:

  • By typing on-chat commands into the chat windows (e.g., sm, evoker, rain).
  • By breaking certain blocks  (e.g., fence blocks) that trigger a program event
  • By changing program configuration variables (e.g., b_Education_Edition)
  • By writing scripts that automatically run interesting cases.

A more detailed description of the user interface is described in The Code for Snowman Army in MakeCode for Minecraft.

The Snowman Army Program 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.

Get the Code

Snowman Army code is shared on Code Connection at this URL

https://makecode.com/_Waq1xk5K0CfP

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.

iPad Pro 2018 Takes on Surface Studio 2 Running MakeCode for Minecraft

Here I compared the iPad Pro and the Surface Studio 2 relative performance running Grandma’s Game of Life  Blinker pattern. It is a good visual measure of the relative speed of the two implementations of MakeCode for Minecraft Education Edition on these two computers. See the video of Blinker test below.
2018-12-02_21-15-34

New iPad Pro Inspired My New Surface Studio

I was depressed that my new — and my old — iPad Pro ran circles around my old  Surface Studio when running the performance hogging MakeCode for Minecraft program  described in Grandma’s Game of Life in MakeCode for Minecraft . I found such a topsy-turvy world to be quite depressing. Since I always crave the latest and greatest computer, I assumed a new Studio was in my near future, so  I decided to make it sooner than later.

My New Surface Studio 2

On the Saturday after Black Friday, I walked over to the Houston Galleria Microsoft Store to explore buying a Surface Studio 2. I told the Microsoft Store person that I wanted to set up the new Studio 2 and test it with my MakeCode for Minecraft app before taking it home. The results are sown in the video below. I wept.

The iPad Pro and the Surface Studio 2  are running Grandma’s Game of Life, specifically the Blinker pattern. It is a good visual measure of the relative speed of two computers. I have run many other patterns to compare — all with the same results: iPad wins.

Oh, well, I took the Studio 2 home anyway. I want to test it against my old studio. And, I have a month to return it.

Don’t Judge  a Computer on Just One Test Program

I’ve done enough computer-performance testing in my life to fill volumes. I understand that one program is an insufficient test. Rather, one needs a balanced load that represents the sphere of applications and users, blah, blah, blah…… But, in my case, I only cared about this one program — and, the Studio 2 results are dismal.

Surface Studio Has Redeeming Features

On the other hand, developing on the iPad even with the 12.9 inch screen, for the size programs I do, is simply not feasible. And, I love that touch  Studio display — but do I need a new Studio? More exploration is needed.

Code Part 5 — Event Processing for Riding the Rail in MakeCode for Minecraft

 

The Code that Processes Events in Response to Player Actions

The code in the not-grayed out area of the screenshot below processes events triggered by the player when something is clicked or tapped, for example, the Spawn Position or a control block.

code part 5

Loading the Hotbar

hotbar

After clearing it, the player’s Hotbar is loaded by the function shown below with the following items:

  • minecart — must be selected before clicking or tapping to place a minecart on the rail.
  • unpowered rail — useful when repairing square corners that sometimes appear when the rail is generated.
  • power rail  — useful if the player accidentally breaks a powered-rail block.
  • grass — useful if the player accidently breaks grass. I do this when being too zealous with a long tap (tap and hold).
  • minecart with hopper — helps to keep some of the debris off the rail.

hotbar load

Placing a Minecart and Spawning a MOB in the Minecart

Riding the Rail is made especially fun because it is easy for a player to place a minecart on the rail with a MOB in it. How the player does this depends on whether she is using touch or a mouse to click on Minecraft objects. The two ways are:

  • Using a mouse, with a minecart selected in the Hotbar, right click on Spawn Position when it is clear of obstructions (e.g., another minecart, MOB, player). This will place a minecart at the Spawn Position and generate an on-minecart-used event, which places a MOB in the just placed minecart.
  • Using touch requires the following two-step process:
    1. With a minecart selected in the Hotbar, quick tap on the Spawn Position when it is clear of obstructions (e.g., another minecart, MOB, player). This will place the minecart; however, no on-minecart-used event will be triggered. Because the event is not triggered, no MOB is placed in the minecart. Touch quick tap is equivalent to mouse right click.
    2. With a minecart selected in the Hotbar, tap and hold on the brown block long enough to break it. This will trigger an on-minecart-used event, which will result in placing a MOB in the minecart that was placed in step 1. Touch tap and hold is equivalent to mouse left click. See below for more information about the role of the brown block.

buttons for code

On-minecart-used Event

The on-minecart-used event, which is triggered as described in the previous paragraphs, calls a function to spawn a MOB at the Spawn Position, but only if the Boolean variable b_spawn_at_spawn_positoiin is true. This Boolean is toggled by the gray/black block to enable placing a minecart with or without a MOB, respectively (see below).
on minecart used

The on-minecart-used event spawns a MOB at the Spawn Position unless the gray/black block is black. The function that does this is spawn_at_Spawn_Position, which first calls the get_MOB_index function to get the index into the spawn list of the next or a random MOB to be used.  It then spawns the MOB from he spawn list at index index_MOB. The MOB is spawned at Spawn Position.

spawn at spawn position

Brown Control Block Actions

As shown by the code in the following graphic, when the brown block is broken and triggers an on-block-broken event, the block is  restored — and, according to the code below nothing else happens. Hmmm.

button brown code

What actually happens depends on whether the player is using a mouse or touch. If using a mouse, there is no need to break the brown block — ignore it. You can break it all day long but nothing will happen.

If using touch, tap and hold on the brown block long enough to break and a minecart is selected in the Hotbar, an on-minecart-used event will be generated, which will place a MOB at the Spawn Position. Weirdly, no minecart is placed in spite of the on-minecart-used event being generated.  If a minecart is at the Spawn Position, the MOB will be placed in it. The usage here is for the player to short tap the Spawn Position to place a minecart and tap and hold the brown block to generate the on-minecart-used event to place a MOB in the cart.

Gray/Black Control Block Actions

When broken the gray block will be replaced with a black and vice versa; thus causing the block to toggle between gray and black.

button gray black

While it is gray, a MOB will be spawned at the Spawn Position each time the on-minecart-used event is fired. While the block is  black, no MOB will be spawned when the on-minecart-used event fires; thus, enabling a minecart to be placed anywhere on the rail without a MOB being spawned at the Spawn Position. These actions are accomplished by setting a Boolean variable to true or false depending whether or not a MOB should be spawned when the on-minecart-used event fires.

Green Control Block Actions

When the green block is broken, the next MOB in the list is spawned at a random position inside the rail. The broken green block is restored.
button greencode

The on-green-concrete-block-broken event spawns a MOB at a random position inside the rail. The function that does this is spawn_at_random_position.
spawn at random position

The function get_MOB_index

This function is called by both the functions that spawn MOBs. The variable index_MOB is generated by get_MOB_index in the manner determined by the variable b_spawn_in_random_order, which was determined by the on-chat command the started the program.

If the MOBs are to be spawned in random order, the index_MOB is picked as a random number between 0 and the length of the spawn list minus 1.

If the MOB is to be spawned in the order in which they are in the spawn list (sequentially) , index_MOB is set to spawned_MOB_counter modulo length of spawn list. This nifty piece of math guarantees that index_MOB is 0 to length of spawn list minus 1.

Whether random or sequential, spawned_MOB_counter is incremented by 1.

get mob index

Sandstone/Redstone Control Block Actions

When the sandstone block is broken, the redstone blocks under the rail are replaced with sandstone blocks; thus, removing power from the rail. The sandstone control block is replaced with a redstone control block.

When the redstone block is broken, the sandstone blocks under the rail are replaced with redstone; thus, restoring power to the rail. The redstone control block is replaced with a sandstone control block.

The code below that accomplishes this using the northwest and southeast rail corners,  which are diagonally opposing to define the area in which to find blocks to replace. The corners array was defined earlier in The Code Part 3 — Build the Field for Riding the Rail in MakeCode for Minecraft.
button snadstone

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

Code Part 4 — Spawn List for Riding the Rail in MakeCode for Minecraft

 

The Code that Builds the Spawn List for Riding the Rail

The code in the not-grayed-out area on the bottom left and near the middle of the screenshot below initializes the spawn list for a particular run of Riding the Rail. It does this by using the information specified in the on-chat command that launched the program .

code part 4

The Spawn List

As depicted in the drawing below, the spawn list is the link between the on-chat commands (e.g., animals) that start the program and the player-initiated events that enable interaction, for example,  the on-minecart-used event picks a MOB from the spawn list and spawns it in the minecart.

spawn List Big Picture.jpg

The on-chat-commands code put MOBs into the spawn list, which can be any length. Player-initiated events cause MOBs in the spawn list to be used either sequentially or randomly, depending on the player-choice in the initiating on-chat commands. If the MOBs are being used sequentially, when the last MOB in the list is removed, the selection will start over at the beginning of the list. MOBs are never removed from the list.

The variable mobs_to_spawn_list is a list that is initialized the code described in the post. In this post mobs_to_spawn_list is referred to by the short name “spawn list.” The spawn list is set to empty in the function init_beginning, which must be called before the main function described here, which is add_mobs_to_spawn_list_by_source. Note that earlier during coding of Riding the Rail and writing these posts describing the code, the name of the function was ended with “by_method” instead of “by_source” as it is now. It’s the same function.

Add MOBs to Spawn List by Source

The function “run” was described in Code Part 2 — on-Chat Commands for Riding the Rail in MakeCode for Minecraft.  It calls a function that adds MOBs to the spawn list depending on the argument num1 (aka spawn_list_source_of_mob which specifies the source of MOBs and can have values 0 to 3. The following four on-chat commands calls run with the appropriate arguments:

  • 0 — “selected”
  • 1 — “animals”
  • 2 — “monsters”
  • 3 — “all”

This post describes the function in the graphic below add_mobs_to_spawn_list_by_source and the functions it calls. The function is essentially a switch that calls different functions depending on the value of the variable spawn_list_source_of_mobs (aka MOB  source) as follows:

  • If MOB source is 0, the called function is select_mobs_to_spawn, which is modifiable by the player and will be described after all the simpler functions.
  • If MOB source is 1, the called function is add_animals_to_spawn_list.
  • If MOB source is 2, the called function is add_monsters_to_spawn_list.
  • If MOB source is 3, the functions for both animals and monsters are called.
  • If MOB source is any other valu9e, a error message is said.

add mobs by source

Function to Add Animals to Spawn List

In the last block this function calls add_mob_list_to_spawn_list, which does the work of adding one list to another. The preceding three statements set the variables used by the called function. The variable aList is the list to be added, which in this case is set to animalList (initialized when Riding the Rail starts and described below). The variable number_of_each_MOB_to_add is set to 1 here because only one of each animal type is needed. The variable number_of_MOB_types_to_spawn is set to the length of animalList because all animals are wanted.

add animals

Function to Add Monsters to Spawn List

The function to add the monsters list to the spawn list is similar to the one to add animals, except of course, it adds uses monsterList instead of animalList.

add monsters

The Animal List and Monster List

The lists animalList and monsterList  are initialized when the program starts. The on-chat command “animals” places all the animals in animalList in the spawn list, “monsters” places all the monsters in monsterList in the spawn list, and “all” puts both in the spawn list. These lists are also available for use for the advanced player using the “selected” on-chat command.
MOB lists

These lists can be modified by the player. For example, the player might want the items in the lists to be in a different order. She might want to exclude those MOBs that are not suitable for riding in a minecart (e.g., endermen, bats). In other situations, some monsters are not  available in the version of Minecraft being used. For example, as of this writing, only the first three monsters — shulker, vindicator and evoker — are available in Minecraft Education Edition. The other monsters should be removed from the list to avoid spawning “invisible” monsters, which is what will appear to happen if the unavailable monsters are left in the list and are used in the spawn list. Shulker, vindicator and evoker are the first three monsters in the monsterList so that they will be easier to retain and/or use by Education Edition players.

Player-Modifiable Function to Select MOBs to be Spawned

The function select_mobs_to_spawn  is provided for advanced players who want

  • complete control over which MOBs available to be spawned,
  • the MOB order in the spawn list and
  • the number of each to be used.

The function can have any number of sections, which are each terminated by a call to one of the functions add_mob_list_to_spawn_list or add_one_MOB_type_to_spawn_list. The sections can be in any order and more than one of each section type can be included in the function.

select mobs to spawn

The first section shown above adds animal types from animalList starting with index 0 and adding  number_of_MOB_types_to_spawn to the list . The second section is similar but uses the monsterList to add monsters. The last three sections each add one MOB type to the spawn list, which is specified by the variable MOB_to_add. The number to be added is specified by number_of_each_MOB_to_add.

Functions to Add MOBs to Spawn List

The function select_MOBs_to_spawn above terminates each section with a call to one of the following functions:

  • add_one_MOB_type_to_spawn_list or
  • add_mob_list_to_spawn_list.

The first function adds the MOB specified by the variable MOB_to_add to the spawn list number_of_each_MOB_to_add times.

add one type

This function adds number_of_mobs_from_list MOBs from aList to the spawn list. If number_of_mobs_from_list is less than the length of the list, all the MOBs in aList are added. the variable aList was set before this function was called to be either animalList or monsterList. By using the math block min (minimum) of the length of the list and the number_of_MOB_types_to_spawn, the code guarantees that it will not try to add more MOBs than are the aList.

add mob list

Output  Spawn List Information

Finally, messages are said to the player that provide the following information:

  • the number of MOBs in the spawn list. This is particularly helpful to the player who is learning to use “selected” MOB source to verify that the modified function does select the expected number of MOBs.
  • the spawn method: sequential or random.
  • a reminder to the player to select minecart in the Hotbar before clicking or tapping to spawn a minecart and MOB.

say number and surce

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

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

code part 3

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

  • First, they call the function init_beginning, which is the centerpiece function (described below)  that prepares the Minecraft world and sets variables to their initial values. init_beginning and the functions it calls are described in the post.
  • Second, they call one of the two function that prepare the spawn list according the specification of the on-chat-command that launched the program. (see Part 4).

The function init_beginning

The function init_beginning performs the following actions:

  • Moves the player to a new position by calling the function MoveMe.
  • Sets the position variable “center” to be where the player is initially standing.
  • Initializes the spawn list to be an empty list (array).
  • Sets the spawned_MOB_counter to 0. This counter will be incremented each time a MOB is spawned from the spawn list and, if the MOBs are to be spawned sequentially, will be used as an index into the spawn list to select the next MOB.
  • Initializes the Boolean variable b_spawn_at_Spawn_Position to true. This variable will later be used to decide whether to spawn a MOB at the Spawn Position or at a random position inside the rail.
  • Calls the function init_sides_and_corners, which sets several variables that provide position and size information about the fence and the rail.
  • Launches “fence,” which is an on-chat command that builds the fence. An on-chat command is used instead of a function so that the fence will be built at the same time as the rail is being laid, thus speeding up the program.

init beginning

  • Calls the function lay_redstone_and_rail, which builds the powered  rail.
  • Calls the function lay_corner_blocks to place the four corners of the rail. The corners must be placed after the four sides  (Minecraft requirement).
  • Calls the function put_stuff_in_player_inventory to put items that may be useful  (e.g., minecarts) in player inventory.
  • Calls the function set_buttons_and_spawn_position to place the four blocks that will serve as control buttons and to define the variable spawn_position to one block west of the northwest corner of the rail square.
  • Places a sunflower, which always faces east, just inside the fence so that the coder can conveniently know directions when building the rail. The sunflower is near the northwest corner of the fence.
  • Finally, this function teleports the player to a convenient position near the control buttons and the Spawn Position.

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.

moveme and setcenter

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.

init sides and corners

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

drawing

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

fence

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.

lay redstone and 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.

build square

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:

  • rail_corners is the four positions of the rail corners, which will be the not-powered rail that should curve if all goes well.
  • rail_under_corners is the four positions underneath the rail corners, which will be the cobblestone foundation. I picked cobblestone just so that it would look different from the redstone, which  used everywhere else, to make clear that the corners are not powered.

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

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

init_corners

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.

buttons for code

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.
place buttons

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

Code Part 2 — on-Chat Commands for Riding the Rail in MakeCode for Minecraft

The Code that Processes on-chat Commands that Launch Riding the Rail

The code in the not-grayed-out area to the upper left in the screenshot below launches Riding the Rail. These chat commands define the MOBs to be spawned and the order in which they will be spawned.

code part 2

Goals for Riding the Rail included the following user-interface requirements:

  • For new players — a simple way to choose MOBs (animals and/or monsters) that will be available to ride in minecart and
  • For advanced players — the flexibility to choose any combination of MOBs.

The structure of the on-chat commands were the first step in accomplishing these goals. For new players there is an on-chat command for each MOB type; for example, when the program is launched with on-chat command “vindicator,” only vindicators will be available for riding the rail. Every time a MOB is placed on the rail, it will be a vindicator. Similarly, when started with “llama,” every MOB spawned will be a llama. For advanced players, the on-chat command “selected” will cause a player-modifiable function to be used to chose the MOBs thus enabling the ultimate in flexibility.

The on-chat commands that launch the program fall into the following two groups:

  • Commands that specify only one MOB type, for example, llama, pig or vindicator. These commands result in only the specified MOB type riding the rail .
  • Commands that specify more than one MOB type, for example, all  animals, all monsters, all animals and all monsters or a particular combination of specific animals and monsters (e.g., sheep and evoker).

Commands for Only One MOB Type

The simplest way to launch Riding the Rail is to type the name of a MOB in the chat window. The MOB name launches one of the code snippets in the picture below.

select by name

Each MOB name does the same thing except for setting the variable MOB_to_add to the chosen animal or monster. For example, the code below set MOB_to_add to the monster vex and calls the function run_one_MOB_type.
chat one mob type

The function run_one_MOB_type does the following:

  • sets the variable number_of_each_MOB_to_add to 1 (only 1 is needed because, with only 1 MOB type s in the list, it is spawned over and over again for as many as wanted.
  • calls the function init_beginning to move to a new position and build the field.
  • calls the function add_one_MOB_type_to_spawn_list, which does as it name implies.

Commands for More than One MOB type

The on-chat commands in the picture bellow can add more than one MOB type to the list of MOBs available to be spawned. These eight commands, are easier to remember interfaces to the “run” command (discussed next). These commands specify which MOBs to add to the spawn list and the order in which the are removed from the list, which can be either as ordered in the list or randomly selected from the list.

run

The above eight commands, call the run command with the appropriate two arguments:

  • num1 specifies the method used to add MOBs to the list of MOBs available to be spawned. num1 can have values 0 to 3, which correspond to (0) selected, (1) animals, (2) monsters and (3) both animals and monsters. The value of the num1 variable is saved in the variable mobs_to_spawn_list_creation_method to be used when the MOBs are actually added to the list.
  • num2 specifies the order in which the MOBs are to be spawned. The information in num2 is `stored in a Boolean value that is true if MOBs are to spawned randomly from the list or false if they are to be spawned in the order in which they are in the list. The text variable spawn_method is used only for say output.

run

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

Code Part 1 — Overview of Riding the Rail in MakeCode for Minecraft

 

This post is a companion to Riding the Rail with MakeCode for Minecraft, which explains how to use Riding the Rail and  should be read first. This post is an overview of the code for Riding the Rails.

Code Goals

Goals for Riding the Rail included the following typical user-interface requirements:

  • For new players — a simple way to choose MOBs (animals and/or monsters) that will be available to ride in minecart and
  • For advanced players — the flexibility to choose any combination of MOBs.

As experienced coders know, to have both simplicity and flexibility in a user interface is difficult to achieve. After struggling with several approaches, Riding the Rail provides a reasonable solution to this problem.

A further goal was for the code to be able to place a MOB in a minecart rather than place MOBs and minecarts separately while hoping that some of the MOBs will jump into minecarts. Placing a MOB in a minecart proved troublesome; and, in fact, is the reason this program was not released months ago. The new on-item-used event enabled a tidy way to place MOBs in minecarts. Further, the ability to place MOBs and minecarts separately is retained because it can provide much amusement.

Another goal was for the code to build the powered rail without troubling a player with details. This goal was met except that, after the rail is built, on rare occasions, the corner rails are square, not rounded, as they should be. This is easily repaired by the player (see Riding the Rail with MakeCode for Minecraft), but it is a nuisance. While writing this, I was inspired to make yet another change to the rail-building code that I think may fix this problem. We’ll see.

Code Overview

The code for Riding the Rail consists of the following four logical parts:

  1. Part 2: on-chat Commands — The on-chat commands specify the MOBs that will be available to ride the rail.  The on-chat commands also specifies the order in which MOBs will be chosen for spawning (sequentially or randomly). The program remembers the source of MOBs and the order in which they are to be spawned, then moves on to build the field.
  2. Part 3: Building the Field — The code that builds the field on which Riding the Rail is played. The field consists of the following elements: the fence, the rail with the Spawn Position, the control buttons and a sunflower.
  3. Part 4: Spawn List — The code that initializes the “spawn list” using the information it saved earlier about the source of MOBs. The spawn list is the list of MOBs that will be available to ride the rail. The particular on-chat commnad that starts the program causes the variable mobs_to_spawn_list (the spawn list) to be filled with the chosen MOB or MOBs.
  4. Part 5: Event Processing — The code that processes the events that respond to player actions;  for example, right clicking on the Spawn Position to place a minecart with a MOB inside.

All the code for Riding the Rail is shown below.

code all

The post describing this code grew so long that it became nigh impossible to work on. Therefore, I have split the description into four posts, each with its own video, explaining a section of code. The four posts are as follows:

Code Part 2: on-Chat Commands for Riding the Rail in MakeCode for Minecraft

The code described in Part 2  processes on-chat commands that launch Riding the Rail. These chat commands define the MOBs to be spawned and the order in which they will be spawned. The code is the not-grayed-out code to the upper  left in the screenshot below.

code part 2

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

The code described in Part 3 builds the field (e.g., fence, rail and control buttons). It is the not-grayed out routines near the middle of the screenshot.

code part 3

Code Part 4: Spawn List for Riding the Rail in MakeCode for Minecraft

The code described in Part 4 initializes the spawn list with the MOBs specified by the on-chat command that started the program. The code is the not-grayed-out areas (to bottom left and middle).

code part 4

Code Part 5: Event Processing for Riding the Rail in MakeCode for Minecraft

The code described in Part 5  processes events in response to player actions. It is the not-grayed-out areas in the screenshot below.

code part 5

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