Exercise 2 BabyCatch
Exercise 2 BabyCatch
Title: Baby-Catch
Page 1
Goals
Learn about
event handling simple sequential execution loops variables conditionals parallel execution message passing
Page 2
Game Description
We will make a game where the player controls a baby using the arrow keys to catch a falling ball. If the baby misses the ball the game is over.
Page 3
Start up Scratch
Click on Scratch.exe
Page 4
Page 5
Page 6
Page 7
Page 8
Page 10
Add a Background
Click on the Stage Click on the Backgrounds tab
In the center In the bottom right area
Page 11
Event Handling
We want to control the baby using the arrow keys
When we click the left arrow the baby should move left When we click the right arrow the baby should move right Responding to user actions like mouse clicks and key presses
Page 12
-180
Page 13
In the center area This allows us to create scripts (programs) for the baby Each sprite can have several scripts
Page 14
Page 15
Page 16
Page 17
Page 18
Paint a Ball
Click on the paint brush and star
It will say "Paint new sprite" if you hover over it
Page 19
Drawing area
Page 20
Page 21
Page 22
Page 23
Sequential Execution
One block is executed after the other In order from top to bottom When the green flag is clicked
the ball will go to the specified x and y location
Page 24
Loops
We want the ball to continue to move down unless the baby catches it How do we make this happen?
We could use lots of blocks one after the other But, that would be slow and repetitive
Page 25
Try it out!
Page 26
Page 27
Variables
If we are going to keep track of the score
We want something to hold the current score And we want to be able to change the score We want the value to change or vary
This is called a variable
Page 28
Page 29
Set score to 0
In the ball script Drag the forever down Drag out "set score to 0 Drag the forever back up Notice the score showing on the window
Page 30
Conditionals
We want to increase the score if the baby caught the ball
So this action will only occur only if some condition is true This is called a conditional or an if
Page 31
Page 32
Page 34
Page 35
Adding Losing
If the baby doesn't catch the ball it just gets stuck at the bottom of the screen Let's tell the player that he or she lost
Page 36
Page 37
Click on Control
drag out "when green flag clicked"
Click on Looks
drag out hide
Page 38
Check if Lost
If the y position gets near the bottom (near -180)
Drag out an if
from Control
Add a y position
From Motion
Type in -175
Page 39
Broadcast a message
Sprites communicate by passing messages
One sprite broadcasts the message Other sprites can listen for it and react to it when they receive it Click on Control
drag out "broadcast blank" click on the drop down arrow next to new name it lost Add stop script
to stop the forever loop
Page 40
Receive Lost
Click on the text sprite Click on Control
drag out "when I receive blank" click on the down arrow and select lost drag out show drag out stop all
to stop all scripts
Click on Looks
Click on Control
Page 41
Parallel Execution
We have several things happening at the same time
when the green flag is clicked
Page 42
Create Instructions
Click on the Show Project Notes icon in the upper right corner Add the instructions Press OK
Page 43
Modify the sprites using the Costume tab Save your game with the Save button
Page 44
Click on the Share! button You can also download other projects and see how they were created
Page 45
Other Ideas
Add a sound when you lose Add the ability to win
When you reach a certain score Track the amount of time it takes as well
Speed up the ball over time Add more sprites to catch Add a sprite to avoid (like a big brother)
killer sprite
Page 46
Concept Summary
Variables Forever loops
can hold values and can change value repeat all the commands inside of them one at a time until the script is stopped or all scripts are stopped only execute the body of the if when the condition is true and receive them
Conditionals ifs
Sprites can have several scripts, costumes, and sounds Things can happen one after the other sequential execution or at the same time parallel execution
like clicking the green flag and pressing the left or right arrow keys
Page 47