Assignment 3.3: Key Press Madness - Audio
Learn to use keys in your programGoals
In this assignment, we will practice the following computer science concepts
- Using conditional statements
- Using logical operations
Computer Science Principles Curriculum
- Big Idea: Creativity: EU 1.2, LO 1.2.3, EK 1.2.3A
- Big Idea: Algorithms: EU 4.1, LO 4.1.1, EK 4.1.1B
- Big Idea: Programming: EU 5.2, LO 5.2.1, EK 5.2.1A, EK 5.2.1B, EK 5.2.1C, EK 5.2.1D, EK 5.2.1E, EK 5.2.1F
Common Core Standards
- English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.9-10.1, CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.8, CCSS.ELA-Literacy.RST.9-10.9
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8, CCSS.Math.Content.HSF.IF.A.1
Overview
This lab demonstrates how to map certain sounds to specific keys on the keyboard. When that key is pressed the program will play the designated sound.
The Quorum Games
and Audio
libraries will be used, along with the InputMonitor
and KeyboardEvent
libraries to handle keyboard input during the program.
You will write a program that will play certain sounds upon a key being pressed.
Download this zip file for a template to help you get started.
You may also use the IDE below for this assignment. The sounds we recommend using for this assignment are:
- media/Bing.ogg
- media/Boom.ogg
- media/Chime.ogg
- media/Clang.ogg
- media/Firework.ogg
- media/Fwip.ogg
- media/longSong.ogg
- media/Modem.ogg
- media/Motorcycle.ogg
- media/Robot.ogg
- media/song.ogg
Goal 1: Using Conditional Statements
Open the provided template. In the main.quorum file code needs to be written in two places. The first place is in an action called CreateGame
on lines 38 through 39. In this action, you set up your Audio
objects. The second place you write code is in the action called Update(number seconds)
on lines 48 through 49. In this action, you write the code that plays the Audio
objects when keys are pressed. Three Audio
objects have already been declared for you. A folder called media has been provided to you that includes sounds for you to use for this program.
Activity: Load sounds to three Audio
objects and enable looping on the three Audio
objects.
Go through the media folder and choose three sounds you want to play during your program.
Once you have chosen your sounds, you need to load them to your Audio
objects.
You may want to enable looping on these sounds once you have loaded them so that the sounds play over and over again while a key is pressed.
Choose the three keys you want to play sounds when pressed and choose which Audio
object to play when that key is pressed. In the Update(number seconds)
action beginning on line 27, check for these keys being pressed.
If they are, play the Audio
object for that key.
Example: Play a sound when a key is pressed.
// this line checks if the "A" key is pressed.
if inputMonitor:IsKeyPressed(keys:A)
// this line plays the sound we chose to associate with the A key
sound1:Play()
end
Activity: Choose 2 more keys and play the other two Audio
objects when those two keys are pressed.
When you run your program and press those keys, the sounds you associated with those keys should play. You can keep a key pressed down to play a sound for a longer period of time (due to the loops you created earlier) or just tap the key to play the sound for a short period of time. You will only hear the sound for the period you press the key. If you tap it you will barely hear a sound! Additionally, you may press more than one key at once to play multiple sounds.
Compile and run your program (after fixing any errors). You may notice that the sounds do not sound right if you hold a key down. Why might this be?
Goal 2: Using Logical Operations
Recall that the Update(number seconds)
action is called on every frame of your program. Also recall that the Play
action plays the audio file from the beginning. So each time the Update(number seconds)
action is called, any audio object currently playing will be played again from the beginning if the appropriate key is still pressed down.
How might you fix this?
One easy way to fix this is to use the IsPlaying
action. IsPlaying
returns a boolean
— true
if the audio is playing and false
if not.
We can use this action to our advantage because we only want to start playing our Audio objects if the appropriate key is pressed AND if the Audio
object is not already playing.
Example: Only start playing the sound if the key is pressed and it is not already playing.
// this line checks if the "A" key is pressed and that the sound is not already
// playing
if inputMonitor:IsKeyPressed(keys:A) and not sound1::IsPlaying()
// this line plays the sound we chose to associate with the A key
sound1:Play()
end
Activity: Add the additional condition for the other Audio
objects.
For extra fun, instantiate more Audio
objects (near the top of the file, above the CreateGame
action), load files to them, and play them on certain key presses!
Sample Output
The program should play at least 3 different sounds upon three different key presses. The sounds should continue to play smoothly while a key is held down.
Next Tutorial
In the next tutorial, we will discuss Assignment 3.4, which describes how work Key Press Madness - Visual in Quorum..