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

 

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

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

Field GrandmasChickens

Strategy for the Design of the Grandma’s Chickens Game

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

The Code for Grandma’s Chickens

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

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

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

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

GrandmasChickes Code 1
Grandma’s Chickens Part 1 of 4

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

GrandmasChickes Code 2

Grandma’s Chickens Run —  Part 2 of 4

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

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

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

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

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

GrandmasChickes Code 3

Grandma’s Chickens Run —  Part 3 of 4

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

GrandmasChickes Code 4

Grandma’s Chickens Run —  Part 4 of 4

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

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

Grandmas Chickens kill
grandmas chickens giveswords

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

Don’t Worry About the Chickens

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

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

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

Get the Code

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

 https://makecode.com/_RFUKHraM7Tbp

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

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

Import Copy link

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

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

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.

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.

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.