We need to choose a backdrop, then add a ball to the game and to begin defining how it behaves.
Add the Backdrop
- In the lower right, click the Backdrop icon.
- Select a suitable backdrop. It will appear on the stage.
Select a Sprite
-
In the lower right, click the Sprites icon.
- Choose a sprite that can serve as a ball (ball, basketball, apple, etc.).
Your sprite will appear in the lower right and on the stage.
Add Movement
-
In the lower right, ensure that the ball sprite is selected.
-
In the upper left, under Motion, drag the Move (10) steps code block into the middle. It will be applied to the selected sprite.
- To "test drive" the code block, click the code block to see your sprite move!
Note: You can edit the number of "steps" in the code block. A higher number will make the ball move faster.
Make It "Bounce"!
-
In the upper left, under Motion, drag the if on edge, bounce code block into the middle and position it just below the Move (10) steps code block. It should "click" into place, like two magnets placed close to one another.
- To "test drive" the code block, click the code block multiple times, to see your sprite move and bounce off the walls!
Why does my program stop?
You click the Move (10) steps code block, and your sprite moves once, but that's it! Why doesn't it continue?
The code blocks in Scratch are commands, for the computer to execute. Once a command executes it goes to the next command (if any) and executes that. When it runs out of commands, it stops.
We need to find a way to make that part of the program "execute" over and over. Most programmers would use what's called a "loop" to accomplish this. We have a code block that can accomplish this for us.
Make It "Run" Without Stopping!
For the game to work, the ball has to bounce around without stopping. To accomplish this, we need to make the code blocks execute over and over. We'll use what's commonly referred to as a "loop."
-
From the left, under Control, click the forever code block and drag it over the two existing blocks. The forever code block will turn gray as it slides over the other two. Make sure that the new block "contains" both of the other two blocks before letting go.
- To "test drive" the new change, click the forever code block, to see your sprite move and bounce off the walls!
Make the Ball Bounce at Angles
- From the left, under Motion, click and drag the point in direction (90) code block into place on top of the stack of blocks.
- Click the number 90 and use the pop-out window to change the value to 45.
Add a Start Command
This last step completes our first block of code. We still have to add some more elements to our game, such as a paddle and a way to keep score.
- At the left, under Events, find the
When (
) clicked code block.
- Drag it out and position it on top of the group of blocks.
- Above the stage, click the Flag icon to
start (
). Click the Stop sign icon to stop the game (
).
That's it! Your first block of code is done! Go to the next tab to continue building your game!