Binary Snap Part Two: Game Time

If you haven’t already taken a look at part one, then it would be worth starting there for more information about this project.

The next step in our Binary Snap Game journey is to connect all of the other elements we need to play the game, which include the Smart Crumb: Digits and a Push Switch.

Now we need to work on the premise of our game – what do we want it to achieve? We want a number in binary to appear on our Sparkle Baton and another to appear on the Smart Crumb:Digits for a limited amount of time. If they are the same we need to press the button to score a point. If not, we wait until they change. Let’s start by getting the same random number to display on both components.

We then have a few other additions to make to complete the game:

  • Generate a random number for each output
  • Have a button press check whether the outputs are same
  • Have a timer for each round, which then checks whether the outputs are different
  • Have the game reset

We start with a timing loop, which will end either when the button is pressed, or the round is over.

We now need to check the outcome of the game. If Timer is less than 40, it means the button must have been pressed, so if that is true and both of our random numbers are the same, then we win. We show this by turning on all of the Sparkles green. On the other hand, If Timer equals 40, the timer has elapsed, so if that is true and both of our random numbers are not the same, then we also win and the Sparkles light up green. Finally, if we get this far through our conditions it means either we incorrectly pressed the button or we missed a matching pair of numbers and so we lose this round and the Sparkles turn red,

If we now add this to the previous code along with a pause to give us a chance to see the round results (wait 2 seconds) and nest the whole lot within a loop, we should have the basis of our Binary Snap game!

One thing you may notice before or after you’ve played are the odds of getting a matching pair. It is 1 in 225, which is fairly unlikely (although not impossible). Let’s add a small bias into our program to make every 1 in 10 match. For clarity we’ve created a new variable called Bias, which we set to a random number between 1 and 10. If that number is 5, then our binary number and normal number become the same.

With the bias put into our code we now have an awesome game! There are plenty of other ways you could achieve the same results or even take this further. Adding in larger numbers. How about keeping score? Or what about increasing the speed between each round after every successful answer!?

If you have a go at this project, or any other, we’d love to see! Get in contact with us via emailFacebookTwitter or our Forum, and we may feature your work!

Comments are closed.