AP CSP CodeX - Mission 9: Game Spinner

Mission 9 Lesson Plan

Mission 9: Game Spinner

Students use a list, functions, and while loops to animate the arrow of a game spinner on the CodeX.

⏰ 45 min đŸŽ¯ Grades 9-12 đŸ’ģ CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

During this mission, students animate a game spinner with custom functions. They use a logical operator to detect a press from either button, traverse a list of arrows, and learn techniques to avoid an index out of range error. After Objective 8, students use modulo division from the previous lesson to keep the index in range.

đŸŽ¯ Mission Goal: Students will use a list, functions and while loops to animate the arrow of a game spinner.

đŸŽ¯

Learning Targets

  • I can apply properties and uses of an index to a new program.
  • I can define and call a function.
  • I can utilize multiple variables in a new program and describe their purposes.
  • I can utilize loops to make my code more efficient.
  • I can create simulations using computer hardware and software.
💡

Key Concepts

  • Logical operators like 'and', 'or', and 'not' allow your code to test for multiple conditions.
  • Define functions to break complex code into smaller, easy to use (and re-use) pieces.
  • Detect instantaneous button presses with button.is_pressed()
  • Make your own animated sequences with loops and index variables.
  • Loop for a determined number of iterations: while index < N
✅

Assessment Opportunities

â˜‘ī¸

Success Criteria

  • Display a random arrow from a list
  • Detect a press from either BTN-A or BTN-B
  • Use a loop to animate an arrow spinning
  • Use a local variable to increase the number of loops
  • Use a local variable to increase the length of the delay
  • Use modulo division to keep the index in range
📁

Digital Resources

🧰

Classroom Materials

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

Real-World Applications

🎲Use the game spinner in a real-world application. After the project, have a group of students gather in a circle to play with what they've programmed. Disconnect the CodeX and run it on batteries. Put it on the floor or table in the middle of the group, so the random arrow selects a student each time the button is pressed. Maybe the chosen one gets to name something different they could build using CodeX and the code they've learned!
đŸ•šī¸Fast button inputs, animation, and simulation are essential ingredients for lots of interesting applications:
  • Video games
  • Flight simulators
  • Virtual reality
🚀

Extensions & Cross-Curricular

ExtensionAdd a "kill switch" to stop the loop and end the program.
ExtensionAdd audio to the game spinner, something like a click or beep each time the arrow moves.
ExtensionUse one button to spin clockwise and another button to spin counterclockwise.
ExtensionUse random.randrange() to pick a different number of loops for each spin, so every spin lasts a different amount of time.
MathThe spinner has 8 arrows. Use modulo division to predict which list index the spin will end on for a given number of loops, then run the program to check your prediction.
ScienceThe delay grows a little on each loop, so the arrow slows down like a real spinner. Research how friction slows a spinning object and compare it to the animation.
Lang ArtsWrite a short set of rules for a board game that uses your spinner. Use clear, step-by-step directions another student could follow.
🔤

Vocabulary

▾
Logical operator - Operators that handle combinations of Boolean results; not, and, or.
Function - A named chunk of code you can run anytime just by calling its name; also called a procedure.
Simulation - Code that builds a model of something, and lets you play with that model. Simulations let you explore "virtual" situations, both realistic and imaginary, that might be difficult or impossible to do in the real world.
Parameter - A local variable in a function that receives a value passed into the function when it is called; information the function needs to complete its task.
Argument - The value passed into a function - information the function needs to complete its task. An argument can be a literal value, a variable, or an expression.
Local variables - Variables defined inside a function, and can only be used within that function.
🐍

New Python Code

▾
if buttons.is_pressed(BTN_A) or buttons.is_pressed(BTN_B):Using a logical operator.
def show_random_arrow(): num = random.randrange(8) display.show(pics.ALL_ARROWS[num])Define a function.
show_random_arrow()Call a function.
while index < 8: my_arrow = pics.ALL_ARROWS[index] display.show(my_arrow) sleep(0.1) index = index + 1Finite loop with a condition. (Also shows incrementing the control variable.)
while loops < count: my_arrow = pics.ALL_ARROWS[index] display.show(my_arrow) sleep(delay) delay = delay + 0.005 loops = loops + 1 index = index + 1 if index == 8: index = 0Finite loop with a condition and list wrapping.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-03 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-12 3B-AP-14 3B-AP-15 3B-AP-16 3B-AP-21 3B-AP-23

AP CSP Framework

AAP-2.F - Write expressions using logical operators, and evaluate expressions that use logic operators.
AAP-3.B - Explain how the use of procedural abstraction manages complexity in a program.
AAP-3.C - Develop procedural abstractions to manage complexity in a program by writing procedures.
Computational Thinking Practice 3.B - Use abstraction to manage complexity in a program.
Computational Thinking Practice 4.C - Identify and correct errors in algorithms and programs, including error discovery through testing.
📝
Preparing for the Lesson
  • The assignment is best completed digitally. Prepare the assignment to give through your LMS.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

🧑‍đŸĢ
Teacher Notes
  • This lesson introduces functions, but it will be mostly a review for your students.
  • 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 8, before turning in the program.
  • Extensions are given in the teaching guide (but not the assignment) for any students finishing the program and having extra time.
  • If you have time at the end of the lesson, use the Mission 9 Kahoot! Review.
  • 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

Use a discussion strategy, like journaling, working at boards, selecting random students, or a form of think-pair-share.

There isn't a specific warm-up for this lesson. As a teacher, decide on a topic that may need review with your students before starting the mission. You may want to review functions, or lists and indexes. Or you can go over common programming errors. Keep it to a quick review and then get right to the mission.

đŸ’ģActivity - Mission 9

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 activity guide open on one computer and CodeSpace on the other computer.

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

Teaching tip - Objective 1: The objective uses a built-in arrow list. If students don't read the instructions carefully, they will create the list. They do not need to! They should use their knowledge (and code if necessary) to display a random arrow from the built-in list.
Teaching tip - Objective 3: This objective is all about functions. This should be a review, since students have been working with functions for quite some time. See how much they remember and can do on their own.
Teaching tip - Objective 5: This objective introduces more vocabulary: simulation, parameter, and argument. Future lessons will cover parameters, arguments and local variables in more detail. Treat this objective as an introduction to them, knowing there is more to come.
Teaching tip - Objective 6: This objective uses the debugger. If students haven't used it for a while, you may need to give them a refresher. Or go through the debugger on a big screen for all the students. They will still need to do it individually to meet the goal. The hint explains when to press a button so the function is called. This objective will cause an error! It is fixed in the next objective.
Teaching tip - Objectives 7 & 8: These objectives are mostly a review and shouldn't take a long time.
🧩Activity - After Objective 8
Teaching tip - After Objective 8: The students will use modulo division in the program to keep the index in range. This is similar to the program they modified for practice in the "Types of Division" lesson.

Extensions for students who finish and have extra time (if the mission takes up the entire class period, skip these altogether):

  • Add a "kill switch" to stop the loop and end the program.
  • Add audio to the game spinner, something like a click or beep each time the arrow moves.
  • Use one button to spin clockwise and another button to spin counterclockwise.

No example solutions are given for the extensions.

✅ The assignment is complete and ready to turn in.

🧑‍🤝‍🧑Wrap-Up

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

Formative assessment:

  • Daily reflection journal
  • Mission 9 Kahoot! Review (in class or individual)
  • Exit ticket or group review on functions, local variables, or loop conditions