In this Scratch game we will use the video sensing blocks to interact and play a game where we have to keep multiple footballs in the air!
To do this lesson your computer must have a camera on it.
Create a new Scratch project and delete the cat sprite.
Go to the Scratch website using the link below and click on the 'Create' link in the blue bar at the top.
By default, each new project starts with the cat sprite already added. To delete the cat click on the x in the blue circle beside the cat in the sprite list.
Add the Basketball sprite from the sprite library.
To add a sprite from the sprite library follow these steps:
You can use search box or the filter links (Animals, People, Fantasy etc) to locate your sprite.
We're going to create a custom block of our own that we'll use to move the basketball up to a random place at the top of the screen.
Create a custom block called 'go to top' and add the following code to the define go to top block to make the basketball go to a random place at the top of the stage area.
define go to top
go to x (pick random (-250) to (250)) y (250)
when green flag clicked
turn video [on v] // add it under here
go to top
And then add the go to top block underneath the when clicked block.
Test that it works by clicking the green flag several times. The basketball should go to different random places at the top of the stage area.
You can create your own custom blocks and give them their own instructions (code). To create a custom block follow these steps:
A 'Define My Block Name' will appear in the code area. You can add your code underneath this and then use the My Block Name block in the 'My Blocks' toolbox anytime you wish to run that code.
Now we will program the ball to fall down by changing it's y position. The y position sets where the sprite is on the Y axis (up and down).
Add the following code underneath the go to top block.
define go to top
go to x (pick random (-250) to (250)) y (250)
when green flag clicked
turn video [on v]
go to top// add it under here
forever
change y by (-5)
end
We're going to create a custom block that we'll use to bounce the basketball when we appear to touch it.
Create a custom block called 'bounce ball' and add the following code to the define bounce ball block to make the basketball 'bounce' back up to the top of the stage area and then go to a new random place.
define go to top
go to x (pick random (-250) to (250)) y (250)
define bounce ball
repeat until < (y position) > (180) > // repeat until it gets to the top
change y by (10) // make it go up
end
go to top
You can create your own custom blocks and give them their own instructions (code). To create a custom block follow these steps:
A 'Define My Block Name' will appear in the code area. You can add your code underneath this and then use the My Block Name block in the 'My Blocks' toolbox anytime you wish to run that code.
We're going to use the video camera to put you into the game, every time you touch the ball it will make it bounce up! We can detect 'video motion' around the sprite by using the video [motion] on [sprite] block.
Add the following code inside the forever block and underneath the change y by -5 block:
define go to top
go to x (pick random (-250) to (250)) y (250)
define bounce ball
repeat until < (y position) > (180) >
change y by (10)
end
go to top
when green flag clicked
turn video [on v]
go to top
forever
change y by (-5) // add the new code under here
if < (video (motion v) on (sprite v)) > (25) > then
bounce ball
end
end
If the basketball falls all the way to the bottom of the screen then it's game over! So we need to add some code to detect if the ball goes all the way down.
Add the following inside the forever block and underneath the if then block.
define go to top
go to x (pick random (-250) to (250)) y (250)
define bounce ball
repeat until < (y position) > (180) >
change y by (10)
end
go to top
when green flag clicked
turn video [on v]
go to top
forever
change y by (-5)
if < (video (motion v) on (sprite v)) > (25) > then
bounce ball
end
if < (y position) < (-150) > then // add in this code
stop [all v]
end
end
To add more basketballs to your game and make it more difficult, we can duplicate the first basketball. This will create a copy of the sprite and it's code!
Right click on the basketball, click on the 'duplicate' option in the menu that appears.
That's all the steps to create your Keepy Up game, have fun playing it!
Can you think of any ways you could make the game better? How about adding some sound effects or even keeping score!
We're going to use the camera that's in your computer to take a video and put you in the game!
Add the Video Sensing Extension so that we can use the Video Sensing blocks.
Then add the following code to Basketball sprite, to turn on the camera on your computer.
when green flag clicked
turn video [on v]
Scratch Extensions make it possible to connect Scratch projects with external hardware (such as LEGO WeDo or micro:bit), sources of information on the web (such as Google Translate and Amazon Text to Speech), or blocks allowing for more advanced functionality.
When an extension is enabled, its blocks appear in a location with the same name as the extension.
To load an extension, click the icon in the bottom-left hand corner of the screen and select an extension.