Build your programming career by learning through online code schools
Programming can be a fun and challenging career with lots of opportunities to learn…
Lets start by opening a new project, and delete the cat sprite right away. We don’t need him for this project. Next, click on the backdrop, click on costumes and set a nice background color by using the bucket tool. For my project, I have chosen a nice creamy, sand color. Next, create a new sprite by drawing a simple, thick black line onto the painting canvas. Make sure that you set its central point in the middle. Now it is time to program simple movement for this sprite. Go to its scripts tab, and from the Events category add two ‘when space key pressed’ blocks, and one ‘when flag clicked’ block. These blocks run the program when a certain condition becomes true. In the drop down menu choose letter ‘q’ and for the other one choose letter ‘a’. These two keys on the keyboard will be the main control. Now, when I press the ‘q’ key I want the sprite to change its ‘y’ position by 30 units. Add the ‘change y by’ block from the Motion category. Duplicate it, and add it for ‘a’ control, and change it to -30, to make it go the opposite way, downwards. Also, add the ‘if on edge’ bounce blocks both of them, to make sure the sprite does not leave the Scratch pong. And for the ‘flag clicked’ block add the ‘go to x: y:’ block, and set -220 for x coordinates and 18 for y coordinates. This will make the sprite start the game at this position on the stage. Before you test it, click on the blue ‘i’ letter on the sprite profile, and choose – left right – rotation style. This will prevent it from twisting when touching edges. Awesome, test the program to see if everything works as scripted, and it should. It is already beginning to look like the original ping pong game. Next, right click on this sprite, and select duplicate. For this sprite, in the scripts tab, change the controls for upward and downward movement to ‘up’ and ‘down’ arrow keys. Additionally, change the starting x coordinate to 220, which will be the far right side. Before we create the bouncing ball sprite, click on the backdrops, go to its costume tab, and draw one colored line on either side of the stage, use different colors. I will use them as indicators for counting points.
Now I am going to create a ball sprite. Paint a new sprite. Click on the ellipse tool and hold ‘shift’ key while drawing a circle. This will make the program draw an even circle, also, choose to have it filled. Next click on its scripts tab. Add two event blocks. Then go to the data category and click on ‘make a variable’. Name it – player 1 score. Then create another one and name it – player 2 score.
Place two ‘set to’ block right under the event block, for each player. This will set the score 0 when the game starts. Then we want the ball to start from the middle of the stage, add the ‘go to x: y:’ motion block, and set the coordinates to 0. Drag and drop the point in direction block under it, and set it to 80 degrees. Now, from the controls category, add the ‘forever’ loop block. Inside it place one ‘move 10 steps’ and one ‘if on edge, bounce’ motion block. This will make the continuously move and bounce of the edges. It is time to set a condition that will make it collide with the black line sprites in Scratch pong Go to control category, and add the ‘if then’ condition block. Inside its condition window, place the ‘touching color’ block from the sensing category. Click on the color box, and click on the black line sprite to acquire its color code. Now, when the ball touches this color, or the black line sprite, it will turn 85 degrees. This will make it bounce right back. It is not 90 degrees, because the ball would just come back in a straight horizontal line. This makes the game more challenging. Alright, for the second event block, add the ‘forever’ loop block, and inside it place two ‘if then’ condition blocks. I am now going to make a program for counting points. Grab two ‘touching color’ sensing blocks, and place them in either of the condition window. Now, when the ball touches the green color, it will change player 1 score by 1. This block can be found in the data category. And beneath it, add the ‘wait 1 sec’ block, in case the ball touches this color repeatedly twice, as a glitch or something. For the second ‘if then’ condition, pick the other color, and make it change the other players score by 1. Finally, adjust the score count boxes on the stage, and you are ready to play the Scratch pong. You can play it right away here https://scratch.mit.edu/projects/158412386/