AP CSP CodeX - Mission 15: Handball

Mission 15 Lesson Plan

Mission 15: Handball

Students program a basic physics engine and build a single player handball game on the CodeX.

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

Overview

In this mission, students build the first half of a Breakout-style game: a single player game of handball on the CodeX. They create a basic physics engine, bounce the ball off the walls and a paddle, and add sound, score and lives. Along the way they use many functions, constants, global variables and delta time. There are a lot of steps, so plan on two class periods. The code continues in Mission 16.

🎯 Mission Goal: Students will apply Python programming concepts and their skills to create a single player game of handball.

🎯

Learning Targets

  • I can program a basic physics game engine.
  • I can use multiple functions to draw the ball, update position, clear the screen, etc.
  • I can use angles to control the ball rebounds.
  • I can use delta time instead of sleep() to manage a fast-paced game.
  • I can manage the game state by initializing the game, updating the game state, and handling game over.
💡

Key Concepts

  • Use functions to organize your code into reusable blocks: paddle position, collision check, updating score, etc.
  • Use constants for variables that do not change.
  • Define and initialize variables as local or global.
  • Focus and refine your game's user experience.
  • Implement collision detection algorithms to detect collisions between the ball and other game objects, such as the paddle or the walls.
✅

Assessment Opportunities

☑️

Success Criteria

  • Define and call 12 functions
  • Draw a screen layout for the game
  • Use CodeX buttons as event handlers for the game
  • Keep track of score and lives
  • Create 2 animated objects in the game: paddle and ball
  • Game works correctly, showing a ball bounce, paddle movement, and score
📁

Digital Resources

🧰

Classroom Materials

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

Real-World Applications

⚾️You are creating a very basic physics engine, modeling the mechanics of velocity, distance and time.
📐You are using the trigonometric functions sine, cosine and tangent to calculate ball angles.
🎮This game provides hands-on experience in creating a simple yet engaging game, allowing learners to understand game mechanics, collision detection, user input handling and basic game design.
🚀

Extensions & Cross-Curricular

ExtensionMake the ball speed up a little each time it hits the paddle. Decide how fast is too fast.
ExtensionUse a different sound for a paddle hit than for a wall hit, so the player can hear what happened.
MathUse sine and cosine to find the x and y parts of the ball's velocity for angles of 30, 45 and 60 degrees. Check the results against the game.
ScienceSpeed = distance / time. The ball moves at 0.15 pixels per millisecond. How many pixels does it move in one second? How long does it take to cross a 240 pixel screen?
Lang ArtsWrite a one page set of game instructions for Handball, so a new player knows the controls, score and lives.
🔤

Vocabulary

▾
Physics engine - Code that models motion using velocity, distance and time.
Initialization (review) - Set the initial or first value of a global variable when the program starts.
Delta time - Elapsed time in milliseconds (or change in time).
UX - User experience. How a person interacts with a program and how it feels to use.
🐍

New Python Code

▾
else: continue if n_lives == 0: continueContinue jumps back to the top of the while loop instead of going to the next sequential step. It can only be used inside a loop. The second example skips the rest of the game loop if no lives are left.
tone = soundmaker.get_tone('trumpet') tone.set_level(15)Adjust the volume of a sound effect.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-03 3A-AP-13 3A-AP-14 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21 3A-AP-23

CSTA Standards - Grades 11-12

3B-CS-02 3B-AP-10 3B-AP-12 3B-AP-14 3B-AP-16 3B-AP-21 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-3.C - Develop procedural abstractions to manage complexity in a program by writing procedures.
AAP-3.D - Select appropriate libraries or existing code segments to use in creating new programs.
Computational Thinking Practice 3.A - Generalize data sources through variables.
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.
  • Plan for two class periods. If you split the mission, stop after Objective 6 on day one.
  • Optional: have the Breakout video or Wikipedia page ready for the warm-up so students can see where the game is headed.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

🧑‍🏫
Teacher Notes
  • There are a lot of steps and code in this project. It will probably take two days to complete.
  • Some objectives are heavy in math, especially trigonometry. The earlier objectives also go into detail about physics with velocity, distance and time. If the math is too advanced for your students, they can still complete the mission using the code that is given and reading through the hints. They are not required to understand all the math equations and concepts.
  • Students create a lot of functions and include many global variables and constants. It is a lot to keep track of, so students need to pay attention to details and type carefully. They should scroll through the code in CodeTrek to make sure they are placing new code in the correct functions and with the correct indenting.
  • Make sure each objective is correct and the code works properly before going to the next objective. Otherwise it will be very difficult to debug.
  • There are no challenges, remix, or extra steps for this program. The code is continued in Mission 16.
🗺️

Lesson Outline

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

🗣️Warm-up

No specific warm-up for this mission. You may want to show a short video of the Breakout game so they can see what they will complete after Mission 16. Or review the background from the Wikipedia page.

💻Activity - Mission 15 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 15.

Teaching tip - Objective 1: This objective includes two functions: one draws the ball and one serves the ball. It uses a bit of math for the location and speed of the ball and the time it takes to move across the screen. It uses a delta for time, similar to the deltas used in Mission 14. The ball will go off the screen.
Teaching tip - Objective 2: Students modify their code to make the ball bounce off the walls instead of disappearing. Students answer a question on the assignment document.
Teaching tip - Objective 3: Students add another function to return the elapsed time. The HINT is useful. Students answer a question on the assignment document.

NOTE: Not many lines of code, but each line is important, so be careful not to skip a step.

Teaching tip - Objective 4: Students use a tuple to keep track of both the x and y position, and a list to keep track of the two velocities. Why not use only lists or only tuples? A list is changeable, so you can do math with it, and a tuple is not. A Boolean variable is added just before and during the collision if statements.

NOTE: There is a LOT going on in this objective. Pay attention to details!

Teaching tip - Objective 5: Students add a function for the screen layout. It needs to be called once at the beginning of the main program. Also, the if statements for collision need to be adjusted.
Teaching tip - Objective 6: Students add sound to the game in the form of beeps. A new function is added and called in the collisions. Several variables and constants are added. Again, pay attention to details!

NOTE: Remember to add from soundlib import *. This shows in CodeTrek but is not specifically mentioned.

💻Activity - Mission 15 Objectives 7-12

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

Teaching tip - Objective 7: Define two more functions, one to draw the paddle and one to check the button presses. Then call the functions in the correct places. With so much going on, it is really important to debug as you go. If a student sees that something is off, fix it before going on!

NOTE: The sound_cut variable is moved from below the tone constants to above. It does not matter either way, so do not let this trip up your students.

IMPORTANT: The values for the ball velocity are changed. The new values show in CodeTrek, but they are not specifically pointed out. The speed is lowered! Use ball_v = [0.1, -0.15], which is in the serve_ball() function.

Teaching tip - Quiz: Students take a short quiz. The first question has three answers. The last question can be a little tricky. You may want to go over this, as it involves two steps.
Teaching tip - Objective 8: The paddle collisions are explained and coded. Careful coding here!
Teaching tip - Objective 9: Make the project into a game by adding game elements. The ball can go off the bottom of the screen, the ball is served again, etc.
Teaching tip - Objective 10: Make the project into a game by adding a score.
Teaching tip - Objective 11: A lot of little details here, in different functions. Not complicated, but students need to pay attention to details.

IMPORTANT: Students need to add n_lives and score to the global declaration in the check_buttons() function. It is mentioned in CodeTrek, but easily missed.

Teaching tip - Quiz: Students take a short quiz. The question is kind of a tricky one using continue. You may want to trace this one with your students.
Teaching tip - Objective 12: This objective uses angles for the bounce. A new function is created for the hit ball angle. More math (a little trig) is used!

IMPORTANT: The global variable ball_speed needs to be defined near the top of the program under serve_timer. This is not pointed out in CodeTrek, so be aware if students get an error message about the variable being undefined and do not get a green checkmark.

IMPORTANT: When typing the math for hit_ball(), make sure the ball_v[1] = -math.sin(angle) * ball_speed statement includes the negative!

IMPORTANT: The check_ball() function is not completely correct in CodeTrek for Objective 12. The global score and score = 0 lines are missing. If students are checking CodeTrek, they may notice this. Do not delete them!

NOTE: The second HINT mentions a line of code to add to the program. It probably is not necessary to do this, but it does not hurt to do it, either.

The assignment is complete and ready to turn in.

🧑‍🤝‍🧑Wrap-Up

The post-mission reflection asks students what they learned during the mission. They may list Python or programming concepts, debugging strategies, or something about themselves.

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