(Also available in Pyret)
Students discover that inequalities have an important application in video games: keeping game characters on the screen! Students apply their understanding to edit code so that it will keep Sam the Butterfly safely in view.
Lesson Goals |
Students will be able to:
|
Student-Facing Lesson Goals |
|
Materials |
|
Supplemental Materials |
|
Key Points for the Facilitator |
|
🔗Introducing Sam 15 minutes
Overview
Students are introduced to Sam the Butterfly, a simple activity in which they must write simple inequalities to detect when Sam has gone too far in one dimension.
Launch
-
Open the Sam the Butterfly Starter File in a new tab and save a copy of your own.
-
Complete Introducing Sam, clicking "Run" and using the arrow keys to investigate the program with your partner.
As you explore the program, notice that Sam’s coordinates are displayed at the top of the screen! When Sam is at (0,0), we only see a part of Sam’s wing because their position is based on the center of the butterfly image.
Students should observe that Sam can go up to, but not beyond, an x of -50. Students can represent this algebraically as x > - 50, or (for students who notice that Sam only moves in increments of 10) x ≥ - 40.
Eagle-eyed students might also notice that the butterfly image is defined at the top of the file, as IMG
! Changing this definition will allow them to customize the way Sam looks.
Every time Sam moves, we want to check and see if Sam is safe.
In this programming language, question marks are pronounced "huh?" So safe-left?
would be pronounced "safe-left-huh?"
-
What three functions do you see defined in this file?
-
What should our left-checking function do?
-
Check to see if x is greater than -50.
-
-
What should our right-checking function do?
-
Check to see if x is less than 690.
-
-
What should
(onscreen?)
do?-
Answers may vary. Let students drive the discussion, and don’t give away the answer!
-
Investigate
-
Complete Left and Right with your partner.
-
Once finished, fix the corresponding functions in your Sam the Butterfly file, and test them out.
Students will notice that fixing safe-left?
keeps Sam from disappearing off the left side, but fixing safe-right?
doesn’t seem to keep Sam from disappearing off the right side! When students encounter this, encourage them to look through the code to try and figure out why.
"False" doesn’t mean "Wrong"!
A lot of students - especially confident ones - may struggle to come up with an example where safe-left?
returns false
:
; Students hate writing the second one!
(EXAMPLE (safe-left? 189) (> 189 -50))
(EXAMPLE (safe-left? -65) (> -65 -50))
This misconception comes from confusing a statement that is "false" with a program that is "wrong". In the second example, above, the result of (safe-left? -65)
is false
, because "65 is greater than -50" is a false statement. Remind your students that you want one example that’s true, and a second that’s false!
The only way to know if safe-right?
works is to test it by itself. Did you write examples for that function…?
If a car starts up when you turn the key, does that mean we know the sunroof works? Of course not! You can’t trust the behavior of a complex system, just because one part of it works! The only way to see if the sunroof works is to test it.
Synthesize
-
Does
safe-left?
work correctly? How do you know? -
Does
safe-right?
work correctly? How do you know?
🔗Protecting Sam on Both Sides 30 minutes
Overview
Students solve a word problem involving compound inequalities, using and
to compose the simpler Boundary-checking functions from the previous lesson.
Launch
-
We need three volunteers to roleplay the functions
safe-left?
,safe-right?
, andonscreen?
! -
Take 1 minute to read the Contract and code for your function, as written in the program.
Ask the volunteers what their name, Domain and Range are. Explain that you, the facilitator, will be providing a coordinate input. The functions safe-left?
and safe-right?
will respond with either "true" or "false".
The function onscreen?
, will call the safe-left?
function, giving them a number representing Sam’s x-coordinate. The student roleplaying onscreen?
should turn to safe-left?
and give the input to them.
For example:
-
Facilitator: "onscreen-huh 70"
-
onscreen?
(turns tosafe-left?
): "safe-left-huh 70" -
safe-left?
: "true" -
onscreen?
(turns back to facilitator): "true"
-
What is the problem with
onscreen?
?-
It’s only talking to
safe-left?
, it’s not checking withsafe-right?
-
-
What should
onscreen?
be doing?-
It needs to talk to
safe-left?
ANDsafe-right?
-
Investigate
-
Complete Word Problem: onscreen?.
-
When this function is entered into the editor, students should now see that Sam is protected on both sides of the screen.
Extension Option
What if we wanted to keep Sam safe on the top and bottom edges of the screen as well?
-
What additional functions would we need?
-
What functions would need to change?
Note: In the context of Sam the Butterfly, there’s a clear reason why we want to stop Sam from going off the top and bottom of the screen! But when we add the onscreen?
function to our Game Starter File, we might want to let the player go above and below the screen. It will be important that the onscreen?
function we use in the game only checks the x-coordinate.
We recommend that students tackling this challenge define a new function onscreen2?
so that their initial onscreen?
code remains ready for them to use in the next section of this lesson.
Let’s have our three student volunteers roleplay those functions again, with the onscreen?
function working properly. Be sure to test out values where Sam is off one edge, off the other, and on the screen!
Repeat the back-and-forth that happened earlier, making sure that the onscreen function calls both the left-checking and right-checking functions and that those functions work correctly!
Synthesize
-
How did it feel when you clicked Run, and saw Sam hit both walls for the first time?
-
Are there multiple solutions for
onscreen?
? -
Is this Top-Down or Bottom-Up design?
🔗Boundary Detection in the Game 10 minutes
Overview
Students identify common patterns between two-dimensional Boundary detection and detecting whether a player is onscreen. They apply the same problem-solving and narrow mathematical concept from the previous lesson to a more general problem.
Launch
Open your in-progress game file and click "Run".
-
How are the
TARGET
andDANGER
behaving right now?-
They move across the screen.
-
-
What happens when they go off the edge of the screen?
-
They just keep going!
-
-
What do we want to change?
-
We want them to come back after they leave one side of the screen.
-
-
Is this similar to a problem we’ve already solved somewhere else?
-
This is the same problem we solved with Sam!
-
Investigate
Apply what you learned from Sam the Butterfly to fix the safe-left?
, safe-right?
, and onscreen?
functions in your own code.
Since the screen dimensions for their game are 640x480 - just like Sam - we can use our code from Sam with virtually no changes!
Students who tackled the optional safe-top?
and safe-bottom?
challenges should be sure NOT to add this to their game code! There are some exciting challenges in later lessons that rely on characters being able to go off the top or bottom edge of the screen!
Common Misconceptions
-
Students will need to test their code with their images to see if the boundaries are correct for them. Students with large images may need to use slightly wider boundaries, or vice versa for small images. In some cases, students may have to go back and rescale their images if they are too large or too small for the game.
-
Students may be surprised that the same code that "traps Sam" also "resets the
DANGER
andTARGET
". It’s critical to explain that these functions do neither of those things! All they do is test if a coordinate is within a certain range on the x-axis. There is other code (hidden in the teachpack) that determines what to do if the coordinate is offscreen. The ability to re-use function is one of the most powerful features of mathematics - and programming!
Synthesize
The same code that "trapped" Sam also "resets" the DANGER
and the TARGET
. What is actually going on?
🔗Additional Exercises
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.