If you haven't used MakeCode before, then check out the following guide:
First, let's make the "treasure" code. This one just sends out a number, so it's pretty easy. Here's a link to the code:
The key block that matters is the
infrared send number block found under
This block sends out the provided number on the infrared transmitter. Here, we've just set it to 1.
The rest of the code just waits 15 seconds so the number is not transmitted constantly (this makes the game more challenging). We also turn on the NeoPixels when we transmit the number. This is because our human eyes can not see the infrared light. So the NeoPixels give us a visible indication that the number is being sent. Otherwise it would look like the Treasure was just sitting there doing nothing.
The other Treasures are the same. The only thing that changes is the number being sent out. We're going to make 3 total Treasures. You can use the code above for Treasure 1. Here are links to code ready to go for Treasures 2 and 3:
OK, this one is a little more complex - it has a lot to do. We'll go through it, but here's the link to the final code:
Even though there are a lot of blocks, it's basically just doing the same thing three times - one for each treasure. First, it sets variables (
TREASURE_1, etc.) that hold the actual treasure ID numbers 1, 2, and 3. Then, to keep track of whether a treasure has been found, we set three more variables (
found_1, etc.) to be initially false. These will become true when the treasures are found. You will see how that is done later. Finally, we just make sure all the NeoPixels are off by using the
Next, there's the
forever block. As the name implies, this runs forever - over and over again in a loop.
This is where we check to see if all of the treasures have been found. When that happens, all of the variables like
found_1 will be true. So the
if statements just checks for that using
and to tie them all together.
Once all of the variables are true, then the code inside the block runs. This plays a little "ba ding" sound and then loops a rainbow animation on the NeoPixels. The animation will run forever, so you'll need to press reset to start a new game.
Now for the most important part - the part that listens for incoming signals on the IR receiver. This is handled using the
on infrared received block which is also found under
Four our Hunter code, the entire block looks like this:
Every time the IR receiver detects a number, this code block will run. The number that the IR receiver saw is stored in
found_id. We just check that value to see if it's one of the treasure ID's, which are stored in the
TREASURE_1 variables we created in
Each time we get a match, we turn on one of the NeoPixels. This gives you a visual indication of your progress in finding the treasures. More importantly, we set the
found variable to true.