Published on: Mar 4, 2016
Transcripts - Pong Game
For this honors enrichment component class I decided to create a pong game. This game was
made for two players that can only play it sharing one computer. The game itself is very simple and
does not give the players a lot of variety of levels, difficulty and also it does not have a score
system.When one of the two players misses a ball a message pops up on the screen, saying that
another player won. That is why neither of two players can win. In other words it is an infinite game
that will go on until one of the players will press exit.
The game was written in C++ programming language. Standard C++ IDE does not allow
users to integrate graphics into the code. And because a pong game requires animation I needed a
library. For this game I used Standard DirectMedia Layer, a cross-platform multimedia library
which allows the user to create two- dimensional animations and games and also provides low
level access to audio, keyboard, mouse, joystick.
In my code I used a lot of object oriented programming concepts. And speaking more
precisely- classes. Class allows me to define a completely new type object, what other standard C++
types do not allow you to do. In this image you can see that I have defined the paddle on the left as
a new class. It has members for velocity, a collision box and I am defining several functions for
movement, a constructor for initialization, and input and output on the screen.
For the second paddle I used the
same type of the class. The class that I
created for the ball is different from
the previous two because it has a function
for resetting and another function that will
check if the ball hit one of the paddles. In
other words, when one of the players wins
the ball will be reset in the center of
Here I am defining some surfaces that will be used to upload images on them. Both paddles,
background and ball are just images that will be outputted on the screen.
Also I needed a function that will upload all surfaces on the screen.
Here you can see a function that will calculate the sides of the paddles.
I created two constructors for two paddles and another for the ball. The constructor is an
initialization process which will be called automatically at the time of object creation. It prepares the
new object for use, often accepting parameters which the constructor uses to set any member
variables required when the object is first created. It is called a constructor because it constructs the
values of data members of the class. Here you can see a constructor for the ball.
The reset function resets the ball to the center of the screen when the ‘spacebar’ is pressed and
handle_input function randomizes the ball’s direction when the game starts. Here you can see both
of the functions.
Here is a function that creates the ball’s movement.
In order for the game to start the ‘space bar’ must be pressed and the ball will start moving in a
random direction, and when one of the players misses the ball the following function will do
calculations to prove it or if the player did not miss the ball it will just bounce off the paddle.
Here is my game in working condition.
When one of the players misses the ball the following happens. Here you can see that the player who
controls the green paddle missed. After that one of the players should press ‘space bar’ and the game
This is the first game that I created using C++ programming language. In the process of
the creation of this pong game I learned a lot and improved my existing knowledge about object
oriented programming and Standard DirectMedia Library.