If you haven't used MakeCode before, then check out the following guide:
For the MakeCode version, there are three separate programs. You choose what you want to be by loading the appropriate version. We'll go over each below, starting with the Zombie and Healer, since they are very similar and the most simple.
There are only thee main chunks. The first is the on start
block, which just sets the initial values for some variables and turns all the NeoPixels to Zombie Red.
The Zombie needs to send out the Zombie code every now and then to try and attack the Humans. This is done with a forever
loop. Pretty simple - it sends out the code and then sleeps for the amount of time set by ZOMBIE_TIME
. Then does it again - over and over, forever.
The other forever
loop is used to create the pulsing effect on the NeoPixels. This is done by changing the value of brightness up and down over and over again.
And that's it. Now let's look at the Healer.
The Healer is pretty much the same thing as the Zombie. It is simply altered to send out a different IR code and at a different rate, have different color NeoPixels, and pulse at a slower rate.
Now for those Humans.
Oof. This one has a lot going on. It doesn't even fit on one screen! But hey, humans are complex. We can still go through it chunk by chunk.
It all starts with the familiar on start
, which again just initializes a bunch of variables to be used by the program.
With the Zombie and Healer, we were transmitting IR signals. For the Human, we need to receive those signals and process them. That is done using the on infrared received
block. The value of received_code
is checked and the value of health_counter
is either decreased or increased, depending on if the code is from a Zombie or a Healer.
The value of health_counter
is then checked. We limit it to a max of 10. But more importantly, we check to see if it has reached 0. If so, the Human is turned into a Zombie.
The rest of the code is split amongst several forever
loops. The first one checks to see if the CPX is still Human. If so, it updates the NeoPixels to indicate the current amount of health left.
The next forever
loop kicks in when the CPX becomes a Zombie. It should look similar to the one you saw in the Zombie program. This is what sends out the Zombie code on the IR transmitter. The difference here is we add a check to only do this when the board actually is a Zombie. Therefore, this does not happen when the board is still a Human.
The last forever
loop is what pulses the NeoPixels when the board becomes a Zombie. It is also just the same thing from the Zombie code, but again with a check to only run when the board actually becomes a Zombie.
And that's it. It looks like a lot, and it kinda is. Hopefully it helps to try and just think in terms of the large chunks described above. Like the big chunk that "receives the IR signal" and the big chunk that "pulses the NeoPixels". Then it becomes easier to see how these big chunks fit together to make the program work. After that, you can look at the chunks in more detail to try and understand how they are actually achieving that.
OK. Enough discussion. Gather up some friends and some Circuit Playground Express boards. Load up the programs. And have fun.
rrrrrrrrrrrr.....brains.......
Text editor powered by tinymce.