We need a way to make the ball react to hitting the paddle. We'll add a second code block to the ball, to handle that interaction.
The If/Then Code Block
An If/Then statement watches for an event to occur or for certain conditions to be met. IF this happens, THEN one or more commands are executed. This is a common feature in many programming languages.
-
In the lower right, select the Ball sprite.
- From the left, under Control, drag the if ( ) then code block to the middle.
Note: Don't add this code block to the previous stack of code blocks. This is the start of a new group.
Make Ball Detect Collision with Paddle
-
From the left, under Sensing, click and drag the
touching ( mouse pointer ) ? code block onto the hexagonal placeholder of the if ( ) then code block.
- Select the drop-down arrow of the
touching ( mouse pointer ) ? code block and change mouse pointer to Paddle.
Note: We have the IF, now we'll add the code blocks for the THEN, to tell the Ball what to do after it touches the Paddle.
Add Ball's Reaction to Being Hit by Paddle
-
From the left, under Motion, click and drag the
Turn ( 15 ) degrees code block to the middle and place it inside the if ( ) then code block.
- Select the number in the Turn ( 15 ) degrees code block. Change the number to 180.
-
From the left, under Motion, click and drag out the
Move ( 10 ) steps code block and place it inside the
if ( ) then code block, below the code block that's already there.
Add Forever Loop, Start Flag
-
From the left, in Control group, select the forever code block. Move it over the stack of code blocks, where it will show a gray "shadow." Position the shadow so that it contains the entire stack and release the mouse button.
-
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 (
).
You're almost done! Now we need to add features that allow you to keep score, to make this a real game! Click the next tab to finish your game!