Lesson 13: Other Forms of Input
Overview
In this lesson students continue to explore ways to use conditional statements to take user input. In addition to the simple keyDown()
command learned yesterday, students will learn about several other keyboard input commands as well as ways to take mouse input.
Purpose
Students have learned how to make simple decisions with conditionals. Sometimes however we want to make decision based on if the condition we asked about originally was false or we want to make a decision based on multiple conditions being true. Thats where else statements and more complex conditionals come in. Else statements are a second statement which is attached to a if statement. Else statements execute when the if statement it is attached to is false. You can think of it as "if something is true do thing 1 else do thing 2.
This concept is introduced alongside several new key and mouse input commands, allowing students to gradually build up programs that input in different ways.
Agenda
Warm Up (5 minutes)
Activity (40 minutes)
Wrap Up (5 minutes)
View on Code Studio
Objectives
Students will be able to:
- Use an else statement as the fallback case to an if statement
- Differentiate between conditions that are true once per interaction, and those that remain true through the duration of an interaction.
Vocabulary
- Conditionals - Statements that only run when certain conditions are true.
Introduced Code
Teaching Guide
Warm Up (5 minutes)
Check for Understanding
Remarks
Today we'll be picking up today where we left off yesterday - using conditionals to write programs that respond to user input. Let's refresh what we learned yesterday.
Prompt:
- What is a Boolean? (eg. a true/false value)
- What is the relationship between a Boolean and a Conditional? (eg. a conditional asks a Boolean question and runs code if the answer is true)
- What are some examples of comparison operators that result in a Boolean? (eg. >, <, ==)
- What is the difference between
=
and==
? (eg.=
is used to assign a value,==
is used to check if two values are equal)
Activity (40 minutes)
If/Else and More Input
Video: Watch the Conditionals Video together as a class. It will be a review of if statements and introduce some new concepts too.
Transition: Send students to Code Studio
Wrap Up (5 minutes)
Share Out
Share: The final program in this lesson is asks students to expand a program to uses conditionals for user input in new and interesting ways. Have your students share their projects with the class, prompting them to explain how they used conditionals in their programs.
- Lesson Overview
- Student Overview
Student Instructions
Mouse X and Y
One of the simplest ways to take input is to just make a sprite follow the user's mouse position. You can get the x and y location of the mouse using World.mouseX
and World.mouseY
. This follows the pattern you learned with sprite properties. World
is the name of the object. mouseX
and mouseY
are the names of the properties.
Do This
You are going to make a bee sprite follow the mouse around the game area.
- The bee image is already loaded in the animation tab for you.
- Create a bee sprite that draws in the center of the window.
- Inside the draw loop update the position of the sprite to the position of the mouse:
- Set the x position of the sprite to the value of
World.mouseX
. - Set the y position of the sprite to the value of
World.mouseY
.
- Set the x position of the sprite to the value of
- Run the program to test that it works.
Student Instructions
Random Around Point
Now that you can make the bee follow the mouse, lets make a bee fly around the mouse as shown in the picture on the right. In order to do this, you will need to add a random amount between -50 and 50 to the mouseX
or mouseY
.
Do This
You already have a bee sprite that follows the mouse.
- Update the x and y location to be randomly close to the mouse.
- Add a random number between -50 and 50 to the
World.mouseX
andWorld.mouseY
.
- Add a random number between -50 and 50 to the
- If necessary, use the
World.frameRate
block to slow down the animation and find the best frame rate.
Challenge: Add 3 more bees that follow the mouse in the same way to make a swarm of bees.
- Video: Conditionals
- Student Overview
- If-Else Statements
- Student Overview
Student Instructions
What If My Condition Isn't True?
Sometimes we want to tell our program what to do if a condition is true
, but also what to do if it's false
. Pressing the plus button at the bottom of your conditional block will give you another section called else
. This else
section will be run whenever the condition in the if
before it is false
.
Do This
The gears are back again. The last time you worked with them, you made them spin when the space bar is pressed. Instead of the gears only spinning when the space key is pressed, we want them to spin one way when the space key is pressed and spin the other way when it's not pressed.
- Click the plus button at the bottom of the if block to add an else.
- Add commands to make the gears spin opposite of the direction that they do when the space bar is pressed.
Student Instructions
When to Provide a Fallback
The else clause is useful as a fallback to the main condition that you're checking - that is, if you care what happens when your primary condition is false
, you should provide an else clause to take care of it.
Do This
Click "Run" to see the swarm of bees created for you and a flower on the left side of the screen. Make the swarm of bees appear when the mouse is near the flower (on the left side of the screen) and disappear when the mouse is away from the flower (on the right side of the screen). Look at the example on the right.
- Add an
if else
statement after you update the position of the bees. - In the input of the if use a boolean to check if the x position of the mouse is on the side of the screen with the flower.
- Set the
visible
property of each bee inside both the if and else statements appropriately to make the bees only show near the flower.
Student Instructions
Make a Prediction: User Input
So far you've used keyDown
as a way to let users control your programs, but that's just one of many ways to take input. In fact, just one of many ways to detect keypresses! Depending on how you want to react to a keypress, there are a few other blocks you might want to use.
Read the program and predict below what will happen when you press each of the up, down, left, and right arrows.
After making your prediction, run the code and write down or share with your neighbor your observations.
- What seems to be the difference between
keyDown()
,keyWentDown()
, andkeyWentUp()
? - What do you think the exclamation mark (
!
) on line 10 does? - How might you use the different keypress blocks in a game?
Student Instructions
Student Instructions
Mouse Clicks
Keypresses are great, but sometimes you want users to interact through mouse clicks. There's a new block called mouseDown
which, similar to keyDown
, checks whether the left or right mouse buttons is being pressed. If you are using a computer with a mouse or trackpad that has only one button, you'll want to always use mouseDown("left")
.
Do This
Here's a program that drops a balloon down the screen - you're going to program the mouse button to raise the balloon back up while it's clicked.
- Add an if else statment that checks for
mouseDown
. - Inside the conditional, move the balloon up one pixel if the mouse is down. Otherwise, move the balloon down.
Hint: You'll need to move the code that drops the balloon for this to work - you only want it to run if mouseDown
is false
Challenge: Can you make the balloon drift randomly to the left and right as it rises and falls?
Student Instructions
Responding to a Single Click
Earlier we learned that keyWentDown
and keyWentUp
can be used to respond to a keypress a single time. The blocks mouseWentUp
and mouseWentDown
allow you to do that for the mouse!
Do This
Let's make a simple game that counts how many times you've clicked. We've already provided a variable clicks
that you can use to track how many times the user has clicked.
- Add a conditional that checks if the mouse went down.
- Inside your conditional, add to the
clicks
variable.
Challenge: Can you add a sprite that responds to mouseWentDown
as well? Add an image of your choice and increase the sprite's size each time the mouse is clicked.
Student Instructions
mouseDidMove
We can also use Boolean expressions to check whether or not the mouse has moved. The mouseDidMove
block will return false
if the mouse is still, but true
if the mouse has been moving.
Do This
Right now, this program just displays a salt shaker sprite. You'll need to use mouseDidMove
so that you can "shake" the salt by moving the mouse back and forth.
- Add a conditional that checks if
mouseDidMove
. - If the conditional is true, rotate the salt sprite randomly to the left or right.
Challenge: Can you keep track of how many times the mouseDidMove
shakes the salt, and then rotate it right side up after 100 shakes?
Student Instructions
Challenge: Checking for Multiple Conditions
Check with your teacher before taking on this challenge.
So far we've looked at a lot of ways to check if a single condition is true, but often a program needs to check the state of many conditions simultaneously before making a decision. For this challenge, let's assume the following scenario:
- The sprite should move up, down, left, and right if the corresponding arrow key is pressed.
- The sprite should not go all the way off the screen in any direction.
Do This Together
Before you tackle writing this program, you'll need to figure out how to check multiple conditions at once.
- Brainstorm with your neighbors ways you might check for more than one condition.
- Share back with other classmates so you can see other potential approaches.
- Explore the toolbox for blocks that might help (pay extra attention to the Math and Control drawers).
- Program your proposed solution.
- Test your program to make sure it's actually checking all of the conditions you intended.
Student Instructions
Free Play
Use what you've learned to create whatever you like. When you're finished, you can click to send your creation to a friend, or to send it to your Projects Gallery.
Standards Alignment
View full course alignment
CSTA K-12 Computer Science Standards (2017)
AP - Algorithms & Programming
- 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
- 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
- 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
- 2-AP-16 - Incorporate existing code, media, and libraries into original programs, and give attribution.
- 2-AP-17 - Systematically test and refine programs using a range of test cases.
- 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.