Skip to Main Content

Scratch Programming Language: Build a Simple Game, Part 4

Building a Simple "Pong" Game: Add Scoring to the Game!

Use a Variable to Keep Score

Keep track of your progress as you master the game of Pong!

  1. Make a variable
    From the Variables group, select Make a Variable.
     
  2. Name the new variable
    In the New variable name: box, enter score.






     
  3. Code block: Change (my variable) by (1)
    From Variables, drag Change (my variable) by (1) to the larger Ball code stack. Insert it inside the if-then block, above the turn (pick random [160] to [200]) degrees code block.
     
  4. Change (my variable) to (score)
    Click the my variable drop-down and change it to score.


     
  5. Your finished stack of code blocks should look something like this:
    Ball and Paddle code stack

Building a Simple "Pong" Game: Start a New Game

Now let's create a new stack of code to reset the score and the position of the ball each time a new game is started.

Reset the Score to 0 at the Start of Game

  1. Ball sprite
    In the lower right, ensure that the Ball sprite is selected.

     
  2. set ( my variable ) to ( 0 ) code block
    From Variables, select the set ( My variable ) to ( 0 ) code block and drag it to the middle.
    Note: This is the start of a new stack of code blocks.
  3. Changing ( my variable ) drop-down
    In the new code block, click the ( my variable ) drop-down and select score.



     

Center Ball at Start of Game

This moves the ball to the center of the play area when a new game is started (when the green flag is clicked). This is essential, as a previous game probably left the ball at the bottom of the play area.

  1. From Motion, select the go to x: ( valuey: ( value ) code block and drag it out to join with the previous code block. This block can be placed above or .
  2. Click the x: value and change to 0.
  3. Click the y: value and change to 0.
  4. At the left, under Events, find the When (green flag) clicked code block. Add it to the top of your new "stack," for a total of three blocks.

Your finished stack of code blocks should look like this:

That's it! You're done! Now you can go play a game you made yourself!

How to Share Your Scratch Project!

You need to have a free Scratch account to save and share programs.

  1. Go to scratch.mit.edu.
  2. Scratch Sign in button
    In the upper right, select the Sign in button.
     
  3. Scratch Sign in window
    In the Sign in window, enter your Username and Password and click Sign in.





     
  4. My Stuff
    In the upper right, click your Username.
  5. From the drop-down menu, select My Stuff.



     
  6. On the My Stuff page, select either the name of the project to go to the Project Page.
  7. On the Project Page, fill in any information you wish in the Instructions or the Notes and Credits boxes.
  8. Share button
    In the upper right, click the Share button to make your program public. Now others can try it, leave comments, and remix it to make their own version!
     
  9. Copy Link button
    In the lower right, select the Copy Link button, so you can send the link to your friends!
     
  10. Unshare link
    To make the project private again, return to the My Stuff page and click the Unshare link.

 

Staff LADR