AP CSP CodeX - Mission 13: Sounds Fun

Mission 13 Lesson Plan

Mission 13: Sounds Fun

Students build a race day controller with a menu, sound effects and music on the CodeX, using a graphical user interface.

⏰ 90 min 🎯 Grades 9-12 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

During this mission, students create a user-friendly graphical user interface (GUI) for the CodeX so it can act as a race day controller. They draw a four-item menu, move a selection rectangle with the buttons, and use the soundlib module to play music and sound effects. Along the way they work with local and global variables, for loops, blocking and non-blocking functions, and the not operator. This mission takes about two class periods, and works well split into two days.

🎯 Mission Goal: Students will create a user-friendly graphical user interface for CodeX so that it can act as a race day controller.

🎯

Learning Targets

  • I can explain what a GUI is used for in a computer application.
  • I can explain the difference between a local and global variable.
  • I can use a for loop to iterate over a list.
  • I can distinguish between a blocking and non-blocking function.
  • I can use the not logical operator accurately in a program.
💡

Key Concepts

  • Variables defined inside of a function are local variables; variables defined outside of functions are global variables.
  • The soundlib Python module has functions to create different types of tones, as well as playing recorded samples and MP3s.
  • The for loop is made for looping across a range of numbers, or iterating over lists.
  • Blocking functions block your code from continuing until they finish.
  • Non-blocking functions allow code to continue to run without waiting for sound to finish.
  • Use the not logical operator to flip the state of a global variable.
  • A screen is made up of pixels, each with a location (x, y). The x values are columns going across, and the y values are rows going down.
  • The upper left corner of the screen is (0, 0) and the lower right corner is (239, 239).
☑️

Success Criteria

  • Display a GUI with four menu items
  • Use a rectangle to show which menu item is selected
  • Scroll the selection rectangle up and down to the different menu items
  • Select a menu item using BTN A
  • Play music in the background
  • Play music for starting the race
  • Play music for finishing the race
  • Play music as a warning sound
📁

Digital Resources

🧰

Classroom Materials

  • ▸CodeX
  • ▸USB-C cable
  • ▸Computer
🌍

Real-World Applications

🖥️A graphical user interface is basically a visual way to interact with a computer or digital device as an alternative to typing out text commands. Common applications of GUIs include:
  • Operating systems, like Windows and macOS
  • Mobile and web apps
  • Web browsers, like Chrome and Safari
  • Internet of Things, like smart home devices
  • Medical equipment
🚀

Extensions & Cross-Curricular

ExtensionLight up the pixels during the program, such as a color for each race event. Students can use their creativity to add functionality.
ExtensionAdd a fifth menu item, and write the sound or message that goes with it.
ExtensionAdd a "kill" switch and change the color of the selection rectangle. Then discuss how color choices make a program more accessible.
MathMenu items sit at set y coordinates on the screen. Work out the y values for menu items spaced 40 pixels apart on a 240 pixel screen.
ScienceResearch how sound pitch relates to frequency in hertz. Why does 880 Hz sound higher than 440 Hz?
Lang ArtsWrite a one page user guide for the race controller, so a new user knows what each menu item does.
🔤

Vocabulary

▾
User Interface (UI) - The area where a person interacts with a physical device, often through a screen.
Bitmap - Graphics bits, drawing images and text. A bitmap is an object that can hold a 2D image of a given width and height; a list of pixel RGB values.
Local variable - A variable that is "private" to a function. It only exists while the function is running, and is separate from any other variable outside the function.
Global variable - A variable defined outside of a function. It exists the entire life of the program and can be accessed and used inside a function.
Initialization - Set the initial or first value of a global variable when the program starts and the variable is defined. Also, set the screen to its beginning look.
Soundlib module - Functions for creating music and sound effects, including different types of tones.
For loop - Looping across a range of numbers, or iterating over a list. A for loop uses a built-in range function to specify the sequence of numbers you need. It is simpler than a while loop because you don't need to initialize and update a loop variable; it happens automatically.
Blocking function - Functions that block your code from continuing until they are finished. The code has to wait while a song plays, for example.
Non-blocking function - A function that doesn't make your code wait for the function to finish. For example, other lines of code can execute while a song is playing.
Toggle - Flip the state of a variable (True to False or False to True) that is used to either do or not do something.
Nested for loop - A for loop with a for loop inside, or nested. The second loop is nested inside the first one. Every time the outer loop executes, the inner loop completes all its cycles.
🐍

New Python Code

▾
display.draw_rect(0, 80, 240, 40, GRAY)Draw a rectangle from one point to the other.
display.draw_text("MUSIC", x=20, y=90, color=WHITE, scale=3)Draw text, which is different from display.print(). The keyword parameters are optional and can be listed in any order.
max(menu_index-1, 0) menu_index = min(menu_index+1, 3)Returns the largest or smallest item of the arguments. The functions max() and min() are usually part of an assignment statement.
from soundlib import *Import the soundlib module.
trumpet = soundmaker.get_tone('trumpet')Get a tone from the soundmaker. You can have up to 16 tones playing at the same time.
siren.set_pitch(440) siren.play()Set the pitch of a tone and then play the tone. In this example, the tone is siren.
sleep(1.5) siren.stop()Stop playing a tone after a set amount of time.
trumpet.set_pitch(440) for i in range(4): trumpet.play() sleep(0.1) trumpet.stop() sleep(0.1)A for loop used for playing a note multiple times. i is the control variable, incrementing from 0 to 3, so the note plays 4 times.
global is_playing is_playing = not is_playingUsing the not operator to toggle a Boolean variable. It flips the state from True to False or False to True.
race_music = soundmaker.get_mp3('sounds/funk.mp3') race_music = soundmaker.get_mp3('sounds/funk.mp3', play=False)Non-blocking function for playing audio mp3s. You can add a parameter play so the music doesn't start automatically.
trumpet.play() for freq1 in range(500, 1500, 100): for freq2 in range(freq1, freq1+1000, 100): trumpet.set_pitch(freq2) sleep(0.023)Use a for loop while playing a sound. It uses a nested for loop. The inner loop takes the current frequency and increases it for a sweeping sound. This is repeated 10 times (outer loop) by changing the frequency by 100 each loop.
siren.set_pitch(440) siren.play() siren.glide(880, 1.5)Use the glide() function from soundlib. The function takes two arguments: the new or ending pitch and the duration. It is a non-blocking function.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-01 3A-CS-03 3A-DA-11 3A-AP-13 3A-AP-14 3A-AP-15 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21

CSTA Standards - Grades 11-12

3B-CS-02 3B-AP-10 3B-AP-14 3B-AP-16 3B-AP-22 3B-AP-23

AP CSP Framework

AAP-1.C - Represent a list or string using a variable.
AAP-2.H - Write conditional statements.
AAP-2.K - Write iteration statements.
AAP-2.O - Write iteration statements to traverse a list.
AAP-3.D - Select appropriate libraries or existing code segments to use in creating new programs.
Computational Thinking Practice 1.B - Determine and design an appropriate method or approach to achieve the purpose.
Computational Thinking Practice 3.B - Use abstraction to manage complexity in a program.
📝
Preparing for the Lesson
  • The assignment is best completed digitally. Prepare the assignment to give through your LMS.
  • The completed program uses a mix of global and local variables. It helps to have completed the "Functions and Global Variables" assignment from the Create PT Prep unit. Have the slides from that lesson ready if students need a review.
  • Plan for two class periods. If you split the mission, stop after Objective 6 on day one and have the Mission 13 Obj. 1-6 Kahoot! Review ready.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

🧑‍🏫
Teacher Notes
  • Encourage the students to do as much code on their own as they can, and use CodeTrek to check their work, or as a hint when needed.
  • The assignment adds an extra step to complete after Objective 11, and a challenge.
  • You can divide the mission into two days. On the first day, stop after Objective 6 and use the Mission 13 Obj. 1-6 Kahoot! Review. The second day will complete Objectives 7-11 and the final steps. Use the Mission 13 Obj. 7-11 Kahoot! Review. If time permits, include the challenge as well.
  • Refer to the lesson plans in the Python with CodeX Mission Pack Curriculum for additional challenges, cross-curricular suggestions, and additional information.
🗺️

Lesson Outline

This outline gives the narration for one way to present the lesson.

🗣️Warm-up

In this mission you will use a list. What do you remember about lists? Have a brief discussion. This mission will also use a global variable that is changed in a function. What do you remember about local and global variables? Have a brief discussion. Use the Functions and Global Variables slides if needed.

💻Activity - Mission 13 Objectives 1-6

Randomly group students into pairs for pair programming (or they can work individually).

Students log in to one computer. Two computers can be used if they want to have the assignment open on one computer and CodeSpace on the other computer.

Students go to make.firialabs.com and should be at the beginning of Mission 13.

Teaching tip - Objective 1: This objective is fairly involved. Students create a user interface for the race controller. They start with a function that will be called frequently to draw all the rectangles and text for the graphical user interface. It uses a location, or position, on the screen to draw all the elements. You may need to have a short lesson or review the coordinates on the screen. Students will answer one question in their assignment. Students will create a new file, and then follow CodeTrek to code the user interface.
Teaching tip - Objective 2: Start the objective by having students answer the reflection question. The code will use a list for the y-coordinates. It will be used to move the select rectangle. The x-coordinate never changes; it is always 0, so y is the only one that needs to be included in a list. The code will also use a variable for the list index. CodeTrek will use the max() and min() functions for the first time. You may want to review what these functions do, and give the students some practice. Students will answer two more questions in their assignment.

NOTE: CodeTrek introduces a new variable to keep track of the previous menu selection. CodeTrek doesn't specifically stop on this line of code (line 33) but there is a comment about it. You might want to point it out.

NOTE: The code will cause a new error with a global variable. It is discussed and fixed in the next objective. But students MUST throw the error to meet the goal of the objective.

Teaching tip - Objective 3: Fix another bug! This time the rectangle moves, but the old one stays. So, erase your tracks!

NOTE: Students need to be careful where they erase the rectangle. It needs to be BEFORE they draw the new rectangle.

Teaching tip - Objective 4: This objective uses the previous selection variable from Objective 2 to "erase" the rectangle before drawing the new rectangle at "menu index". Review as needed.
Teaching tip - Objective 5: Students will create two more functions. One function takes a parameter for a message and displays it at the top. The other function checks which button is pressed and calls the first function, passing in a message for the parameter.
Teaching tip - Objective 6: Students make the UI more user-friendly by initializing the screen. It uses a global Boolean variable "init" and modifies the if statement in the while True loop.
💻Activity - Mission 13 Objectives 7-11

Continue either in pairs or individually. Students go to make.firialabs.com and should be on Objective 7 of Mission 13.

Teaching tip - Objective 7: A lot of information here about the soundlib module. It has functions for creating music and sound effects. First a tone will be selected, and then set a pitch and play. Use stop() to quit playing the tone at the given pitch.
Teaching tip - Objective 8: This objective introduces the for loop as an alternative to the while loop. You might want to practice with your students. Students learned about for loops while preparing for the Create PT. Review as needed.
Teaching tip - Objective 9: This objective discusses blocking and non-blocking functions. You can go back to Mission 5 and look at the code for playing a sound. It is blocking! Here students will learn a different code for non-blocking. In the hint, it shows an argument to add to the get_mp3 statement so the music doesn't start automatically.

This objective also uses a "toggle" Boolean variable. If this is a difficult concept for your students, you will want to review and practice it. CodeTrek does not specifically stop on this line of code (#45) so you may need to specifically point it out.

Teaching tip - Objective 10: Students create another sound using a loop within a loop. You might want to step through this part of the code using the debugger to track the variables, or work it out on a whiteboard.
Teaching tip - Objective 11: Students create another sound using the glide function.
🧩Activity - After Objective 11
Teaching tip - After Objective 11: Students are asked to add two more pieces of code to the program: add a "kill" switch, and change the color of the selection rectangle. You can discuss accessibility during this step, and a reason for changing the color of the selection rectangle. Have students come up with other suggestions for making the program more accessible. Solution code for the final program is provided.
Teaching tip - Challenge: If students have time, have them try the challenge. This gives them an opportunity to apply their knowledge in a new situation. The challenge is to light up pixels during the program. Students can use their creativity to add functionality. A code solution showing a function and the function calls is provided, but students should not be required to have the same solution. Solution code for the challenge is provided.

✅ The assignment is complete and ready to turn in.

🧑‍🤝‍🧑Wrap-Up

Discussion: You can now make a GUI on CodeX! This is a very useful skill that you can apply to many other programs. What are some programs you already created that could use a GUI?

Students complete the wrap-up questions on the assignment. You can have a short discussion on their answers.

Optional. Use a formative assessment for the wrap-up.

Formative assessment:

  • Daily reflection journal
  • Mission 13 Obj. 1-6 Kahoot! Review
  • Mission 13 Obj. 7-11 Kahoot! Review (in class or individual)
  • Exit ticket on lists
  • Exit ticket on global and local variables
  • Exit ticket on blocking and non-blocking functions