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

 

field 70 seconds

Handy Timer

I needed a timer for the game Snowman Army, which I will describe  in a  future post. Games often need a timer. This post describes Handy Timer, which explores how a timer might work in a game.

Handy Timer Strategy

The guts of Handy Timer is a forever loop that increments a second counter each time a second passes. The second counter is displayed each time it changes; however, in a real game other game-appropriate actions could be done each time the counter is updated. . The default second interval is one second, but it can be changed to other intervals, e.g., 10 seconds, 60 seconds.

The Code

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

The code consists of the forever loop, the on-chat commands that control the timer and my ubiquitous moveMe chat command.

Forever Loop: The Guts of Handy Timer

Below is the forever loop guts of Handy Timer. The Boolean variable bRun is true when the timer is running and false when it is stopped. The pause block pauses the code for delta seconds. The variable delta is the timer interval. Actions are taking after delta seconds have passed but only if bRun is true; that is, the timer is running. The actions shown here are to increment the second timers by delta seconds and display the timer value.

forever code

on-chat commands Control the Timer

The following on-chat commands control the timer:

  • go — starts the timer by setting bRut to true
  • stop — stops the timer by setting bRun to false
  • reset — initializes the timer setting seconds to 0 and delta to 1 second. These same values are set when the program starts in the on-start block.
  • delta — changes the interval to the number specified in the on-chat command argument. The variable delta will be changed only when the second counter is 0; therefore, it can be changed when the program first starts or after a stop then reset commands have been executed.

go stop reset chat code

The reset actions are implemented as a function so they can be called from both the on-start block and the on-chat command reset.

on start and reset code

The on-chat command delta has one argument num1. If num1 is greater than 0, delta is set to num1; otherwise, delta is set to 1, the default. If num1 is not provided, it is 0 and the default is used.

delta code

moveMe chat Command

I love my moveMe command because it saves me all that manual moving from place to place in a Minecraft world just to find a clean place to try my program again and again and again, which is usually needed during development and testing. Notice that only X is changed each time moveMe is run; thus, the player is always moved 100 blocks to the East. The player is also placed on the ground because this is what is expected by the other sections of the program. For Handy Timer, a sunflower is placed 2 blocks east of the player as a direction guide. Sunflowers always face east. Since the timer will be printed to the East of the player, its good to know in which direction to look.
moveme basic

Is Handy Timer Accurate?

I used my phone timer to compare to Handy Timer for an estimate of accuracy. For a short time, e.g.,  5 seconds — not very accurate. For longer time periods, e.g., 30 seconds, then results are essentially the same, or at least as close as my eye balls can discern. The difference is the time it takes to perform the actions Handy Timer does at the end of each interval. With a say-block to display the time, it is more accurate than it is with a print block, which takes much longer to display the seconds.

For games, a timer similar to this would be useful for marking more-or-less regular intervals, but it will not replace a stop watch.

Handy Timer’s Future

In my next post, Using Minecraft Blocks as Buttons: Block-by-Block Tutorial Video in MakeCode for Minecraft  I will use Handy Timer to show how to use Minecraft Blocks as Buttons to control the timer. In the post after that, I will use it to explain When and How NOT to Use Player World Position in MakeCode for Minecraft.

We Will Do More Block-by-Block Tutorial Videos

The Girl argued that some of our videos be tutorials on coding MakeCode block by block so that a beginner can see exactly how and why each MakeCode block is used. This post presents our first such effort. Our prior videos have described a program by looking at the final code and describing section by section what it does and how it works. Admittedly, this may be overwhelming to a MakeCode beginner. We will do a bit of both in the future. Our tutorial coding videos will be listed under the category “Block by Block Tutorial Video.”

Get the Code

Handy Timer code is shared on Code Connection at this URL

https://makecode.com/_3C89FjDJaEbR

To get and use the code, follow these steps.

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

5 thoughts on “Handy Timer — Block-by-Block Tutorial Video in MakeCode for Minecraft

  1. Hi Grandma and The Girl:

    Tom Ball from the MakeCode team here. We are loving all your posts! One thing that might be useful for first timers is to tell them how to get the code in your examples over to the Code Connection application via copy-and-paste. Perhaps you can expand a bit in the Get the Code section (a sentence or two ought to do it)?

    Also, please note we have fixed the bug you posted about.

    Best,

    — Tom

  2. Pingback: Using Minecraft Blocks as Buttons: Block-by-Block Tutorial Video in MakeCode for Minecraft | We Code MakeCode