Created June 21st, 2014. Built on the Raspberry Pi.
The all time classic video game. We had to build it.
One of the tricks I’ve found useful in Scratch is to put a sliver of a specific color in the stage background so you can later trigger something when your sprite is touching that color. In this case, we have a different color for right and left side of the stage so we can keep track of the player’s scores. We also found the “if on edge, bounce” command very helpful.
Here are the sprites involved.
For the players, we created a slightly different color on the edges of the paddle so we could randomly change the angle in use. More on that later.
Here’s the stage code which keeps track of a timer and speeds up the ball to increase the difficulty as the game goes on. For this game, we also added a space bar to start the game and various stage costumes for when the game first starts and when it’s over.
The player paddle has edges with slightly different colors so we can do some interesting things when the ball hits them. That logic is stored in the ball as the paddle code is pretty straightforward.
Note: You’ll need to adjust this code for each player.
The ball code is where the meat of the logic lives. It checks to see if the ball hit the edge of the screen to score the correct player. We also change things up just slightly by adding a random value to the angle the ball leaves the paddle. If the ball hits the edge, we increase that randomness for more fun. Another thing worth noting is how many variables we use (instead of magic values directly in the code) so we can easily fine tune and tweak the game to get the play we like.
Ready to play? Download the Pong Game Scratch file.
Space bar to start.
Player 1: W, S for up and down.
Player 2: Arrow keys for up and down.