Red Light Green Light With Micro:Bits
by 0637402 in Circuits > Computers
712 Views, 0 Favorites, 0 Comments
Red Light Green Light With Micro:Bits
With the release of the hit Netflix show ‘Squid Game’ on September 17th, the world went crazy with ‘Squid Game’ hype. When this assignment was initially given to me, I decided to make a game from the show as it was popular. While there are many games within the show, many of them would be very difficult to port into a Micro:Bit computer. I realised that the easiest game that fit the assignment using networking was Red Light, Green Light. A popular children's game, Red Light, Green Light is a variation on a game by the name of ‘Statues’.
The game starts with players selecting someone to be ‘it’. The person who is ‘it’ stands at the end of the playing field. The objective of the game is to reach the person who is deemed ‘it’ without being caught by the person who is ‘it’. When the person calls out “Green Light” and turns around, players are allowed to move towards ‘it’. When the person calls out “Red Light” and turns back to face the players, players must freeze and not move for the duration of the “Red Light” call. If players are spotted moving, they will be sent back to the beginning of the playing field to try again. A winner is declared when they reach ‘it’. In some variations, the winner may become ‘it’ and the game will reset.
Basic Coding
I started this coding process by figuring out that I would need to use multiple Micro:Bits to make this work. They needed to connect to each other, and the way that Micro:Bits are able to connect to one another is a use of radio frequencies. I realised that I would need to have 2 different programs for this project to work, so I started with the foundations for both. The first step was to start the programs. I started both the host and player programs very similarly, with only 1 difference.
I started both programs with a “on start” block, using block programming. Within, I created 3 variables, “Game State”, “Red Light” and “Green Light”. I defined “Game State” as 0, “Red Light” as 1 and “Green Light” as 2. I then finished the block with a radio command to set the group to “1”. Within the player program, I also created another variable named “Alive”. I defined “Alive” as 10. While this was not used for most of the program, it was still a reliable way to make sure that players were eliminated from the game until the next round began.
After this starting code, the rest is different from one another. As such, I will separate them into two different sections from here.
Host Coding
After the starting code with the host, I decided to rely on having a player as the Host. As such, I decided to use the built-in buttons for the programs. However, I worked on a variation which used random number generators to decide when the light would change colour, but I decided that it would not be as enjoyable with it being entirely random.
Once I had finished the starting programming, I placed a “forever” block into my workspace. Within that block, I put a radio block which would broadcast the value of the variable “Game State”.
After this, I programmed the buttons to change the value of “Game State” to match either “Green Light” or “Red Light”s values. Afterwards it would then display either a tick or a cross graphic to indicate which status that the light was currently set to.
This was all of the programming that the Host Micro:Bit required to make it function as expected. It was simple, but it also was effective.
Player Coding
After completing the Host code, the player code was more complicated. I started by adding a block which receives radio signals and had it set “Game State” to the received number.
After this small piece of code, all that was required was to simply get the display to work, and have a way to register the player being eliminated. This however, proved to be more complicated than any of the code which I used previously.
I started by adding a “forever” block and then putting three “if <> then” logic blocks into it, two being nested within the first. In the first layer of the logic blocks, I added a comparison logic block which checked if the variable “Alive” still was valued as 10. If “Alive” still equaled 10, then the code would continue to the next layer. The second layer contains two “if <> then” logic blocks stacked on top of each other. Both of these logic blocks contain a comparison logic block which compares the values of “Game State” and “Red Light” or “Green Light”. After one has been compared to the value of “Game State”, the code will continue. If there is a difference, it will compare “Game State” to the other available variable. All either of these options does is set a graphic on the LEDs on the front of the Micro:Bit. A tick for “Green Light” and a cross for “Red Light”.
The final piece of code in the Player code, is to check whether the player holding the Player Micro:Bit is moving. I did this by using the in-built accelerometer. I once again started by using a “forever” block and nesting “if <> then” logic blocks. The first layer of logic blocks checks if the current value of “Game State” is equal to the value of “Red Light”. I then created another variable named “Microbit Movement”. I defined the value of “Microbit Movement” using a block from the ‘Input’ tab and two from the ‘Math’ tab. A ‘Math’ block was nested within the definition block, defining “Microbit Movement” as the “Absolute of x”. The ‘Input’ block was nested within the second ‘Math’ block was set to “Strength of Acceleration (mg)”. From the “Strength of Acceleration” I subtracted 1024 from the number. This is because the accelerometer doesn’t account for the pressure of gravity. I found the number of 1024 by looking for sources which showed the effect of gravity on the Micro:Bit accelerometer, as I was unable to measure it myself. (Source: Here) After defining the variable, I had the second “if <> then” logic block. I added another logic comparison block within, and compared “Microbit Movement” to be greater than 100. This was to account for movement of the Micro:Bit. If higher than 100, it is fairly certain that the device is moving. Within this logic block, I attached a block which set the LEDs to a custom skull graphic, and set the variable “Alive” to the value of 11. Now that the variable of “Alive” was changed, the rest of the code won’t work until the game restarts as that player is out.
Conclusion
This project was an experiment from me as I didn’t know anything about coding Micro:Bits before it. The player code is able to be replicated endlessly across many Micro:Bits as it doesn’t have a direct connection to the host Micro:Bit. The accelerometer elimination method may be a bit sensitive as I was unable to test it, but it should be good enough to be accurate most of the time. As you know how to make it, you know how to change it now.