Scratch Project 6: Finch Pong

Description: Use the Finch to control the paddle in a game of Pong.

Programming Concepts: Using the Finch to control Scratch graphics, defining new blocks

Difficulty: Advanced

Approx. Time: 50 Minutes

Programming steps

In this project, the player will move a paddle by tilting the Finch to the right and left. The player must move the paddle so that the ball bounces off of it.

1. Let's start by creating the paddle that the player will control with the Finch. Click the paintbrush icon to paint a new sprite.

2. Under the "Costumes" tab, click the button in the bottom right corner that says "Convert to vector".

3. Use the rectangle tool to draw a narrow rectangle.

4. Center the rectangle by aligning the green circle with the light gray plus sign in the center of the screen.

5. (Optional) Use the paint bucket to color the paddle.

6. Delete the Scratch Cat sprite (Sprite1) by right-clicking it and selecting "delete".

7. Click the blue "i" on the paddle sprite and name it "Paddle".

8. Click the paintbrush icon to paint the ball sprite.

9. Click "Convert to vector".

10. Hold down shift while using the ellipse tool to draw a circle.

11. Center the circle.

12. (Optional) Use the paint bucket to color the ball.

13. Name the ball sprite "Ball".

14. Now that we have the ball and paddle, it is time to make them move! Select the paddle sprite by clicking on it in the Sprites list and select the "Scripts" tab.

15. The game will start when the player presses the space bar. Add a "when space key pressed" block and a forever loop into the program.

16. When the Finch is tilted to the right or left, the paddle should slide in the correct direction. The more the Finch is tilted, the faster the paddle should move. To move the paddle, add a "change x by" block from the "Motion" category to the forever loop.

17. From "More Blocks", add a "Finch Y acceleration" block to the slot in the "change x by" block to move the paddle an amount based on the horizontal tilt of the Finch.

18. Press the space bar to start the program. Hold the Finch so that its beak faces your computer monitor and tilt the Finch so that its right wing is down. Observe what happens.

19. The paddle moves slowly in the wrong direction. To fix this problem, use a multiplication block from the operators category to multiply the acceleration by -12. The 12 increases the speed while the minus sign changes the direction.

20. Run the program and the Finch should easily control the paddle.

21. Now we need to make the ball move. Click on the ball sprite to select it.

The page will now be blank. This is because each sprite has its own set of scripts. These scripts control the properties of that specific sprite. For example, placing a "change x by" block in the scripts of the paddle sprite will cause the paddle sprite to move the specified amount. Putting the same block in the scripts of the ball sprite would move the ball instead. The scripts tab only shows the scripts of the sprite that is currently selected.

22. Start the code with a "when space key pressed" block and a forever loop.

23. To make the ball move, add a "move 10 steps" block from the "Motion" category.

24. After that, use an "if on edge, bounce" block to make the ball bounce when it hits a wall.

25. Run the code and the ball will bounce left and right. To make it bounce at an angle, we need to point it diagonally. From the "Motion" category, add a "point in direction" block right below the "when space key pressed" block. Set the direction to 135.

26. When the game starts, we should also reset the ball's position. Add a "go to" block from the "Motion" category and place it below the "when space key pressed" block. Set the x to 0 and the y to 150.

27. Now let's make the ball bounce off the paddle. Add an if statement from the "Control" category inside the forever loop after the "if on edge, bounce" block.

28. Go to the "Sensing" category and add a "toughing _ ?" block to the if statement and make it determine if the ball is touching the paddle.

29. When the ball bounces off the paddle, it should turn 90 degrees to the left if it is coming from the top left (135 degrees) and 90 degrees to the right otherwise. To do this, add an if...else statement inside the initial if statement.

30. For the condition, use an "=" block from the "Operators" category.

31. In the left side of the "=" block, attach a "direction" block from the "Motion" category and determine if the direction is 135.

32. In the if section, add a turn left block and set it to 90 degrees. In the else section, add a turn right 90 degrees block.

33. Drag the paddle sprite so it is near the bottom of the screen.

34. Run the project and the ball should bounce off of the paddle! Use the stop button to stop the program.

35. If the player misses the ball, the game should end. Add another if statement below the "if is touching paddle" section. This if will run if the ball is below the paddle.

36. Add a "<" block from the "Operators" category as the condition.

37. in the first section on the "<" block, add a "y position" block from the "Motion" category.

38. Go to the "Sensing" category and add a "x position of Paddle" block to the second section of the "<" block. Select "y position" from the drop down menu.

39. Add a "say" block from the "Looks" category inside the if statement. Set the text to "Game over!"

40. When the game ends, the ball should no longer interact with the paddle and should simply bounce around the screen. To do this, we will need to make a variable that keeps track of if the game is currently running. Under the "Data" category, click "Make a Variable" and name it "Game Running".

41. Move the two if statements that are inside the forever loop into a new if statement. This can be done by dragging a new if statement over the gap between the "if on edge, bounce" block and the "if" block below it.

42. Make the if statement check if Game Running = 1.

43. Before the "say" block, add a "set Game Running to 0" block from the "Data" category.

44. Right below the "when space key pressed" block, add another set block that sets Game Running to 1.

45. Now run the program and the ball should stop bouncing off the paddle when the game ends.

46. Let's add a variable that knows how many times the user hit the ball in a row. Make a new variable called "Hit Counter".

47. Set the variable to 0 when the program starts.

48. Add one each time the ball bounces off of the paddle.

49. We can make the game more interesting by randomizing the initial position of the ball. Instead of setting the x to 0 when the game starts, use the "pick random" block from the "Operators" category to select a number from -200 to 200.

50. We can also make the game harder by increasing the speed of the ball as the game continues. Make a new variable named "Speed".

51. Set Speed to 5 to make the ball move slowly when the program starts.

52. Increase Speed by 1 each time the ball bounces off of the paddle.

53. Instead of moving 10 steps, make the ball use Speed to determine the number of steps.

54. Set Speed back to 5 when the player loses, so the "Game Over" message is readable.

55. Play the game again and the ball should move faster each time it hits the paddle.

56. We are beginning to have a lot of code that runs at the beginning of the game (all the code above the forever loop). To make everything more organized, we can group this all into one block. Under the "More Blocks" category, click "Make a Block".

57. Name it "Reset".

58. Move the "define Reset" block to an empty space on your screen.

59. Move all the blocks above the forever loop so they are attached to the "define Reset" block. This can be done in two steps: First move the "set Speed to 5" block and all the blocks below it so that they are attached to the "define Reset" block. Then, move the forever loop and the blocks inside it back to the "when space key pressed" block.

60. Now attach a newly-created Reset block directly below the "when space key pressed" block.

The code will behave exactly as before, but we can now use the Reset block anywhere else in our code.

61. Currently, when the player loses and wants to play again, he or she must click the stop button and then press the space bar. It would be better if pressing the space bar immediately reset the game. Add another "when space key pressed" block to an empty area of your screen.

62. Attach a Reset block to it and the game should reset any time the player presses the space bar.

63. When the game is reset, the ball should no longer say "Game Over". Add a "say" block to the end of the Reset block's definition and backspace the text "Hello!" so the block is empty.

64. Now let's add some lights and sound effects to make the game more engaging. Use a "Finch LED color" block and a "Finch buzz" block to make the Finch play a sound at 880 Hz for 250 ms and flash the beak green.

65. Then make the beak turn off again after the ball bounces.

66. When the player loses, make the beak red and play a different tone sequence using two "Finch buzz" blocks and a "wait" block.

67. Turn off the Finch's beak in the definition of the reset block.

68. Finally, go to the "Data" category and uncheck all the variables except Hit Counter, so only it is visible while playing.

Have fun playing pong!

Example file