PONG
Write your own classic Pong game
Matt Holder takes a trip down memory lane and creates a Pong-style game, for all your retro gaming needs.
OUR EXPERT
Matt Holder was told he wasted far too much of his youth playing Pong – but it turns out it was all in the name of research.
QUICK TIP
Download the code from the following link: https://github. com/mattmole/ LXF300.
We can’t imagine there are many folk who need introducing to Pong. However, for those who do, it is a two-player game in which table tennis (or ping pong) is simulated.
Pong was an early computer game, first released in 1972. Upright arcade cabinet machines existed that ran the game as well as home consoles that became available from 1975 onwards. Pong was first released by Atari and there have been many versions created over the years, both ones that use hardware and versions using various programming languages that run on top of your operating system.
In this article, we are creating a basic Pong game and the full code is available on GitHub (see Quick Tip, left). We cover a number of concepts including the event loop, how to use keyboard control and how to use variables to store each player’s score. Before we begin coding, let’s quickly discuss the gameplay. A paddle is drawn on the left- and right-hand sides of the screen. Each player controls the vertical position of their paddle on the screen, which is used to make sure that when the ball is travelling towards the paddle, it collides and then bounces off. The ball starts from near the centre of the screen with a random angle, to ensure that the game is different each time.
While describing the gameplay, we’ve introduced a few of the elements for which we need to create code: we need to calculate how to generate a random angle and calculate the ball’s trajectory, and how to change the direction of the ball when it bounces off a player’s paddle or off the top or bottom of the screen.