240 likes | 247 Views
Learn about variables and iteration in computer programming by creating a Scratch game that rewards points for choosing healthy food and deducts points for choosing unhealthy food.
E N D
Variables Exploring Computer Science Lesson 4-8
Objectives The students will be able to: • Explain the concept of variables. • Create examples of variables. • Explain the concept of iteration. • Create examples of iteration.
Discussion What does the word variable mean in both mathematical and English terms?
Variables • Look at this example: x + 3 = 5 • What is the variable? • What is the value of x? • Look at this example: 2x= 12 • What is the variable? • What is the value of x? • Why isn’t “x” the same in both cases?
Variables in Programming • A variable is a name that represents a value that can be changed.
Open Scratch! • You are going to build the beginning of a Scratch game. • The program will reward you with points when you choose a good food and take points away when you choose a bad food.
Add Text and Sprites • Create a solid color background. • Add the text “Click on a food to eat it” to the background. • Add sprites for bananas and cheesy-puffs.
Adding a Variable • Click on “Variables”. • Click on “Make a Variable”. • Name the variable “Good Nutrition Points” and set it for all sprites.
Adding Nutrition Points • I want to add a point when the Banana sprite is clicked. • With the Banana sprite selected, add this block: • Adding 1 to a variable is called iteration.
Subtracting Nutrition Points • I want to subtract a point when the Cheesy-puffs sprite is clicked. • With the Cheesy-puffs sprite selected, add this block:
Initializing the Variable • We want the Nutrition Points to be set to zero when the game starts. • Add this block to any sprite:
Customize Your Program! • Add a food that is worth 2 points when clicked. • Add a food that is -3 points when clicked. • Show me your program when you’re done. • For extra credit, separate your 1-scene program into 3 scenes with different backgrounds: title, instructions and game itself.
Conditionals Exploring Computer Science Lesson 4-9
Objectives The students will be able to: • Explain the concept of conditionals. • Enhance a variable program with conditionals.
Discussion • How would you finish a knock-knock joke when the third line begins with “If”? • What are some ways we use the word “if” in English?
If Statements in Computing • If you have started to think about building animations like simulations and video games, then to build those more advanced programs, you will need to write code that involves decisions: If (some condition), then (do this).
Examples • When you exit Microsoft Word, it asks you if you want to save your file. • If you click “Yes”, the file is saved. • If you click “No”, your changes are discarded. • In a car-race simulation, the driver steers the car around curves and past mile-markers. • If the car stays on the road, the score increases. • If the car goes off the road into the stands, the car crashes. • If the driver gets the car over the finish-line, the time is posted and the driver wins!
Scratch “if” Block • The Scratch “if” block looks like this: • Note that only a hexagon shaped block will fit.
Example • What does this program do?
Expand the Program Currently, it only does the first condition. Your task is to finish the program so that the cat will tell you the rest: • If you are older than 2, “You don't need diapers." • If you are older than 15, “You can drive." • If you are older than 16, “You can see an R-rated movie." • If you are older than 17, “You can vote." • If you are older than 20, “You can gamble." • If you are older than 24, “You can rent a car." • If you are older than 49, “You can retire.“ • Finally add: If the age is less than 3, "Sorry. You are not old enough for anything yet!"
Enhance Your Nutrition Program Enhance your Nutrition program by: • Adding a message about being nutritious if the number of points becomes greater than 9. • Adding a message about eating healthier food if the number of points becomes less than -4. • A sprite can say these messages or you can use broadcast to change the sprites/stage to convey the messages. • For extra credit, use both broadcast AND a sprite to convey the messages.