Skip to Main Content

Scratch Programming Language: Build a Simple Game, Part 3

Building a Simple "Pong" Game: Modify the Second Code Stack for the Ball

Let's begin this part by modifying the behavior of the ball. We'll also create a variable to keep track of the player's score and add a code block to add a point each time the player hits the ball. Here's the second code stack for the ball:

"Soften" the Bounce of the Ball from the Paddle

Let's add a tiny pause in the ball's movement after being hit with the paddle. We'll also add a bit of randomness to its direction, coming off the paddle, as if some spin were imparted to it when it was hit.
  1. Insert wait ( 1 ) seconds code block
    From the left, in the Control group, select the wait ( 1 ) seconds code block.
  2. Drag it to a position below the turn ( 180 ) degrees code block.
     
  3. wait ( .5 ) seconds code block
    Click the number 1 and change it to .5 seconds.
     

Add Some Random Movement to the Ball

When the ball hits your paddle, it bounces away on the exact reciprocal of its direction when it struck the paddle. We can easily make these bounces more random and make the game more interesting.

  1. Add random numbers to Turn code blockFrom the left, in the Operators group, choose the pick random ( 1 ) to ( 10 ) code block.
  2. Drag and drop the new code block onto the turn ( 180 ) degrees code block.
  3. pick random number from 160 to 200
    Click the two numbers and change them to two numbers on either side of 180. The wider the separation between the two numbers, the more
    random the ball's motion will be.

Building a Simple "Pong" Game: Raise the Stakes!

Add a Line at the Bottom of the Game
  1. Sprites icon
    In the bottom right, click the Sprites icon.

     
  2. In the collection of sprites, look for the Line sprite and select it.
  3. On the stage, position the Line near the bottom of the background, below the Paddle.

Detect When the Ball Touches the Bottom (Line)

  1. In the lower right, select the Line sprite.
  2. IF-THEN code block
    From the left, under Control, drag the if (    ) then code block to the middle.

     
  3. IF-THEN detect when Ball touches Line
    From the left, under Sensing, click and drag the
    touching ( mouse pointer ) ? code block onto the hexagonal placeholder of the if (    ) then code block.
  4. Select the drop-down arrow of the touching ( mouse pointer ) ? code block and select Ball.

Stop Game When the Ball Touches the Line

  1. IF Ball touches Line, THEN stop all
    From the left, under Control, drag out the stop ( all ) code block and drop it inside the if (    ) then code block.

     

Add Forever Loop, Start Flag

  1. Forever stop the game when Ball touches Line
    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. IF Ball touches Line THEN stop game - complete 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 ).

 

 

Staff LADR