Monday, 22 June 2020

Chuck a Duck - The Lesson That Never Was

Now that we'd been introduced to radio communications, the next step was to use this feature to write a game. 

I had seen the 'group teleporting duck' game on the micro bit projects page at https://microbit.org/projects/make-it-code-it/group-teleporting-duck/ and thought that would be a good basis for a game. 

This project would be a little more complicated that what we've been doing to date, so I decided to write this myself first before going through the process in a lesson.   

The idea of the game is: 

  • Each player has their own micro:bit. At startup, they can press the 'A' button to cycle through a list of id's (1-4 in a 4 player game). 
  • Once everyone has a unique number, one player starts the game by pressing the 'A' and 'B' buttons together. This transports the duck to a random micro:bit. 
  • The player that receives the duck has to shake the micro:bit to 'chuck' it to another random micro:bit. The game continues for 20 seconds and the player left with the duck at the end will lose the game. 

Allowing players to select their own id means that the same code can be loaded onto each micro:bit. The only things that are hard coded are the game duration and the number of players.

The game timing was a bit of an issue. There were two approaches - I could have each micro:bit count down the time independently - or I could send the time remaining around with the duck itself via the radio message to try and keep everyone in sync. 

In the end I went with the simpler approach of having the micro:bits count the time independently, and they were so close in terms of being synced by the initial start message that it didn't affect the game-play. In theory there are a couple of timing issues at the end of the game - for example if you throw the duck just as the timer runs out - but they didn't materialise in practice. 

After writing the game, I decided that I had really made it far too complicated to use as a teaching project. However, as a game it was quite fun. Four of us loaded the code, and played the game. So I hope it showed the boys that a fun game could be written and played on their micro:bits.

I think I'll go back to this later on and go through it with them - maybe just going into chucking the duck.  

So - what did the code look like? 

Setup

During setup we initialise the game play variables, and allow each player to select their id using the 'A' button. 



Starting the Game

To start the game, one player needs to press the 'A' + 'B' buttons. This calls the 'startGame' function to start the timer, and reset the game flags and leds. It also sends a '0' to all the other micro:bits to tell them to also call the 'startGame' function. We then call the 'sendDuck' function. This picks a random id, making sure not to pick our own id, and sends that number to all the other micro:bits. 
The radio received event handler takes the number, and if it matches our id, then we set the flag to indicate we now have the duck, and show the duck icon on the display. 


Chucking the Duck

If a player has the duck, then they can 'shake' the micro:bit to get rid of it and send it to another random micro:bit. This uses the same 'sendDuck' function we used when starting the game. 



The Timing Loop

The forever loop is responsible for handling the game timer and working out whether we have won or lost at the end of the game. 

If the game is in play (gameStarted is true) then we first work out how much time is remaining. 
The Leds at the bottom of the display (which are not used by the duck icon) are used to display an indication of how much time is remaining. 

If the time has run out and we currently have the duck then we've lost and we display a cross icon. If we don't have the duck then we're one of the winners and a tick icon is displayed. The gameStarted flag is set to false which means a new game can now be started by one of the players pressing 'A' + 'B'. 



So that was the game. The only issue we had was that Max's micro:bit's shake function stopped working, so whenever he got the duck he couldn't get rid of it and lost. Resetting the micro:bit fixed this - so not sure what was going on there. 

I want to move onto the accelerometer next because I think that is one of the most useful and exciting built in features of the micro:bit. 






No comments:

Post a Comment