Hints For The Game of Pong

  1. Click here to load this file. Then save it to your Snap! account.
    Examine the starter file where a rectangular paddle sprite and a circular ball sprite have been created for you.
  2. Write the code for the paddle first. The paddle should be allowed to move only up and down near the right edge of the screen. When the user presses the up/down arrow keys on the keyboard, the paddle should move up or down a certain amount but it should never wander off the edges of the screen. You may find the following blocks useful.
    Paddle code blocks: when green flag clicked, when up arrow pressed, when down arrow pressed, go to x: (230) y:(0), if on edge, bounce, change y by (15), change y by (-15)
  3. The code for the ball is a bit trickier due to the paddle bounce. Initialize the ball to appear in the middle of the screen and give it a random direction. Then check if the ball is at the edge of the screen or is touching the paddle. In each case the ball needs to bounce. Snap! already provides you with a block for bouncing off the edges of the screen.
    if on edge, bounce
    You can write the code for the ball bouncing off the paddle by realizing that the ball bounces from the paddle with the negative of the direction at which it hits the paddle. Remember that in Snap! positive angles are measured clockwise from the 0°, up direction, and negative angles are measured counterclockwise from the 0°, up direction.
    Ball bouncing with final direction equal to the negative of the initial direction. Directions: 0, 90, 180, 270 and 0, -90, -180, -270
    Now write the code for the ball. You may find the following blocks useful.
    Ball Code blocks: when green flag clicked, forever, if( ), (-1)X(direction), touching(Paddle)?, go to x:(0) y:(0), point in direction (), if on edge, bounce, move (5) steps, pick random (0) to (360)