Wednesday, April 21, 2021
HomePc and MobilesMicrosoft MakeCode for Minecraft: Learn JavaScript for Free While Having Fun

Microsoft MakeCode for Minecraft: Learn JavaScript for Free While Having Fun

JavaScript (JS, ECMAScript, ES) is a versatile programming language that supplies thousands of high-paying jobs worldwide… so it may come as a surprise that you can learn it by playing Minecraft.

Microsoft MakeCode for Minecraft seeks to bring the world of software development to anyone and everyone with fun, free projects that bring your code to life before your very eyes in the world of blocks.

What Is Microsoft MakeCode?Microsoft MakeCode Minecraft logo

Microsoft MakeCode is a learning platform dedicated to making education in computer science interesting and accessible. It’s free and open-source with a variety of methods for learning, building, and playing.

If you’ve played Minecraft before, this option may catch your eye. You can learn Python and JavaScript in this section, or use the block editor to work on programming logic without worrying about code. We’ll be using JS in this article.

This article will introduce you to MakeCode for Minecraft‘s features by breaking down the initial project, Chicken Rain, as well as introducing a few custom scripts—the first will allow you to toggle common settings (like weather or time), and the second will allow you to create and play a custom mini-game.

Note: In order to take advantage of the full functionality of MakeCode for Minecraft, you’ll need a Minecraft Bedrock Edition (Minecraft for Windows 10) or Minecraft Education Edition account.

Setup: Code Connection and a New World

To sync your code with your Minecraft client, download and install Microsoft Code Connection.

Setting up code connection

Choose your edition of Minecraft, then follow the steps laid out on the Minecraft for Windows 10 Setup for MakeCode page to install and run Code Connection.

With Code Connection running, start Minecraft and create a new world (Play > Worlds > Create New > Create New World > Create) with cheats enabled. Activate Cheats must be turned on to access the full functionality of MakeCode.

Start your new world and enter the command displayed by Code Connection into the chat to sync your text editor and game client.

Related: The Ultimate Minecraft Commands Cheat Sheet

Now that Code Connection is set up and a new world is waiting for you, you’re ready to jump in and start coding!

Minecraft MakeCode: Chicken Rain

MakeCode for Minecraft Chicken Rain prompt

The first available project is Chicken Rain. This five-step tutorial will help you familiarize yourself with the MakeCode UI (like the text editor, menu, Play button, and helpful documentation.)

Related: Want to Learn Programming? Key Terms You Should Know

We’ll also take a rudimentary look at ‘variables’ and ‘functions’, two key building blocks of computer science. You’ll also have your very own flood of floating chickens when you’re finished writing the script.

This may not sound like much if your sights are set on creating full-fledged mods, but seeing your code come to life is an incredible feeling that gets many engineers hooked for life. This is your first step!

Minecraft MakeCode Chicken Rain step 2

Examine the prompt before copying the code into your text editor. This is a good example to demonstrate how you should be thinking like a computer scientist.

Note: Don’t understand programmer’s lingo? Here’s a JavaScript cheat sheet.

  1. First, we call an object’s function with mobs.spawn(). This is how we specify what we want the computer to do. We want Minecraft to spawn a mob.
  2. Great, but what kind of mob? Here we specify a variable—CHICKEN—to tell the computer which mob we want to spawn.
  3. mobs.spawn() takes in two parameters: mob type and position. We still need to tell the computer where we want the mob to spawn. We do this with pos(0, 10, 0) where (0, 10, 0) is the XYZ coordinates with respect to your Minecraft character.
mobs.spawn(CHICKEN, pos(0, 10, 0))
MakeCode play button

Click the Play button to run your code. Switch back to Minecraft and look up! If everything went smoothly, you’ve spawned a feathered friend.

What if we want more chickens (and honestly, who doesn’t?) Should we just copy and paste this code a few times then hit run again? Well, you can, but you shouldn’t—a good mantra to learn early in coding is “DRY” (Don’t Repeat Yourself.) This will make you a more efficient developer.

To run the same code more than once, we use a loop.

MakeCode for Minecraft step 4

A “for” loop will run the code we put inside it for as many iterations as we specify—in this case 100.

  1. Begin with for() {}. The curly braces are used to define the scope of this loop—any code inside the braces will be run multiple times.
  2. We need to tell the computer how many times this code should run. Inside the parenthesis, create a variable using let and set that variable to 0. Then we specify how many times this loop should run by including i<100. Finally, we specify what should happen at the end of each iteration—our variable’s value will be increased by one (i++).
  3. Paste the previous step’s code into the curly braces. Be sure to indent as indicated.
for (let i=0; i<100; i++) {
mobs.spawn(CHICKEN, pos(0, 10, 0))

Click the Play button and swap back to Minecraft. Chicken Rain!

MakeCode for Minecraft: More Fun with JavaScript

With your first lines of JavaScript under your belt, you can access any number of other MakeCode tutorials like Mega Jump or Save the Cake, or create a blank document and let your creativity flow.

Let’s take a look at some custom scripts to work on our JS skills.

Related: How to Declare Variables in JavaScript

Controlling Time and Other Settings

Sleep in too late and miss tending to your farm? Want to set the clocks back? And why is it always raining? Don’t worry, programming to the rescue!

player.onItemInteracted(CLOCK, function() {
} else {

This block of code introduces the if else statement, a staple of computer science logic.

  1. Call the “player” object’s function that runs when you interact with a certain item, player.onItemInteracted(). Specify the item you want to use, in this case a CLOCK, and set up the function that will run when you interact with the item function() {}.
  2. Include an if else statement if () {} else {} to act as an on/off switch. Fill in the parameters if(gameplay.isDayLightTime(Day)) to check what time it is in Minecraft.
  3. Include the code you want to run inside of the if curly braces {gameplay.timeSet(DayTime.Night)}. So far, your code checks if it’s Day, then sets it to Night if it is.
  4. Write the code in your else section {gameplay.timeSet(Daytime.Day)}. Let’s say your if statement isn’t fulfilled (it’s currently Night time in-game)—your else code will run.

Click the Play button, then swap to Minecraft and enter /gamemode 1 into the chat; open your inventory and spawn in a clock. Right-click with the clock in your hand a few times—if you wrote the code correctly, your world will be swapping between sun and moon.

Minecraft MakeCode menu

The possibilities of this bit of logic are endless. Use the menu on the left to view all sorts of commands and variables to play with. Get your hands dirty; find out what works and what doesn’t (and what seemingly should work but still doesn’t.) This is what it means to explore the world of computer science.

MakeCode for Minecraft Mini-game: Bat Hunter

player.onChat("batHunt", function () {
for (let i=0; i<32; i++) {
mobs.spawn(BAT, randpos(
pos(-5, 0, -8),
pos(5, 0, 5)
let points = 0
let addPoints = function () {
points += 1
player.say(`2 Points! Total: ${points}`)
let clearPoints = function () {
points = 0
player.say("Points Cleared.")
mobs.onMobKilled(BAT, addPoints)
player.onChat("clearPoints", clearPoints)

This may look like quite the jump from the previous projects, but don’t worry; you already know most of the logic that goes into this mini-game. All you have to do is keep an open mind and fill in the blanks.

  1. We’re calling a player.onChat() function as the setup. This will set your gamemode to Survival and your game difficulty to Normal, then supply you with a Bow and 64 arrows.
  2. Next, we set up a for loop with a mobs.spawn() function call to spawn in 32 bats. We use randpos() to specify the range of locations the bats are able to spawn in. This will cause them to swarm around you unpredictably.
  3. Now we set up the points tracking algorithm. Initialize a points variable with a value of 0. Now create a function that tracks 2 points per bat takedown. We use a template literal to combine string text (“2 points!” Total: ) with a variable’s value (${points}).
  4. Create a similar function as in step 3 to clear your point total.
  5. Now create the instances in which these functions should run. We want addPoints to run when a bat is killed, so we use mobs.onMobKilled(BAT, addPoints). We want clearPoints to run when you type “clearPoints” in the chat.

Click Play and get as many points as you can! I recommend playing inside a large room (maybe throw in some obstacles for bats to get behind) because playing outside makes it all too easy for the bats to fly off into the distance.

As with the other scripts in this tutorial, and all other code you come across in your computer science journey, this code is simply a base for you to add your own flair. You could implement a timer to add elements of urgency, or a scoreboard to track your previous games; what you build is only limited by your own creativity.

MakeCode for Minecraft Agent

MakeCode for Minecraft Agent tutorials

The Agent is a nifty NPC that lets you test out multiplayer interactions in your single-player game, automate tasks, and much more.

MakeCode provides guided tutorials for Agent Checkers, navigating a maze with your Agent, and it also has a plethora of Agent-related source code that you can modify as you please.

Take a Deeper Dive Into JavaScript

We hope MakeCode for Minecraft piqued your coding interests! We stand behind its mission to create a new generation of programmers.

While you practiced some vital JavaScript methods, we merely examined the tip of the JavaScript iceberg in this article. If you want to learn JavaScript to be job-ready, you should learn how JS interacts with other web technologies like HTML and CSS. You’d be wise to study up on common data structures and algorithms as well.

What Is JavaScript and How Does It Work?

If you’re learning web development, here’s what you need to know about JavaScript and how it works with HTML and CSS.

Read Next

About The Author

Source link

Leave a Reply

- Advertisment -

Most Popular

Recent Comments

%d bloggers like this: