Skip to Main Content

Scratch Programming Language: Build a Simple Game, Part 2

Building a Simple "Pong" Game: Create a Paddle

The paddle is very simple and easy to create. Let's do this!
  1. Sprites icon
    In the lower right, click the Sprites icon.
     
  2. In the collection of sprites, look for the Paddle sprite. Alternatively, any sprite of suitable shape can be used, such as the Broom or the Convertible.
  3. Scratch stage with ball and paddle
    On the Stage, move the paddle close to the bottom of the space.







     
  4. Paddle sprite - selected
    In the lower right, ensure that the Paddle sprite is still selected.

     
  5. set x to (number) code block
    From the top left, under Motion, drag the set x to (number) code block to the middle.

     
  6. set x to (mouse x)
    From the left, under Sensing, drag the mouse x code block onto the previous code block. It will replace the number. This will lock the paddle to the mouse and keep its "height" on the stage the same (lock its x value).
     
  7. set x to (mouse x) and forever code blocks
    From the left, under Control, click the forever code block and drag it over the existing block. The forever code block will turn gray as it slides over the other block. Make sure that the new block "contains" the other block before letting go.

     
  8. Paddle - complete code block
    At the left, under Events, find the
    When (green flag) clicked code block.
  9. Drag it out and position it on top of the group of blocks.
  10. Above the stage, click the Flag icon to
    start ( ). Click the Stop sign icon to stop the game ( Stop sign icon ).



That's it! The Paddle is complete. Now we need to add the code blocks to allow the Ball and Paddle to interact with one another.

Building a Simple "Pong" Game: Ball & Paddle Interaction

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.
  1. if (    ) then code block
    In the lower right, select the Ball sprite.
  2. 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

  1. touching (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.
  2. 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

  1. IF-THEN code block: touching paddle, turn 180 deg.
    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.
  2. Select the number in the Turn ( 15 ) degrees code block. Change the number to 180.
     
  3. 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

  1. Applying forever block to a stack of code blocks
    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.


     
  2. Adding Start code block to stack
    At the left, under Events, find the
    When (green flag) clicked code block.
  3. Drag it out and position it on top of the group of blocks.
  4. Above the stage, click the Flag icon to
    start ( ). Click the Stop sign icon to stop the game ( Stop sign icon ).

 

 

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!

Staff LADR