Channel your inner rock god with our plucky homage. Mark Vanstone has the code in this month’s edition of Wireframe magazine, available now.
In 2005, a new game let players live out their rock star fantasies. Having already made specialised game controllers for Konami, American firm RedOctane enlisted the help of Harmonix to help them create Guitar Hero: a game where the aim was to play along with a music track by hitting the right buttons on a guitar-shaped controller. The game was an instant hit, spawning over 20 sequels and spin-offs over the course of around five years.
The guitar controller featured five coloured buttons representing different notes, a strumming bar that had to be hit in time with the notes being played, and a whammy bar to change the pitch of the notes. The buttons corresponded with a scrolling display of coloured circles that moved down the screen as the music played. If the player ‘strummed’ the guitar at the correct time while holding down the correct coloured button, they scored points and kept the (virtual) audience entertained.
For our Pygame Zero version, we don’t have access to a custom-designed controller, so we’ll have to use a standard computer keyboard. We can set up the letters Z, X, C, V, and B as the five buttons. Then we’ll use the SPACE bar to strum the notes as they arrive at the bottom of the fretboard. We can make a list of Actors for the coloured circles which we’ll then move down the screen every time
update() is called. We have five strings numbered 0 to 4 to attach our coloured circles to, and these need to be organised so they hit the bottom target points exactly when the note plays.
We don’t want the coloured circles to get out of sync with the music, so the timing of the movement is key. We must make sure that the speed is consistent, regardless of the speed of computer or other things going on which may make the frame rate change. To do this, we need to work out the time between each frame, known as the delta time, and then move the coloured circles based on that value. If you’re using your own music for the backing track, you may need to play around with these values, but the way this sample is set up, the tuples in the
counterPoints list are the string number and then the number of seconds at which the note should be played from the beginning of the song. This setup means the player can get ready to play the note as it comes down the fretboard, press the appropriate button just before it gets to the bottom target, and if they press the SPACE bar as the coloured circle goes over the target, we fire off a ‘shine’ animation to indicate they’ve scored points. We can give them a little leeway so that they don’t have to be too precise with the SPACE bar press.
To make the music sample easy to play along with, I’ve edited a shortened version of one of my own fairly slow songs where the player plays the bassline. There are a couple of tricky bits in it, but you should be able to hit all the notes without too many attempts. You can use any MP3 or WAV file for this, just drop it in the music directory and change the line that reads
music.play_once(‘themoment’). Then all you need to do is change the timings in the
There are other things that you could add, like having error notes play if the wrong buttons are held down. There’s also that whammy bar on the original controller that may require a bit of lateral thinking to replicate on a keyboard, but we’ll leave that challenge to you.
Get your copy of Wireframe #62
You can read more features like this one in Wireframe issue 62, available directly from Raspberry Pi Press – we deliver worldwide.
And if you prefer your magazines in digital form, you can also download Wireframe issue 62 as a free PDF!