Better Programming

Advice for programmers.

Follow publication

Create the Classic Ping-pong Game With Java

Nicolai B. Andersen
Better Programming
Published in
9 min readMay 17, 2022
A GIF showing Ping-pong.
A GIF showing the Ping-pong game from this article.

1. Download and install Processing

2. What is Processing?

3. Game Requirements

3.1 Visual requirements

3.1 Functional requirements

4. Game Objects

Figure 1: A UML Class diagram showing the two classes used in the Ping-pong game.
Figure 1: A UML Class diagram showing the two classes used in the Ping-pong game.

4.1 The player object

A GIF showing the properties of each player and the path (the red line) they can move.
Figure 2: A GIF showing the properties of each player and the path (the red line) they can move.
Figure 3: Shows the implementation of properties in the player class.
Figure 4: Shows the implementation of the player constructor, and getter and setter methods.
Figure 5: Shows the implementation of the player’s update method.
The player object is moved at the anchor point (the value of the position property) and it should therefore stop at the screen height subtracted by b and h to ensure the rectangle stop when its bottom side reaches the boundary.
Figure 6: The player object is moved at the anchor point (the value of the position property) and it should therefore stop at the screen height subtracted by b and h to ensure the rectangle stop when its bottom side reaches the boundary.

4.2 The ball object

A GIF showing the properties of the ball object.
Figure 7: A GIF showing the properties of the ball object.
Figure 8: The implementation of properties in the ball class.
Figure 9: The implementation of the ball’s constructor, and the methods, getPosition(), resetMovement(), invertDirectionX(), and update().
The figure shows the ball drawn within a coordinate system. The anchor point is the ball’s position, and each vector/arrow points to a point relative to the ball’s anchor point. Each vector is rotated 45 degrees.
Figure 10: The figure shows the ball drawn within a coordinate system. The anchor point is the ball’s position, and each vector/arrow points to a point relative to the ball’s anchor point. Each vector is rotated 45 degrees.
The formula for converting degrees to radians.
The formula for rotating a vector v, θ radians
Figure 11: The figure shows a player object overlapping with the ball object. It also shows each corner point of a player based on its position, width, and height property.
Figure 12: The figure shows the implementation of the ‘overlapsWith(player)’ method.

5. Setting up Processing control flow

5.1 Global properties

Figure 13: Shows the implementation of the game’s global properties.

5.2 Setup

Figure 14: Shows the implementation of the game’s setup method.

5.3 Draw

Figure 15: Shows the implementation of the game’s draw method.

5.4 KeyPressed

Figure 16: Shows the implementation of the game’s keyPressed method.

6. Summary

Bibliography

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Nicolai B. Andersen
Nicolai B. Andersen

Written by Nicolai B. Andersen

Programming | Game Development | Writing 💻📚🕹

No responses yet

Write a response