Over the next eight projects, we're going to use buttons, LEDs and a good bit of code to build and program a pattern-matching "Simon Says" game, much like the popular toy Simon®:
If you're not familiar with Simon®, let me describe how our pattern-matching game will work:
The game will start by flashing one of four different color LEDs at random and will play a tone specific to that LED. You (the player) will then have a short amount of time to press the button associated with the LED that flashed. If you do that successfully, the game will repeat the first LED flash and then add a second LED flash at random. Again, you'll need to remember the pattern and press the correct buttons to replicate the pattern. Each time you successfully enter the correct sequence, the game will replay the previous pattern and add a random new LED flash at the end. This will continue until you incorrectly repeat the sequence — of if you take too long between button presses — at which point the game ends and the entire sequence is repeated back to you one last time.
Since this is a bit longer and more complex project than we've done so far, it's important to spend some time planning what the code will do and in what order. To do this, we'll create an outline for Simon Says using a common programming technique called pseudo-code.
Writing pseudo-code before you write software is similar to writing an outline before you write a paper or an article. Basically, you create an outline for your code using a combination of regular English mixed with some bits of programming code. This allows you (or others) to read the order of the code and determine if what you're getting ready to write is logical and will accomplish what you want and expect.
Let's use the project to show an example of how we'd write pseudo-code. In that project, we had a bunch of buttons, and we wanted to print when the buttons were pressed.
Here is what our pseudo-code might look like:
Create a list of Buttons Repeat: For each button in the list of buttons: If the button is pressed: Print which button was pressed Pause
You notice that each line is easily readable as English — with some indenting to help the readability — and overall, you now have an outline that you can use as a template to create your code.
Now, in reality, you might not need to use pseudo-code for such a simple program. But, for more complex software, writing the pseudo-code before you start writing the actual code can make the entire process much easier.
Let's talk about what the pseudo-code for our Simon Says game will look like. An effective way to outline a programming project is to start with big ideas and then break those ideas down into smaller and smaller pieces until the pieces are small enough that we're comfortable starting to write our code.
For example, this is what the very high level outline for our Simon Says game might look like:
Import Modules and Initialize Game Add a new random LED to sequence Flash the current LED sequence Allow player to repeat sequence one button at a time Get the user's button presses, one-by-one If user enters incorrect button, end game If user enters correct button, flash LED and play tone If user enters the whole sequence, go back & add another random LED If user enters the wrong sequence, play an error sequence
That's basically all our Simon Says game needs to do. But, now it's time to
start thinking about how this outline would actually look when written with
code. You'll notice that the second to last line indicates that — for as long
as the user does something correctly — we go back to the top of the code.
Does this sound like a concept we've already discussed? A while
loop, perhaps?
Also, you'll notice that in the middle of the code, we're checking for the
sequence of button presses from the user — one-by-one. This sequence of
getting input and checking it would likely be a loop as well — in this case,
probably a for
loop.
Let's rewrite our pseudo-code above, but this time, let's add in a couple loops to make it more clear how the code will work:
Import Modules and Initialize Game while (user enters the sequence correctly): Add another random LED to sequence Play LED sequence for (each of the buttons in the sequence): Get the user's button press If user enters incorrect button: End game Otherwise (user entered correct button), so flash LED and play tone If the user enters the wrong sequence, play an error sequence
The pseudo-code above is easy to follow, but by adding in the two loops, we start to see the format for our program.
We could drill down into each piece of the pseudo-code further — and in some future projects we'll be much more detailed — but for our Simon Says game, we think this is enough detail to get started. As we build up the code over the next many projects, we'll use the outline above to organize and ensure that we're doing everything that needs to be done to get the game working properly.